Click here to Skip to main content
Licence Ms-PL
First Posted 13 Feb 2008
Views 31,453
Downloads 14
Bookmarked 18 times

Cascading Dropdonlist in ASP.NET

By | 13 Feb 2008 | Article
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)

About the Author

Avinash Desai


S.N.Software Technology Bangalore
India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberC.V.Vikram0:21 3 Sep '09  
GeneralMy vote of 1 PinmemberPhilip Smith3:41 28 Aug '09  
Generalhumm... PinmemberRiad MS Afyouni21:47 16 Feb '08  
GeneralRe: humm... Pinmemberavinashdesai1018:28 17 Feb '08  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 14 Feb 2008
Article Copyright 2008 by Avinash Desai
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid