renge
renge

Reputation: 27

Search ListView by date format

i have a listview with columns:

ID, Contact_Name, Contact_Number, DateAndTime, Messages

I want to search the listview using "Date" in DateAndTime Column.

using textbox_textChanged:

Try


 Dim sqlQuery As String = "SELECT From SentItems WHERE DateAndTime like '%" & TextBox6.Text & "%'"


            Dim sqlCommand As New OleDbCommand
            Dim sqlAdapter As New OleDbDataAdapter
            Dim Table As New DataTable

            With sqlCommand
                .CommandText = sqlQuery
                .Connection = cn

            End With
            With sqlAdapter
                .SelectCommand = sqlCommand
                .Fill(Table)

            End With
            lvSent.Items.Clear()

            For i = 0 To Table.Rows.Count - 1
                With lvSent
                    .Items.Add(Table.Rows(i)("ID"))
                    With .Items(.Items.Count - 1).SubItems
                        .Add(Table.Rows(i)("Contact_Name"))
                        .Add(Table.Rows(i)("Contacts_Number"))
                        .Add(Table.Rows(i)("DateAndTime"))
                        .Add(Table.Rows(i)("SentMessage"))
                        .Add(Table.Rows(i)("Status"))


                    End With

                End With
            Next

        Catch ex As Exception

        End Try

the format of date in textbox6 = M/d/yyyy

the format of date in listview = M/d/yyyy

the problem is every time I searched and the result found(matched). All items in listview appear also even it do not match the text in textbox. please help guys... thanks....

Upvotes: 0

Views: 1419

Answers (1)

King of kings
King of kings

Reputation: 695

Change your query as like this

Dim dt as string
dt=Format(CDate(TextBox6.Text),"yyyy-MM-dd")
Dim sqlQuery As String = "SELECT From SentItems WHERE DateAndTime like '%" & Format(CDate(dt),"yyyy-MM-dd") & "%'"

Upvotes: 1

Related Questions