Click here to Skip to main content
Click here to Skip to main content

Cascading Dropdonlist in ASP.NET

, 13 Feb 2008
Rate this:
Please Sign up or sign in to vote.
Cascading dropdown list in using MYSQL database


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)
        myreader = cmd.ExecuteReader()
        Do While myreader.Read()
            state.Items.Add(New ListItem(myreader("state_name"), myreader("country_id")))
        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


Avinash Desai


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


About the Author

Avinash Desai
S.N.Software Technology Bangalore
India India
No Biography provided

Comments and Discussions

GeneralMy vote of 1 PinmemberC.V.Vikram3-Sep-09 0:21 
GeneralMy vote of 1 PinmemberPhilip Smith28-Aug-09 3:41 
Generalhumm... PinmemberRiad MS Afyouni16-Feb-08 21:47 
GeneralRe: humm... Pinmemberavinashdesai1017-Feb-08 18:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 14 Feb 2008
Article Copyright 2008 by Avinash Desai
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid