Im having some trouble creating a search query for displaying items in a datagrid view, the error im getting is "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"
Below is my code:
Try
connect()
Dim sql = "SELECT pcb, component, hour, faultcode, line FROM [sqlcnvfaultentry] WHERE "
If CheckBox_pcb.Checked Then
Sql = Sql & " and pcb = @pcb "
cmd.Parameters.AddWithValue("@pcb", ComboBox_pcb.Text)
End If
If CheckBox_part.Checked Then
Sql = Sql & " and component = @component "
cmd.Parameters.AddWithValue("@component", ComboBox_part.Text)
End If
If CheckBox_hour.Checked Then
Sql = Sql & " and hour = @hour "
cmd.Parameters.AddWithValue("@hour", ComboBox_hour.Text)
End If
If CheckBox_fault.Checked Then
Sql = Sql & " and faultcode = @faultcode "
cmd.Parameters.AddWithValue("@faultcode", ComboBox_fault.Text)
End If
If CheckBox_line.Checked Then
Sql = Sql & " and line = @line "
cmd.Parameters.AddWithValue("@line", ComboBox_line.Text)
End If
Dim adapter = New SqlDataAdapter(cmd.CommandText, con.ConnectionString)
Dim dt As New DataTable()
cmd.CommandText = Sql
adapter.Fill(dt)
DataGridView_c1.DataSource = dt
DataGridView_c1.Refresh()
DataGridView_c1.Columns(0).HeaderText = "PCB:"
DataGridView_c1.Columns(1).HeaderText = "Component:"
DataGridView_c1.Columns(2).HeaderText = "Hour:"
DataGridView_c1.Columns(3).HeaderText = "Fault Code:"
DataGridView_c1.Columns(4).HeaderText = "Line:"
disconnect()
Catch exp As Exception
Throw exp
Finally
End Try
Any help would be great.
WHERE and blah = @blahat the end of the the command text. That syntax is invalid between the "where" and the "and". Also, you are setting thecommandTextof yourcmdobject after you have already created theSqlDataAdapterfrom it.cmd.CommandText = Sqlline to a line above theDim adapter =bit..Where(...)clauses dynamically