Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

Cascading Dropdonlist in ASP.NET

1.11/5 (8 votes)
13 Feb 2008Ms-PL 1   14  
Cascading dropdown list in asp.net using MYSQL database

Introduction

This is the code for Cascading dropdown list which helps to know how the dropdownlists are cascaded and how the code written and helps to know adding the items to the dropdown list from the database.

Using the code

Here in this below code it shows that when the first dropdown's selected text changes immediately second dropdown loads and it helps in the case a user has some super categories and after selecting that super categories it has to display the sub categories respective to the values selected in the dropdownlist 1.

In below code connection string for mysql is assigned to a variable and here i used ODBC to establish a connection to the database and table.Data in the dropdownlist1 is populated automatically and after selecting first second drop down gets populated with the help of "Do while " statement , the data goes on adding up to the condition is false.


    Protected Sub country_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles country.SelectedIndexChanged
        Dim myreader As Odbc.OdbcDataReader
        Dim ConnStr As String = "Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=TaxPRo;uid=root;pwd=password;option=3"
        Dim con As OdbcConnection = New OdbcConnection(ConnStr)
        Dim cmd As OdbcCommand = New OdbcCommand("select * from states where country_id=" & country.SelectedValue & " order by state_name", con)
        con.Open()
        
        myreader = cmd.ExecuteReader()
       
        
        Do While myreader.Read()
            
            state.Items.Add(New ListItem(myreader("state_name"), myreader("country_id")))
                      
        Loop
        myreader.Read()
        state.DataValueField = ("country_id")
    End Sub
 <%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Odbc" %>

Remember to import these above Namesapce if you are using ODBC

By

Avinash Desai

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)