Hi all,
I am new to coding and also new to this forum so please forgive me if I posted this in the incorrect place.
I have the following piece of code and when I run it I receive the above mentioned error. Basically I am using a text box to input a word. The db is then searched returning words "like" the one entered and must populate the datagrid with the results. If I change the SQL statement to the following - Dim sql As String = "SELECT CustomerID,Firstname FROM Customer WHERE Firstname = 'Rithin'", then it executes fine and populates the datagrid with the correct information where it finds multiple instances of the word Rithin. So not sure why it is giving me the error when I use the "like" statement.
Public Sub findcustomerDetails()
Try
Using myConnection As OleDbConnection = New OleDbConnection(connString)
myConnection.Open()
' Dim test As String
lbresults.Items.Clear()
Dim sql As String = "SELECT CustomerID,Firstname FROM Customer WHERE Firstname LIKE @val"
Using cmd As OleDbCommand = New OleDbCommand(sql, myConnection)
cmd.Parameters.AddWithValue("@val", "%" & managecustFnameTxtbox.Text & "%")
Dim dataadapter As New OleDbDataAdapter(sql, myConnection)
Dim ds As New DataSet()
dataadapter.Fill(ds, "Customer")
Using dr As OleDbDataReader = cmd.ExecuteReader
While dr.Read()
'test = dr("CustomerID") & dr("Firstname")
'lbresults.Items.Add(test)
' lbresults.Items.Add(dr("CustomerID"))
' lbresults.Items.Add(dr("Firstname"))
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Customer"
End While
End Using
End Using
End Using
myConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
I am new to coding and also new to this forum so please forgive me if I posted this in the incorrect place.
I have the following piece of code and when I run it I receive the above mentioned error. Basically I am using a text box to input a word. The db is then searched returning words "like" the one entered and must populate the datagrid with the results. If I change the SQL statement to the following - Dim sql As String = "SELECT CustomerID,Firstname FROM Customer WHERE Firstname = 'Rithin'", then it executes fine and populates the datagrid with the correct information where it finds multiple instances of the word Rithin. So not sure why it is giving me the error when I use the "like" statement.
Public Sub findcustomerDetails()
Try
Using myConnection As OleDbConnection = New OleDbConnection(connString)
myConnection.Open()
' Dim test As String
lbresults.Items.Clear()
Dim sql As String = "SELECT CustomerID,Firstname FROM Customer WHERE Firstname LIKE @val"
Using cmd As OleDbCommand = New OleDbCommand(sql, myConnection)
cmd.Parameters.AddWithValue("@val", "%" & managecustFnameTxtbox.Text & "%")
Dim dataadapter As New OleDbDataAdapter(sql, myConnection)
Dim ds As New DataSet()
dataadapter.Fill(ds, "Customer")
Using dr As OleDbDataReader = cmd.ExecuteReader
While dr.Read()
'test = dr("CustomerID") & dr("Firstname")
'lbresults.Items.Add(test)
' lbresults.Items.Add(dr("CustomerID"))
' lbresults.Items.Add(dr("Firstname"))
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Customer"
End While
End Using
End Using
End Using
myConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub