Home > database >  Search button for a database in vb.net for listview
Search button for a database in vb.net for listview

Time:11-11

The code below is not searching, keeps showing error. It is supposed to search for records using a number.

Private Sub btn_search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_search.Click
    Me.Sales_fileBindingSource.Filter = "Receipt Number LIKE '" & "%'" & txt_search.Text & "%'"
End Sub

The error being shown is: Syntax error: Missing operand after 'Number' operator. Click here to see error screenshot

CodePudding user response:

It would make it easier to see what's going on if the code used an interpolated string, so it might end up like:

Me.Sales_fileBindingSource.Filter = $"[Receipt Number] LIKE '%{txt_search.Text}%'"

It is necessary to put the column name in square brackets as it has a space in it - there are examples of the syntax at DataView RowFilter Syntax [C#] (the syntax for the filter is independent of the language).

If you are using an older version of Visual Basic, before interpolated strings were introduced (the year 2015), you'll need something like:

Me.Sales_fileBindingSource.Filter = String.Format("[Receipt Number] LIKE '%{0}%'", txt_search.Text)

which, while a bit longer, still makes it easier to see what is happening than using lots of " & x & "'-type code.

CodePudding user response:

Finally got it! All i had to do was use an under score to join the two words Receipt and Number to one like this "Receipt_Number" Also made other changes like using String.Format. Check code below.

 Me.Sales_fileBindingSource.Filter = String.Format("(Convert(Receipt_Number, 'System.String') LIKE '%{0}%')", txt_search.Text)
  • Related