Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am filling in a combo box after an another combobox.text is populated. Every time the user selects the any option the first time combobox.items list populates correctly. If the user goes between items in the first box the second combobox doesnt clear so I get duplicate entries for the same option. I need help clearing the duplicate data or having the Second box clear upon change. Below is my code for populating the second box after the first one is populated. Thanks in advance.
VB
Private Sub cmbDevice_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbDevice.SelectedIndexChanged

Dim StrQ As String = "Select [Model] from " & cmbDevice.Text & " Group By [Model] "
        Dim da As New SqlDataAdapter(StrQ, con)
        If cmbDevice.Text = "*" Then
            cmdType.Text = ""
        Else
            da.Fill(ds, cmbDevice.Text)
            With Me.cmdType
                .DataSource = ds.Tables(cmbDevice.Text)
                .DisplayMember = "Model"
                .SelectedIndex = 0
            End With
        End If
Posted
Updated 1-Dec-11 8:48am
v2

Why don't you just clear the combobox.Items collection before populating it again. Alternatively, you could set the DataSource property to Nothing.
 
Share this answer
 
Comments
Member 8451273 1-Dec-11 15:56pm    
I tried just adding combobox.Items.Clear and I get this Error: "Items collection cannot be modified when the DataSource property is set."
Try to clear DataSource of cmbType before Clearing Items.
VB
Private Sub cmbDevice_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbDevice.SelectedIndexChanged
 
Dim StrQ As String = "Select [Model] from " & cmbDevice.Text & " Group By [Model] "
        Dim da As New SqlDataAdapter(StrQ, con)
        If cmbDevice.Text = "*" Then
            cmdType.Text = ""
        Else
            da.Fill(ds, cmbDevice.Text)
            cmdType.DataSource = "" ' Or you can set it as Nothing
            cmdType.Items.Clear()
            With Me.cmdType
                .DataSource = ds.Tables(cmbDevice.Text)
                .DisplayMember = "Model"
                .SelectedIndex = 0
            End With
        End If
 
Share this answer
 
Comments
Member 8451273 2-Dec-11 9:47am    
Thanks for your input. However I tried adding the two lines of code where you placed it. I still had duplicates. I even moved in to the beginning of the event and still dups. Of course with it at the end of the event my field was blank.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900