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.
- Create Database DDLDemo
- Use DDLDemo
- Create Table Country(ID int Identity Primary Key, Name nvarchar(50))
- Create Table [State](ID int Identity Primary Key, Name nvarchar(50), CountryID int FOREIGN KEY References Country(ID))
- Insert Into Country Values ('India')
- Insert Into Country Values ('USA')
- Insert Into Country Values ('China')
- Insert Into Country Values ('Australia')
- Insert Into Country Values ('United Kingdom')
- Select * from Country
- Select * from State
- Insert Into State Values ('Delhi',1)
- Insert Into State Values ('UP',1)
- Insert Into State Values ('Kanpur',1)
- Insert Into State Values ('Lucknow',1)
- Insert Into State Values ('New York',2)
- Insert Into State Values ('Boston',2)
- Insert Into State Values ('Chile',2)
- Insert Into State Values ('Japan',3)
- Insert Into State Values ('Tokyo',3)
- Insert Into State Values ('Shinghai',3)
- Insert Into State Values ('Vietnam',3)
- Insert Into State Values ('Delhi',4)
- Insert Into State Values ('Sydney',4)
- Insert Into State Values ('Malbourne',4)
- Insert Into State Values ('England',5)
- Insert Into State Values ('London',5)
- Insert Into State Values ('Singapore',5)
- --This is a query using Inner join which shows all the city for their respective countries.
- Select c.Name as Country, s.Name as State from Country c INNER JOIN State s ON c.ID = s.CountryID
- Add a new Webform to the Web Application.
- Type the following code in the HTML File.
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <link href="Content/bootstrap.min.css" rel="stylesheet" />
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <table class="table text-capitalize text-center text-danger" style="font-size: larger">
- <thead>
- <tr>
- <td>Country</td>
- <td>State</td>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td>
- <%--Please put Autopostback Property true--%>
- <asp:DropDownList ID="ddl_Country" runat="server" AutoPostBack="True"
- OnSelectedIndexChanged="ddl_Country_SelectedIndexChanged"></asp:DropDownList>
- </td>
- <td>
- <asp:DropDownList ID="ddl_State" runat="server"></asp:DropDownList>
- </td>
- </tr>
- </tbody>
- </table>
- </div>
- </form>
- </body>
- </html>
- Type the following code in the Code behind file by Pressing F7 Key.
- using System;
- using System.Data;
- using System.Data.SqlClient;
- namespace DDLPopulateDemo
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- string CS = "Data Source=(local);Initial Catalog=DDLDemo;Integrated Security=True"; //Creating a String to Connect to Database
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack) //Used to Check that Whether the page loads first time or Second Time
- {
- using (SqlConnection con = new SqlConnection(CS)) //Created Connection to Connect to Database
- {
- con.Open(); //Opened the Connection
- SqlCommand cmd = new SqlCommand("Select * from Country", con); //Fetching all the Data using SQL Query through Country
- Table
- SqlDataAdapter da = new SqlDataAdapter(cmd); // Initializing a Data Adapter to Store the data
- DataSet ds = new DataSet(); //Created a Dataset
- da.Fill(ds); //Filled the Dataset with Values contained in DataAdapter
- ddl_Country.DataSource = ds; //Setting the Datasource of DDL to Dataset
- ddl_Country.DataTextField = "Name"; //This is the name of the column of Database through which we want to Show the
- Values
- ddl_Country.DataValueField = "ID"; //This is the Primary key on which the Data is fetched
- ddl_Country.DataBind(); //Binding the Fetched Data to the DDL
- }
- }
- }
- protected void ddl_Country_SelectedIndexChanged(object sender, EventArgs e) //When the Selected index is changed of the DDL, It
- postbacks the Data and This event is fired
- {
- using (SqlConnection con = new SqlConnection(CS))
- {
- con.Open();
- //Created a custom Query which Selects data from the State Table on the Basis of the Foreign key which was Country ID
- SqlCommand cmd = new SqlCommand("Select * from State where CountryID = '" + (Convert.ToInt32(ddl_Country.SelectedValue)) +
- "'", con);
- //Rest of the Code Remains same as Page_Load method
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- da.Fill(ds);
- ddl_State.DataSource = ds;
- ddl_State.DataTextField = "Name";
- ddl_State.DataValueField = "ID";
- ddl_State.DataBind();
- }
- }
- }
- }

Join the conversation! Your thoughts help the community grow.