Hi all!
I have cascading comboboxes and when I choose the value for the first one everything is fine. But when I try to choose the value for the second combo the value of the first disappears. However, the second combobox is populated with the appropriate values and then I have to choose again the value for the first one. This is the code:

For the fisrt Combobox

Dim PostcodeDA As New SqlDataAdapter()
        Dim PostcodeTable As New DataTable
        
        PostcodeDA.SelectCommand = New SqlCommand()
        PostcodeDA.SelectCommand.Connection = conn
        PostcodeDA.SelectCommand.CommandText = "SELECT Postcode,StreetName_LCL, Municipality_LCL FROM T_MUNICIPALITY_STREETS1 WHERE StreetName_LCL= '" & cboStreetName1.Text & "'"
        
        PostcodeDA.Fill(PostcodeTable)
        cboPostcode.DataSource = PostcodeTable
        cboPostcode.DisplayMember = "Postcode"
        cboPostcode.ValueMember = "Municipality_LCL"
        cboPostcode.SelectedValue = 0

And for the second (cascading) combobox which is disappeared only when a checked box is checked and thats the reason I wrote it that way:

Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
        lblStreetName2.Visible = True
        cboStreetName2.Visible = True
        lblStreetNumber.Visible = True
        txtStreetNumber2.Visible = True


        Dim StreetName2DA As New SqlDataAdapter()
        Dim StreetName2Table As New DataTable
       
        StreetName2DA.SelectCommand = New SqlCommand()
        StreetName2DA.SelectCommand.Connection = conn
        StreetName2DA.SelectCommand.CommandText = "SELECT StreetName_LCL FROM T_MUNICIPALITY_STREETS1 WHERE Postcode='" & cboPostcode.Text & "' order by StreetName_LCL"

        StreetName2DA.Fill(StreetName2Table)

        cboStreetName2.DataSource = StreetName2Table
        cboStreetName2.DisplayMember = "StreetName_LCL"
        cboStreetName2.ValueMember = "StreetName_LCL"
        Me.cboStreetName2.SelectedValue = 0

        If CheckBox1.Checked = False Then
            lblStreetName2.Visible = False
            cboStreetName2.Visible = False
            lblStreetNumber.Visible = False
            txtStreetNumber2.Visible = False
        End If
    End Sub

Recommended Answers

All 5 Replies

Sorry I made a mistake.. I meant that the second combobox appears when the checkbox is checked..

I would suggest to use the SelectedIndex istead of SelectedValue.
Also I will rewrite the CheckBox1_CheckedChange sub to some thing like:

Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged

   lblStreetName2.Visible = False
   cboStreetName2.Visible = False
   lblStreetNumber.Visible = False
   txtStreetNumber2.Visible = False
   
   If CheckBox1.Checked = True Then
        Me.Cursor = Cursors.WaitCursor
        Dim StreetName2DA As New SqlDataAdapter()
        Dim StreetName2Table As New DataTable
       
        StreetName2DA.SelectCommand = New SqlCommand()
        StreetName2DA.SelectCommand.Connection = conn
        StreetName2DA.SelectCommand.CommandText = "SELECT StreetName_LCL FROM T_MUNICIPALITY_STREETS1 WHERE Postcode='" & cboPostcode.Text & "' order by StreetName_LCL"

        StreetName2DA.Fill(StreetName2Table)

        cboStreetName2.DataSource = StreetName2Table
        cboStreetName2.DisplayMember = "StreetName_LCL"
        cboStreetName2.ValueMember = "StreetName_LCL"
        Me.cboStreetName2.SelectedValue = 0

        lblStreetName2.Visible = True
        cboStreetName2.Visible = True
        lblStreetNumber.Visible = True
        txtStreetNumber2.Visible = True
        Me.Cursor=Cursors.Default
    End If
End Sub

This way, you only do the search in the DB when it becomes visible. And also, you notify the user that the program is thinking by means of the cursor.

Hope this helps

No change at all..
When I change the selected value to selected index there is an error..
If there is any idea please help..!

This will mean that you have no items on the combo.

Please chech the Count of items in the combo before selecting any thing.

Hope this helps

Also, if the item value is a string (because the StreetName_LCL seems to be string), asigning an integer to the selected value would select nothing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.