Click here to Skip to main content
15,919,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I'm looking for help with cascading combo boxes in VB.NET. I've populated 3 combo boxes using 3 separate Data Tables. I'd like my second combo box to change based on the selected value of the first combo box etc... My Tables are defined as follows:

Table 1
-------
ID_1
Name_1
-

Table 2
-------
ID_2
Name_2
ID_1


Table 3
-------
ID_3
Name_3
ID_2

Thanks
Posted

No problem,

Here is what you do

Simply use the selected value from the first dropdown to load the next, and so on...

This is your dropdown list
<asp:DropDownList runat="server" ID="ddl" DataValueField="IDFIELD" DataTextField="TEXTFIELD" OnSelectedIndexChanged="ddl_OnSelectedIndexChanged" />

protected void Page_Load(object sender, EventArgs e)
{
ddlOne.Datasource = selectcommand();
ddlOne.DataBind();
}

protected void ddl_OnSelectedIndexChanged(object sender, EventArgs e)
{
ddlNext.DataSource = selectcommand(ddlOne.SelectedValue);
ddlNext.DataBind();
}


I hope this helps,,, if not, respond and I will throw in an actual example for you.

Also, if you don't want the page to post back every time, you can wrap this page in an UpdatePanel :)

happy coding!
 
Share this answer
 
Hi Chevy,
Thanks for your response. I tried the following but i'm getting a Null Exception Error:

VB
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
       ComboBox2.DataSource = ds.Tables("Sections").Rows(ComboBox1.SelectedValue).Item("Dept_ID")
   End Sub


If you could post your example it would be great.

Thanks
 
Share this answer
 
Well, you're on the right track, but you're trying to bind a control to a single value. I'll try to give you a working example:

VB
Imports System.Data.OleDb

Public Class Form1
    Private Const dbLoc As String = "D:\something.mdb"
    Private Const password As String = "something"
    Public connectionString As OleDbConnection
    Public boolLoading As Boolean

    Private Sub Form1_Load(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) _
                Handles MyBase.Load
        'Set up the connectionString
        connectionString = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
                                                     "Data Source=" & dbLoc & _
                                                     ";Jet OLEDB:Database Password=" & password)

        'Open the connectionString
        Try
            connectionString.Open()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        'Set up the dataset
        Dim cbo1DataSet As New DataSet
        Dim myDataAdapter As OleDbDataAdapter
        Dim sql As String = "SELECT * FROM TreeData;"
        myDataAdapter = New OleDbDataAdapter(sql, connectionString)

        myDataAdapter.Fill(cbo1DataSet, "TreeData")
        myDataAdapter.Dispose()

        boolLoading = True
        'Set up the ComboBox
        With ComboBox1
            .DataSource = cbo1DataSet.Tables("TreeData")
            .DisplayMember = "Display_Name"
            .ValueMember = "DataID"
            .SelectedIndex = 0
        End With
        boolLoading = False

        'close the connectionString
        connectionString.Close()
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If boolLoading Then Return

        'Open the connectionString
        Try
            connectionString.Open()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        'Set up the dataset
        Dim cbo2DataSet As New DataSet
        Dim myDataAdapter As OleDbDataAdapter
        Dim sql As String = "SELECT * FROM Data WHERE ID=" & ComboBox1.SelectedValue & ";"
        myDataAdapter = New OleDbDataAdapter(sql, connectionString)

        myDataAdapter.Fill(cbo2DataSet, "Data")
        myDataAdapter.Dispose()

        'Set up the ComboBox
        With ComboBox2
            .DataSource = cbo2DataSet.Tables("Data")
            .DisplayMember = "Name"
            .ValueMember = "ID"
            .SelectedIndex = 0
        End With

        'close the connectionString
        connectionString.Close()
    End Sub
End Class
 
Share this answer
 

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