Error: OleDbException (0x80040e10): No value given for one or more required parameter

Rithin

New member
Joined
Aug 20, 2012
Messages
1
Programming Experience
Beginner
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
 
Using the OleDbCommand object your created:
New OleDbDataAdapter(cmd)

or if you initialize the adapter with the string query, which creates the SelectCommand object:
dataadapter.SelectCommand.Parameters.AddWithValue("@val", "%" & managecustFnameTxtbox.Text & "%")

Don't open the connection, the adapter manages that during Fill.
 
Back
Top