Cascading A DropDownList With Another DropDownList in ASP.Net

This article shows how to cascade one dropdown list with another dropdown list in ASP.Net using C#. Here I took three dropdown lists, Country, State, and City. You will see how to fill the state dropdownlist based on the data of the country dropdownlist. So here we go!
 
Initial Chamber
 
Step 1
 
Open your VS10, create an empty website (I hope you will get the idea of creating an empty website from my previous articles). Name it "Cascade_dropdownlist_demo".
 
Step 2
 
In Solution Explorer, right-click on your website then select Add New Item -> Web Form. Name it dropdownlist_demo.aspx (or whatever you want to give the name. No pressure. :P). In Solution Explorer you will get your dropdownlist_demo.aspx and dropdownlist.aspx.cs, both files.
 
Step 3
 
Again you need to get to Add New Item and Select -> SQL Server Database. (You know very well what we must do if they prompt you by asking Would you like to place your Database inside the App_Data_Folder? Say “Yes”, always). You will get your database in the Server Explorer (CTRL + ALT + S).
 
 
Database Chamber
 
Step 4
 
In Server Explorer, click on the arrow sign of your database (“Database.mdf”). Go to tables then right-click and select Add New Table.
  1. Country Table : tbl_country (Don't Forgot – IS INDENTITY = “TRUE”)
     
     
  2. State Table: tbl_State (IS IDENTITY=”TRUE”)
     
     
  3. City Table: tbl_city (IS IDENTITY= “TRUE”)
     
Make “Is Identity True”. Don't forget it. Another thing is the data of the tables that you must enter manually. Just right-click on your tables (Country, State, and City) then select Show Table Data. Here you need to add all the data that will be shown in the dropdown list when we run the project.
 
Design Chamber
 
Step 5
 
Open your dropdownlist.aspx from Solution Explorer and start designing your application.
 
It will look Like this:
 
 
Here is your design code:
  1. <form id="form1" runat="server">  
  2.     <table style="width: 100%; height: 86px;">  
  3.         <tr>  
  4.             <td class="style1">  
  5.                 <asp:Label ID="Label1" runat="server" Text="Choose Your Country :"></asp:Label>  
  6.             </td>  
  7.             <td>  
  8.                 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"   
  9.                     DataTextField="country_name" DataValueField="country_id" AppendDataBoundItems="true"   
  10.                     onselectedindexchanged="DropDownList1_SelectedIndexChanged">  
  11.                     <asp:ListItem Value="0">--Select Country--</asp:ListItem>  
  12.                 </asp:DropDownList>  
  13.             </td>  
  14.             <td>  
  15.                  </td>  
  16.         </tr>  
  17.         <tr>  
  18.             <td class="style1">  
  19.                 <asp:Label ID="Label2" runat="server" Text="Choose Your State :"></asp:Label>  
  20.             </td>  
  21.             <td>  
  22.                 <asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems="true" DataTextField="state_name"   
  23.                     DataValueField="state_id" AutoPostBack="True"   
  24.                     onselectedindexchanged="DropDownList2_SelectedIndexChanged">  
  25.                     <asp:ListItem Value="0">-- Select State--</asp:ListItem>  
  26.                 </asp:DropDownList>  
  27.             </td>  
  28.             <td>  
  29.                  </td>  
  30.         </tr>  
  31.         <tr>  
  32.             <td class="style1">  
  33.                 <asp:Label ID="Label3" runat="server" Text="Choose Your City :"></asp:Label>  
  34.             </td>  
  35.             <td>  
  36.                 <asp:DropDownList ID="DropDownList3" runat="server" AppendDataBoundItems="true" DataTextField="city_name"   
  37.                     DataValueField="city_id">  
  38.                     <asp:ListItem Value="0">-- Select City--</asp:ListItem>  
  39.                 </asp:DropDownList>  
  40.             </td>  
  41.             <td>  
  42.                  </td>  
  43.         </tr>  
  44.     </table>  
  45.     <div>  
  46.    </div>  
  47. </form>  
Coding Chamber
 
Step 6
 
Now we are entering into our Code Zone. Let's begin by adding the following namespaces:
 
 
Here is the code for cascading more than one dropdown list in ASP.Net:
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (!Page.IsPostBack)  
  4.     {  
  5.         SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
  6.         SqlCommand cmd = new SqlCommand("select * from tbl_country", con);  
  7.         SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  8.         DataTable dt = new DataTable();  
  9.         sda.Fill(dt);  
  10.         DropDownList1.DataSource = dt;  
  11.         DropDownList1.DataBind();  
  12.           
  13.     }  
  14.   
  15. }  
  16. protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)  
  17. {  
  18.     DropDownList2.Items.Clear();  
  19.     DropDownList2.Items.Add("Select State");  
  20.   
  21.     SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
  22.     SqlCommand cmd = new SqlCommand("select * from tbl_state where country_id=" + DropDownList1.SelectedItem.Value, con);  
  23.     SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  24.     DataTable dt = new DataTable();  
  25.     sda.Fill(dt);  
  26.     DropDownList2.DataSource= dt;  
  27.     DropDownList2.DataBind();  
  28. }  
  29. protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)  
  30. {  
  31.     DropDownList3.Items.Clear();  
  32.     DropDownList3.Items.Add("Select State");  
  33.   
  34.     SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
  35.     SqlCommand cmd = new SqlCommand("select * from tbl_city where state_id=" + DropDownList2.SelectedItem.Value, con);  
  36.     SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  37.     DataTable dt = new DataTable();  
  38.     sda.Fill(dt);  
  39.   
  40.     DropDownList3.DataSource = dt;  
  41.     DropDownList3.DataBind();  
  42.   
  43. }  
You can get your Connection String by going to your database (in Server Explorer). Right-click Properties and then you can see there “Connection String”. Copy it and paste it into the SQL connection field. Yeah! Surely your connection string is quite different from me, initially, it will look like this:
 
Before
  1. Connection String ("Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\Nilesh\Documents\Visual Studio 2010\WebSites\WebSite13\App_Data\Database.mdf";Integrated Security=True;User Instance=True");  
You need to remove the path and make it short like:
 
C:\Users\Nilesh\Documents\Visual Studio 2010\WebSites\WebSite13\App_Data -- > Remove this
 
And add instead of this |DataDirectory|
 
After
  1. Modified Connection String (@"DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
Output Chamber
 
 
I hope you liked this. Enjoy your day with this tutorial.