Click here to Skip to main content
15,888,802 members
Articles / Web Development / HTML
Article

Cascading Dropdonlist in ASP.NET

Rate me:
Please Sign up or sign in to vote.
1.11/5 (8 votes)
13 Feb 2008Ms-PL 51.8K   14   17   4
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)


Written By
S.N.Software Technology Bangalore
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
C.V.Vikram3-Sep-09 0:21
C.V.Vikram3-Sep-09 0:21 
GeneralMy vote of 1 Pin
Philip Smith28-Aug-09 3:41
Philip Smith28-Aug-09 3:41 
Generalhumm... Pin
Riad MS Afyouni16-Feb-08 21:47
Riad MS Afyouni16-Feb-08 21:47 
GeneralRe: humm... Pin
Avinash Desai17-Feb-08 18:28
Avinash Desai17-Feb-08 18:28 

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

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