The requirement is for three Dropdown List Controls, the contents of which are populated from a database table created in SQL Server. Also until we click a value in the first DDL, the second should not be activated and the same case is with the third DDL.

Solution

Step 1

Create a Web Application Project in ASP.NET and create 3 Dropdown Lists in it. The form should look such as shown below.

Dropdown List

OR

Paste the following code into the HTML file of the ASPX Page.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="CascadingDDLDemo.index" %>
  2. <!DOCTYPE html>
  3. <html>
  4. <head runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True" DataTextField="CountryName" DataValueField="CountryID"></asp:DropDownList>
  11. <br />
  12. <br />
  13. <br />
  14. <asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="True" DataTextField="StateName" DataValueField="StateID"></asp:DropDownList>
  15. <br />
  16. <br />
  17. <br />
  18. <asp:DropDownList ID="DropDownList3" runat="server" DataTextField="CityName" DataValueField="CityID"></asp:DropDownList>
  19. </div>
  20. </form>
  21. </body>
  22. </html>

Step 2

Open SQL Server Management Studio and execute the following SQL Queries.

Create Database CascadingDDLDemo.

  1. Create Database CascadingDDLDemo
  2. Use CascadingDDLDemo
  3. Create Table tblCountries(CountryID int Primary Key, CountryName nvarchar(50))
  4. Create Table tblStates(StateID int Primary Key, StateName nvarchar(50), CountryID int FOREIGN KEY REFERENCES tblCountries(CountryID))
  5. Create Table tblCities(CityID int Primary Key, CityName nvarchar(50), StateID int FOREIGN KEY REFERENCES tblStates(StateID))
  6. Insert Into tblCountries Values (1,'USA')
  7. Insert Into tblCountries Values (2,'Europe')
  8. Insert Into tblCountries Values (3,'India')
  9. Insert Into tblStates Values (1,'New York',1)
  10. Insert Into tblStates Values (2,'California',1)
  11. Insert Into tblStates Values (3,'Albania',2)
  12. Insert Into tblStates Values (4,'Austria',2)
  13. Insert Into tblStates Values (5,'Uttar Pradesh',3)
  14. Insert Into tblStates Values (6,'Maharashtra',3)
  15. Insert Into tblCities Values (1,'Bufflo',1)
  16. Insert Into tblCities Values (2,'Yonkers',1)
  17. Insert Into tblCities Values (3,'Richmond',2)
  18. Insert Into tblCities Values (4,'Norwalk',2)
  19. Insert Into tblCities Values (5,'Tirana',3)
  20. Insert Into tblCities Values (6,'Fier',3)
  21. Insert Into tblCities Values (7,'Hard',4)
  22. Insert Into tblCities Values (8,'Enns',4)
  23. Insert Into tblCities Values (9,'Allahbad',5)
  24. Insert Into tblCities Values (10,'Lucknow',5)
  25. Insert Into tblCities Values (11,'Aurangabad',6)
  26. Insert Into tblCities Values (12,'Akola',6)
  27. Create Procedure spGetCountries
  28. As
  29. Begin
  30. Select CountryName,CountryID from tblCountries
  31. End
  32. Create Procedure spGetStates @CountryID int
  33. As
  34. Begin
  35. Select StateName,StateID from tblStates where CountryID = @CountryID
  36. End
  37. Create Procedure spGetCities @StateID int
  38. As
  39. Begin
  40. Select CityName,CityID from tblCities where StateID = @StateID
  41. End

Step 3

Return to Visual Studio and press the F7 key and paste in the following code.

  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Web.UI.WebControls;
  6. namespace CascadingDDLDemo
  7. {
  8. public partial class index : System.Web.UI.Page
  9. {
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12. if (!IsPostBack)
  13. {
  14. DropDownList2.Enabled = false;
  15. DropDownList3.Enabled = false;
  16. DropDownList1.DataSource = getData("spGetCountries", null);
  17. DropDownList1.DataBind();
  18. ListItem LICountry = new ListItem("----Select----", "-1");
  19. DropDownList1.Items.Insert(0, LICountry);
  20. ListItem LIState = new ListItem("----Select----", "-1");
  21. DropDownList2.Items.Insert(0, LIState);
  22. ListItem LICity = new ListItem("----Select----", "-1");
  23. DropDownList3.Items.Insert(0, LICity);
  24. }
  25. }
  26. protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  27. {
  28. if (DropDownList1.SelectedValue == "-1")
  29. {
  30. DropDownList2.SelectedIndex = 0;
  31. DropDownList3.SelectedIndex = 0;
  32. DropDownList2.Enabled = false;
  33. DropDownList3.Enabled = false;
  34. }
  35. else
  36. {
  37. DropDownList2.Enabled = true;
  38. SqlParameter Parameter = new SqlParameter("@CountryID", DropDownList1.SelectedValue);
  39. DropDownList2.DataSource = getData("spGetStates", Parameter);
  40. DropDownList2.DataBind();
  41. ListItem LIState = new ListItem("----Select----", "-1");
  42. DropDownList2.Items.Insert(0, LIState);
  43. DropDownList3.SelectedIndex = 0;
  44. DropDownList3.Enabled = false;
  45. }
  46. }
  47. protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
  48. {
  49. if (DropDownList2.SelectedValue == "-1")
  50. {
  51. DropDownList3.SelectedIndex = 0;
  52. DropDownList3.Enabled = false;
  53. }
  54. else
  55. {
  56. DropDownList3.Enabled = true;
  57. SqlParameter Parameter = new SqlParameter("@StateID", DropDownList2.SelectedValue);
  58. DropDownList3.DataSource = getData("spGetCities", Parameter);
  59. DropDownList3.DataBind();
  60. ListItem LICity = new ListItem("----Select----", "-1");
  61. DropDownList3.Items.Insert(0, LICity);
  62. }
  63. }
  64. private DataSet getData(string Proc, SqlParameter Parameter)
  65. {
  66. string CS = ConfigurationManager.ConnectionStrings["CS"].ConnectionString;
  67. using (SqlConnection con = new SqlConnection(CS))
  68. {
  69. con.Open();
  70. SqlDataAdapter DA = new SqlDataAdapter(Proc, con);
  71. DA.SelectCommand.CommandType = CommandType.StoredProcedure;
  72. if (Parameter != null)
  73. {
  74. DA.SelectCommand.Parameters.Add(Parameter);
  75. }
  76. DataSet DS = new DataSet();
  77. DA.Fill(DS);
  78. return DS;
  79. }
  80. }
  81. }
  82. }

Step 4

Go the Solution Explorer in the project and double-click on the web.config file and paste in the following code.

  1. <?xml version="1.0"?>
  2. <configuration>
  3. <system.web>
  4. <compilation debug="true" targetFramework="4.5" />
  5. <httpRuntime targetFramework="4.5" />
  6. </system.web>
  7. <connectionStrings>
  8. <add name="CS" providerName="System.Data.SqlClient" connectionString="Data Source=(local);Initial Catalog=CascadingDDLDemo;Integrated Security=True"/>
  9. </connectionStrings>
  10. </configuration>

Step 5

Press F5 to run the application. Remember to name the project CascadingDDLDemo if you are running exactly the same preceding code. After running the application, it should look as in the following screenshot.

screenshot

Explanation of the Preceding Code

Explanation of Step 1

We added 3 Dropdown List controls from the toolbox. The main focus is on the first and the second Dropdown List because when the data is selected in them, that data should be posted back to the server and on the basis of which the values will be populated in the next Dropdown List control. So we set the AutoPostBack property to “true” of these two controls.

Explanation of Step 2

Explanation of Step 3

First, we will create a private method that fetches the data through the database on the basis of the two arguments the Stored Procedure we created in SQL and the parameters they accepts. We have named this method getData().

Explanation of Step 4

We have just added the connection string to the Web.config file of the application.

Please comment in case of any queries.

I hope you like the preceding article.