Search code examples
databasevb.netlocal-database

Search by LIKE not working in database vb.net


For some reason the like is not working, for example is the name where Jason and I search for 'Ja', 'jason' should show up it does not. Is my code faulty? This is in a local database, maybe that helps?

 Private Sub BTN_Search_Click(sender As Object, e As EventArgs) Handles BTN_Search.Click
    'If txtbox is blank then show all records, else do the search by first name.
    If TBX_Search.Text = "" Then
        DoctorsDataGridView.DataSource = Me.RecordsDataSet.Doctors.Select("FirstName LIKE'" & "%" & "'")
    Else
        DoctorsDataGridView.DataSource = Me.RecordsDataSet.Doctors.Select("FirstName LIKE'" & TBX_Search.Text & "'")
    End If
 End Sub

Solution

  •  Private Sub BTN_Search_Click(sender As Object, e As EventArgs) Handles BTN_Search.Click
          'If txtbox is blank then show all records, else do the search by first name.
          If TBX_Search.Text = "" Then
               DoctorsDataGridView.DataSource = Me.RecordsDataSet.Doctors.Select("FirstName LIKE '%'")
          Else
               DoctorsDataGridView.DataSource = Me.RecordsDataSet.Doctors.Select("FirstName LIKE '%" & TBX_Search.Text & "%'")
          End If
     End Sub
    

    In your code you tell the database to fetch articles matching Ja instead of looking for Ja% (and anything behind it)

    • Add the % before your searchstring to allow results with anything in front of the Ja
    • Add the % after your searchstring to allow results with anything after the Ja
    • Add them both before and after the searchstring to match any result with the term Ja in it