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)
-
-
-
- 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: - 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";
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- using (SqlConnection con = new SqlConnection(CS))
- {
- con.Open();
- SqlCommand cmd = new SqlCommand("Select * from Country", con);
-
- Table
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- da.Fill(ds);
- ddl_Country.DataSource = ds;
- ddl_Country.DataTextField = "Name";
-
- Values
- ddl_Country.DataValueField = "ID";
- ddl_Country.DataBind();
- }
- }
- }
- protected void ddl_Country_SelectedIndexChanged(object sender, EventArgs e)
-
- postbacks the Data and This event is fired
- {
- using (SqlConnection con = new SqlConnection(CS))
- {
- con.Open();
-
- SqlCommand cmd = new SqlCommand("Select * from State where CountryID = '" + (Convert.ToInt32(ddl_Country.SelectedValue)) +
-
- "'", con);
-
- 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();
- }
- }
- }
- }