I will explain step-by-step Cascading Dropdown lists in the following.

1: SQL Database

In this database section create the two tables, State and City.

Table 1: State

  1. Create table India_State
  2. (
  3. S_id int identity(1,1) primary key,
  4. S_name varchar(30)
  5. )
Table 2: City
  1. create table City
  2. (
  3. City_id int identity(1,1) primary key,
  4. City_name varchar(30),
  5. S_id int foreign key references India_state(S_id)
  6. )

2: Visual Studio

In this section add a UI Page, BAL class file and DAL class file.

Step 1

Create a DAL file as in the following.

Go to the project in the Solution Explorer and add a DAL class file as in Figure 1.

Add DAL class File
Figure 1: Add DAL class File.

DAL Code

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Configuration;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. namespace EWD.DAL
  9. {
  10. public class DAL_DropDownList
  11. {
  12. public static DataTable StateList()
  13. {
  14. string connection = ConfigurationManager.ConnectionStrings["Conncet_DB"].ConnectionString;
  15. // bool status = false;
  16. SqlConnection con = new SqlConnection(connection);
  17. SqlCommand cmd = new SqlCommand("select S_id,S_name from India_State", con);
  18. con.Open();
  19. cmd.CommandType = CommandType.Text;
  20. SqlDataReader dr = cmd.ExecuteReader();
  21. DataTable dt = null;
  22. if (dr.HasRows)
  23. {
  24. dt = new DataTable("India_State");
  25. dt.Load(dr);
  26. // status = true;
  27. return dt;
  28. }
  29. if (cmd != null)
  30. {
  31. cmd.Dispose();
  32. cmd = null;
  33. }
  34. return dt;
  35. }
  36. public static DataTable GetCityList(int S_id)
  37. {
  38. string connection = ConfigurationManager.ConnectionStrings["Conncet_DB"].ConnectionString;
  39. // bool status = false;
  40. SqlConnection con = new SqlConnection(connection);
  41. SqlCommand cmd = new SqlCommand("select City_id,City_name from City Where S_id=@S_id", con);
  42. con.Open();
  43. cmd.CommandType = CommandType.Text;
  44. cmd.Parameters.AddWithValue("@S_id", SqlDbType.Int).Value = S_id;
  45. SqlDataReader dr = cmd.ExecuteReader();
  46. DataTable dt = null;
  47. if (dr.HasRows)
  48. {
  49. dt = new DataTable("City");
  50. dt.Load(dr);
  51. // status = true;
  52. return dt;
  53. }
  54. if (cmd != null)
  55. {
  56. cmd.Dispose();
  57. cmd = null;
  58. }
  59. return dt;
  60. }
  61. }
  62. }

Step 2

Add a BAL File as in Figure 2.

Add BAL Class
Figure 2: Add BAL Class

BAL Code

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data;
  6. using EWD;
  7. namespace EWD.BAL
  8. {
  9. public class BAL_DropDownList
  10. {
  11. public static DataTable GetStateList()
  12. {
  13. return DAL.DAL_DropDownList.StateList();
  14. }
  15. public static DataTable GetCityList(int S_id)
  16. {
  17. return DAL.DAL_DropDownList.GetCityList(S_id);
  18. }
  19. }
  20. }

Step 3

Create the UI Design.

Add Design Page
Figure 3: Add Design Page,

UI Design Code

In this section add a dropdown list to bind a State and City record from the database.

Also the State dropdownlist AutoPostBack event true bind for Child City Record.

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="DropDownList.aspx.cs" Inherits="EWD.UI.DropDownList" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
  3. </asp:Content>
  4. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  5. <h2>Cascading DropDownList in three tier Application </h2>
  6. <table>
  7. <tr>
  8. <td>Select State</td><td><asp:DropDownList ID="ddlState" runat="server"
  9. onselectedindexchanged="ddlState_SelectedIndexChanged" AutoPostBack="true" ></asp:DropDownList></td>
  10. <td>Select City</td><td><asp:DropDownList ID="ddlCity" runat="server"></asp:DropDownList></td>
  11. </tr>
  12. </table>
  13. </asp:Content>
UI Code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using EWD.BAL;
  9. namespace EWD.UI
  10. {
  11. public partial class DropDownList : System.Web.UI.Page
  12. {
  13. protected void Page_Load(object sender, EventArgs e)
  14. {
  15. if (!IsPostBack)
  16. {
  17. BindState();
  18. }
  19. }
  20. public void BindState()
  21. {
  22. BAL_DropDownList DDL = new BAL_DropDownList();
  23. DataTable dtState = EWD.BAL.BAL_DropDownList.GetStateList();
  24. ddlState.DataSource = dtState;
  25. ddlState.DataTextField = "S_name";
  26. ddlState.DataValueField = "S_id";
  27. ddlState.DataBind();
  28. ddlState.Items.Insert(0, new ListItem("---Select State---"));
  29. }
  30. protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
  31. {
  32. BindCity();
  33. }
  34. public void BindCity()
  35. {
  36. int S_id;
  37. int.TryParse(ddlState.SelectedValue, out S_id);
  38. DataTable dtCity = EWD.BAL.BAL_DropDownList.GetCityList(S_id);
  39. ddlCity.DataSource = dtCity;
  40. ddlCity.DataTextField = "City_name";
  41. ddlCity.DataValueField = "City_id";
  42. ddlCity.DataBind();
  43. ddlCity.Items.Insert(0, new ListItem("---Select City---"));
  44. }
  45. }
  46. }
Output

Now run the code and display the results in the web page.

Parent DropDownList
Figure 4: Parent DropDownList

Child DropDownList
Figure 5: Child DropDownList

Thanks for reading this article. Have a nice day sir.