I cannot seem to get the datagrid to update with the correct data from the combobox selection the program just crashes. Do I need to bind to the combobox some how?? Any help or advise would be apreciated

Private Sub cboBarcodeInCust_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboBarcodeInCust.SelectedIndexChanged
        Dim ds As New DataSet
        Dim Count As New SqlDataAdapter("Select * From VwBarcodeTotals where AccountNo like = '" & cboBarcodeInCust.SelectedValue & "'", Con)
        Count.Fill(ds, "BarcodeTotals")

        dgWorkwearIn.DataSource = ds.Tables("BarcodeTotals")
        dgWorkwearIn.Columns("AccountNo").Visible = False

Hi folks,
solved it!!! Had the "=" sign in the select statement and I put the code into its own sub and had the selectedindexchanged event call it works fine now
here is code if helps anyone else

Public Sub BarcodeTotal()
        Dim ds As New DataSet
        Dim Count As New SqlDataAdapter("Select * From VwBarcodeTotals where Name like'" & cboBarcodeInCust.Text & "'", Con)
        Count.Fill(ds, "BarcodeTotals")

        dgWorkwearIn.DataSource = ds.Tables("BarcodeTotals")
        dgWorkwearIn.Columns("AccountNo").Visible = False

The equals sign is not the problem, its the method of retrieving your text from the combobox that changed. A combox item can have two values, the DisplayMember which is the text that is displayed as each item in the cbo and a hidden value can be assigned to each item in the ValueMember property. cbo.SelectedValue will return the value member but you also need to convert its value into its proper datatype. Such as

cbo.SelectedValue.ToString
or
CInt(cbo.SelectedValue)

Two things I would suggest, for getting the display text as it appears you are trying to do, use the following
cboBarcodeInCust.GetItemText(cboBarcodeInCust.SelectedItem)

The SelectedIndexChanged event will fire multiple times when your first loading your combobox. I would suggest either adding coding to exit the event during your initial load/fill or move your coding to the SelectionChangeCommitted event.

Edited 7 Years Ago by TomW: n/a

Comments
Very Good advise Thanks

Thanks for your help. I changed the code into theSelectionChangeCommitted event and it works a treat now. The Value Member of the combo box is accountNo which is the primary key from the sql database but I could only get the datagrid to filter when I added the Account name ,which is the combbox display member, to the sql select statement

Dim Count As New SqlDataAdapter("Select * From VwBarcodeTotals where Name like'" & cboBarcodeInCust.Text & "'", Con)

Probably doing something wrong here to even though the way I have done it still works!

A primary key sounds perfect for filtering your database. Let me ask to be sure, is AccountNo a numeric datatype or text/string datatype?

A primary key sounds perfect for filtering your database. Let me ask to be sure, is AccountNo a numeric datatype or text/string datatype?

Good question. The primary key is string. It is from database that I didn't design I would of used Numeric ident as primary but never mind!

I would suggest something like:

Private Sub FillDataset()

        Using con As New SqlConnection(strMyConnectionString)
            Dim cmdSelect As New SqlCommand
            Dim da As New SqlDataAdapter

            With cmdSelect
                .Connection = con
                .CommandType = CommandType.Text
                .CommandText = "Select * From VwBarcodeTotals Where AccountNo = @AccountNo"
                .Parameters.AddWithValue("@AccountNo", ComboBox1.SelectedValue.ToString)
            End With

            da.SelectCommand = cmdSelect
            da.Fill(ds, "BarcodeTotals")

            da.Dispose()
            cmdSelect.Dispose()
        End Using 'con

End Sub

Thats great TomW! Had a few other select statements like that and have changed them too. probably alot safer as well to use Parameters. Thanks again

This question has already been answered. Start a new discussion instead.