Ninay11
Ninay11

Reputation: 45

Like statement in VB.NET

If I enter an input into the textbox, my listview updates, but there is always no result even though my input is there.

Private Sub searchRecord()
    Dim dt As New DataTable
    dt = ExecuteQuery("SELECT * FROM tblSupplier WHERE '" & cboSearch.Text 
                      & "' LIKE '" & txtSearch.Text & "%'")
    lvSupplier.Items.Clear()
    If dt.Rows.Count > 0 Then
        For ctr = 0 To dt.Rows.Count - 1
            Dim item As New ListViewItem
            item.Text = dt.Rows(ctr)("SuppID")
            item.SubItems.Add(dt.Rows(ctr)("SuppName"))
            item.SubItems.Add(dt.Rows(ctr)("SuppAddress"))
            item.SubItems.Add(dt.Rows(ctr)("SuppConPerson"))
            item.SubItems.Add(dt.Rows(ctr)("SuppConNumber"))
            item.SubItems.Add(dt.Rows(ctr)("SuppEmail"))
            lvSupplier.Items.Add(item)
        Next
    End If
End Sub

Dim dt As New DataTable
dt = ExecuteQuery("SELECT * FROM tblSupplier")

Try
    If txtSearch.Text = "" Then
        Call fillSupplier(dt, lvSupplier)
    Else
        Call searchRecord()
    End If
Catch ex As Exception
    MsgBox(ex.ToString)
End Try

Upvotes: 0

Views: 6892

Answers (1)

chris_techno25
chris_techno25

Reputation: 2477

Your SQL statement should be like this...

"SELECT * FROM tblSupplier WHERE " & cboSearch.Text & " LIKE '%" & txtSearch.Text.Replace("'","''").Trim() & "%'"

This way you should be able to search for any character or word wherever it's placed in the original word or phrase found in the database.

Upvotes: 2

Related Questions