0

One time when I run this code it run smoothly but then when i search for names there is no record appears. Badly need your help guys for our program project. thanks in advance :)

This is my code :

Private Sub search_btn_Click(sender As Object, e As EventArgs) Handles search_btn.Click
    Me.Show()
    Search_Record()

End Sub

Private Sub Search_Record()
    Dim conn As New OleDbConnection
    Dim cmd As New OleDbCommand
    Dim da As New OleDbDataAdapter
    Dim dt As New DataTable
    Dim sSQL As String = String.Empty

    Try

        conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\VB\HSU\HSU\G11B.mdb"

        conn.Open()
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        sSQL = "SELECT Last_Name, First_Name FROM 10ABM"
        sSQL = sSQL & "WHERE Last_Name like '%" & Me.search_txt.Text & "%'"
        cmd.CommandText = sSQL
        da.SelectCommand = cmd
        da.Fill(dt)
        Me.DataGridView1.DataSource = dt
        If dt.Rows.Count = 0 Then
            MsgBox("No record found!")
        End If
    Catch ex As Exception
        MsgBox(ErrorToString)
    Finally
        conn.Close()
    End Try

End Sub
Kevin1412
  • 1
  • 1
  • 2
    This is a classic case of not looking at what's in front of you. Have you looked at the value of `sSQL`? I'll wager not, because it's not valid SQL code. – jmcilhinney Feb 21 '19 at 06:43
  • I think you have not included any space before the `WHERE` class. So now your query is like `SELECT Last_Name, First_Name FROM 10ABMWHERE Last_Name.....` – Akhilesh B Chandran Feb 21 '19 at 06:51
  • 2
    @AkhileshBChandran Although you're right about the missing space, the OP should **Not** _just_ add that space. Instead, they should use a [parameterized query](https://stackoverflow.com/q/542510/4934172) to avoid [SQL Injection](http://en.wikipedia.org/wiki/SQL_injection). – 41686d6564 stands w. Palestine Feb 21 '19 at 07:01
  • @AhmedAbdelhameed Yeah that's correct – Akhilesh B Chandran Feb 21 '19 at 07:16
  • Just to add my 2-pence.... don't forget to `dispose` your `cmd`, you have `conn.close`, thats good, always use `cmd.dispose` as well with that... just a good habit to get into! :) – Chicken Feb 21 '19 at 09:27
  • Thanks a lot guys for helping. Im just a beginner using vb.net for our project. – Kevin1412 Feb 22 '19 at 13:24

0 Answers0