Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to implement cascaded DropDownList in asp.net

In this article, I will explain you how we can bind multiple dependent DropDownList in asp.net from DataBase.

Data Source:

First, we need to create 3 different tables: tblCountry, tblState , tblCity. And insert related values into tables.

create table tblCountry
(
CountryId int ,
CountryName varchar(20)
)
insert into tblCountry values(1, class='string'>'India')
insert into tblCountry values(2, class='string'>'USA')

create table tblState
(
StateId int,
StateName varchar(20),
CountryId int
)
insert into tblState values(1, class='string'>'Maharashtra',1)
insert into tblState values(2, class='string'>'Gujarat',1)
insert into tblState values(3, class='string'>'NewYork',2)
insert into tblState values(4, class='string'>'California',2)

create table tblCity
(
CityId int,
CityName varchar(20),
StateId int,
)
insert into tblCity values(1, class='string'>'Mumbai',1)
insert into tblCity values(2, class='string'>'Pune',1)
insert into tblCity values(3, class='string'>'Ahmedabad',2)
insert into tblCity values(4, class='string'>'Surat',2)
insert into tblCity values(5, class='string'>'New York City',3)
insert into tblCity values(6, class='string'>'Rochester',3)
insert into tblCity values(7, class='string'>'Los Angeles',4)
insert into tblCity values(8, class='string'>'San Diego',4)
In web.config file:

Write your connection string in web.config within tag.



add name="MyConnString" connectionString= class='string'>"server=XXXX;database=..;uid=..;password=..;"
providerName="System.Data.SqlClient"/>

In designer file:

In designer file, create 3 DropDownList control named as ddlCountry , ddlState, ddlCity. On select of country DropDown, state related to that country will get bind. On select of state DropDown, city related to that state will get bind. And there is a clear Button to clear the state and city Dropdowns.


DropDownList ID="ddlCountry" runat="server" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged"
AutoPostBack="true">DropDownList>
   
DropDownList ID="ddlState" runat="server" OnSelectedIndexChanged="ddlState_SelectedIndexChanged"
AutoPostBack="true">DropDownList>
   
DropDownList ID="ddlCity" runat="server">DropDownList>
   
Button id="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click">Button>


In Code-Behind file:
Namespace used

Include the below Namespaces.


using System.Configuration;
using System.Data;
using System.Data.SqlClient;

Complete Code

string MyConnString = ConfigurationManager.ConnectionStrings[            class='string'>"MyConnString"].ConnectionString;
//retrieving connection string from web.config file
SqlConnection con = null;
SqlCommand cmd = null;

protected void Page_Load( class='kwrd'>object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCountry();
}

}

private void BindCountry()
{
con = new SqlConnection(MyConnString);
cmd = new SqlCommand( class='string'>"select CountryId,CountryName from tblCountry", con);
cmd.CommandType = CommandType. class='type'>Text;
con.Open();
SqlDataAdapter sda = new class='type'>SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
ddlCountry.DataSource = ds;
ddlCountry.DataTextField = "CountryName";
ddlCountry.DataValueField = "CountryId";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem( class='string'>"--select--"));
}

protected void ddlCountry_SelectedIndexChanged( class='kwrd'>object sender, EventArgs e)
{
ddlState.Items.Clear();
ddlCity.Items.Clear();
if (ddlCountry.SelectedIndex != 0)
{
con = new SqlConnection(MyConnString);
cmd = new SqlCommand( class='string'>"select StateId,StateName from tblState where CountryId="
+ ddlCountry.SelectedItem.Value + class='string'>"", con);
cmd.CommandType = CommandType. class='type'>Text;
con.Open();
SqlDataAdapter sda = new class='type'>SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
ddlState.DataSource = ds;
ddlState.DataTextField = "StateName";
ddlState.DataValueField = "StateId";
ddlState.DataBind();
ddlState.Items.Insert(0, new ListItem( class='string'>"--select--"));
}
}

protected void ddlState_SelectedIndexChanged( class='kwrd'>object sender, EventArgs e)
{
ddlCity.Items.Clear();
if (ddlState.SelectedIndex != 0)
{
con = new SqlConnection(MyConnString);
cmd = new SqlCommand( class='string'>"select CityId,CityName from tblCity where StateId="
+ ddlState.SelectedItem.Value + class='string'>"", con);
cmd.CommandType = CommandType. class='type'>Text;
con.Open();
SqlDataAdapter sda = new class='type'>SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
ddlCity.DataSource = ds;
ddlCity.DataTextField = "CityName";
ddlCity.DataValueField = "CityId";
ddlCity.DataBind();
ddlCity.Items.Insert(0, new ListItem( class='string'>"--select--"));
}

}

protected void btnClear_Click( class='kwrd'>object sender, EventArgs e)
{
ddlState.Items.Clear();
ddlCity.Items.Clear();
ddlCountry.SelectedIndex = 0;

}
Output:
Output will be shown as below.


This post first appeared on ASPArticles, please read the originial post: here

Share the post

How to implement cascaded DropDownList in asp.net

×

Subscribe to Asparticles

Get updates delivered right to your inbox!

Thank you for your subscription

×