Aim

We want to Select the Name of the Country from the Dropdownlist and On Selecting a Particular Country, A list of State is populated in the second Dropdownlist control from SQL Database.

Step 1: Creating the Database Structure.

  1. Create Database DDLDemo
  2. Use DDLDemo
  3. Create Table Country(ID int Identity Primary Key, Name nvarchar(50))
  4. Create Table [State](ID int Identity Primary Key, Name nvarchar(50), CountryID int FOREIGN KEY References Country(ID))
  5. Insert Into Country Values ('India')
  6. Insert Into Country Values ('USA')
  7. Insert Into Country Values ('China')
  8. Insert Into Country Values ('Australia')
  9. Insert Into Country Values ('United Kingdom')
  10. Select * from Country
  11. Select * from State
  12. Insert Into State Values ('Delhi',1)
  13. Insert Into State Values ('UP',1)
  14. Insert Into State Values ('Kanpur',1)
  15. Insert Into State Values ('Lucknow',1)
  16. Insert Into State Values ('New York',2)
  17. Insert Into State Values ('Boston',2)
  18. Insert Into State Values ('Chile',2)
  19. Insert Into State Values ('Japan',3)
  20. Insert Into State Values ('Tokyo',3)
  21. Insert Into State Values ('Shinghai',3)
  22. Insert Into State Values ('Vietnam',3)
  23. Insert Into State Values ('Delhi',4)
  24. Insert Into State Values ('Sydney',4)
  25. Insert Into State Values ('Malbourne',4)
  26. Insert Into State Values ('England',5)
  27. Insert Into State Values ('London',5)
  28. Insert Into State Values ('Singapore',5)
  29. --This is a query using Inner join which shows all the city for their respective countries.
  30. Select c.Name as Country, s.Name as State from Country c INNER JOIN State s ON c.ID = s.CountryID
Step 2: Creating the Web Application:

  1. Add a new Webform to the Web Application.
  2. Type the following code in the HTML File.

    1. <!DOCTYPE html>
    2. <html xmlns="http://www.w3.org/1999/xhtml">
    3. <head runat="server">
    4. <title></title>
    5. <link href="Content/bootstrap.min.css" rel="stylesheet" />
    6. </head>
    7. <body>
    8. <form id="form1" runat="server">
    9. <div>
    10. <table class="table text-capitalize text-center text-danger" style="font-size: larger">
    11. <thead>
    12. <tr>
    13. <td>Country</td>
    14. <td>State</td>
    15. </tr>
    16. </thead>
    17. <tbody>
    18. <tr>
    19. <td>
    20. <%--Please put Autopostback Property true--%>
    21. <asp:DropDownList ID="ddl_Country" runat="server" AutoPostBack="True"
    22. OnSelectedIndexChanged="ddl_Country_SelectedIndexChanged"></asp:DropDownList>
    23. </td>
    24. <td>
    25. <asp:DropDownList ID="ddl_State" runat="server"></asp:DropDownList>
    26. </td>
    27. </tr>
    28. </tbody>
    29. </table>
    30. </div>
    31. </form>
    32. </body>
    33. </html>

  3. Type the following code in the Code behind file by Pressing F7 Key.
    1. using System;
    2. using System.Data;
    3. using System.Data.SqlClient;
    4. namespace DDLPopulateDemo
    5. {
    6. public partial class WebForm1 : System.Web.UI.Page
    7. {
    8. string CS = "Data Source=(local);Initial Catalog=DDLDemo;Integrated Security=True"; //Creating a String to Connect to Database
    9. protected void Page_Load(object sender, EventArgs e)
    10. {
    11. if (!IsPostBack) //Used to Check that Whether the page loads first time or Second Time
    12. {
    13. using (SqlConnection con = new SqlConnection(CS)) //Created Connection to Connect to Database
    14. {
    15. con.Open(); //Opened the Connection
    16. SqlCommand cmd = new SqlCommand("Select * from Country", con); //Fetching all the Data using SQL Query through Country
    17. Table
    18. SqlDataAdapter da = new SqlDataAdapter(cmd); // Initializing a Data Adapter to Store the data
    19. DataSet ds = new DataSet(); //Created a Dataset
    20. da.Fill(ds); //Filled the Dataset with Values contained in DataAdapter
    21. ddl_Country.DataSource = ds; //Setting the Datasource of DDL to Dataset
    22. ddl_Country.DataTextField = "Name"; //This is the name of the column of Database through which we want to Show the
    23. Values
    24. ddl_Country.DataValueField = "ID"; //This is the Primary key on which the Data is fetched
    25. ddl_Country.DataBind(); //Binding the Fetched Data to the DDL
    26. }
    27. }
    28. }
    29. protected void ddl_Country_SelectedIndexChanged(object sender, EventArgs e) //When the Selected index is changed of the DDL, It
    30. postbacks the Data and This event is fired
    31. {
    32. using (SqlConnection con = new SqlConnection(CS))
    33. {
    34. con.Open();
    35. //Created a custom Query which Selects data from the State Table on the Basis of the Foreign key which was Country ID
    36. SqlCommand cmd = new SqlCommand("Select * from State where CountryID = '" + (Convert.ToInt32(ddl_Country.SelectedValue)) +
    37. "'", con);
    38. //Rest of the Code Remains same as Page_Load method
    39. SqlDataAdapter da = new SqlDataAdapter(cmd);
    40. DataSet ds = new DataSet();
    41. da.Fill(ds);
    42. ddl_State.DataSource = ds;
    43. ddl_State.DataTextField = "Name";
    44. ddl_State.DataValueField = "ID";
    45. ddl_State.DataBind();
    46. }
    47. }
    48. }
    49. }