I have a form in my vb.net application used to take the data about the returned stock. The form contains two comboboxes. One, named combobox5, contains invoice numbers and the other, named combobox3, contains party codes. Both the comboboxes are pre_loaded using sqldataadapter.
Now what i want is to change the party code in combobox3 when the invoice number is changed in combobox5. Elaborating it further, When Stock is issued the party code is stored along with the invoice number to keep track of to which party was the stock issued. Now when stock is returned i want to keep track that which party has returned the stock and i want that the party code should be automatically selected when the invoice number is changed and it should be what is stored in the database against that particular invoice number….
I’m using the following code for doing so:
Private Sub ComboBox5_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox5.SelectedIndexChanged
' defines a new connection to the database
Dim con As New SqlConnection("Data Source=TAHA;Initial Catalog=ADT;Integrated Security=True")
con.Open()
If ComboBox5.SelectedIndex = 0 Then
ComboBox3.Enabled = True
If Not ComboBox3.Items.Count = 0 Then
ComboBox3.SelectedIndex = 0
End If
Else
Me.ComboBox3.Enabled = False
Me.ComboBox3.BackColor = Color.White
Me.ComboBox3.ForeColor = Color.Black
Dim invoices As New SqlCommand("select invoice_no, party_code from Outgoing_Invoice group by invoice_no, party_code", con)
Dim reader As SqlDataReader = invoices.ExecuteReader
While reader.Read
Dim cnt, i As Integer
cnt = Me.ComboBox3.Items.Count
If Me.ComboBox5.SelectedItem.ToString.Trim = reader("invoice_no").ToString.Trim Then
If Not cnt = 0 Then
For i = 0 To cnt - 1
If Me.ComboBox3.Items.Item(i).ToString.Trim.Contains(reader("party_code").ToString.Trim) Then 'here i have also used equals instead of contains but that too doesn't work
Me.ComboBox3.SelectedIndex = i
Exit For
End If
Next
End If
End If
End While
reader.Close()
End If
con.Close()
End Sub
You want to use the SelectionChangeCommitted event instead because SelectedIndexChanged will also be fired when the selection is changed programmatically.