Prashant Prashant - 2 months ago 15
Vb.net Question

Selecting data between dates - "No value given for one or more required parameters"

I have put following code to extract data from Microsoft Access database to populate a Combobox in VB.NET

Private Sub PopulateList()
If RBDura.Checked = True Then
CmbTstNo.Items.Clear()
myConnection.ConnectionString = connstring
Try
myConnection.Open()
Catch ex As Exception
MsgBox(ex.Message)
End Try
str = "SELECT DISTINCT Test_Code FROM Table_Durability_Test WHERE Log_Date BETWEEN " & DtFrom.Text & " and " & DtTo.Text & ""
cmd = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
While dr.Read()
CmbTstNo.Items.Add(dr.Item(0))
End While
myConnection.Close()
CmbTstNo.SelectedIndex = 0
End If
End Sub


However when the code is run, I get an error "No value given for one or more required parameters"

Please Guide..

Answer

The SQL string you have is "SELECT DISTINCT Test_Code FROM Table_Durability_Test WHERE Log_Date BETWEEN " & DtFrom.Text & " and " & DtTo.Text & ""

If you are entering a text or string to a database using SQL, you have to surround each text entry with single quotes so your SQL string would look like this:

SELECT DISTINCT Test_Code FROM Table_Durability_Test WHERE Log_Date BETWEEN 'Value of DtFrom.Text' and 'Value of DtTo.Text'

Notice that I have surrounded the Value of DtTo.Text and Value of DtFrom.Text with single quotes.

Comments