DropDownList with country, state and city in ASP. NET

Ivinraj.S
Posted by Ivinraj.S under ASP.NET category on | Points: 40 | Views : 33831
Creating table for country, state and city.

SelectCountry Table
Create Table SelectCountry
(
CountryId Int Primary Key,
County Varchar(30)
)


SelectCountrySelectState Table
Create Table SelectCountrySelectState
(
StateId Int Primary Key,
CountryId Int Foreign Key References SelectCountry(CountryId),
State Varchar(30)
)


SelectStateSelectCity Table
Create Table SelectStateSelectCity
(
CityId Int,
StateId Int Foreign Key References SelectCountrySelectState(StateId),
City Varchar(30)
)


Now insert values in the table.

Insert Into SelectCountry Values(101,'Australia')
Insert Into SelectCountry Values(102,'USA')
Insert Into SelectCountry Values(103,'India')

Insert Into SelectCountrySelectState Values(1001,101,'New South wales')
Insert Into SelectCountrySelectState Values(1002,101,'Victoria')
Insert Into SelectCountrySelectState Values(1003,101,'Queensland')
Insert Into SelectCountrySelectState Values(1003,101,'Western Australia')
Insert Into SelectCountrySelectState Values(1003,101,'South Australia')
Insert Into SelectCountrySelectState Values(2001,102,'california')
Insert Into SelectCountrySelectState Values(2002,102,'Washington, D.C.')
Insert Into SelectCountrySelectState Values(2003,102,'North America')
Insert Into SelectCountrySelectState Values(3001,103,'Tamilnadu')
Insert Into SelectCountrySelectState Values(3002,103,'Kerala')
Insert Into SelectCountrySelectState Values(3003,103,'bangalore')

Insert Into SelectStateSelectCity Values(11,1001,'Canberra')
Insert Into SelectStateSelectCity Values(12,1001,'Sydney')
Insert Into SelectStateSelectCity Values(21,1002,'Melbourne')
Insert Into SelectStateSelectCity Values(22,1002,'Geelong')
Insert Into SelectStateSelectCity Values(31,1003,'Brisbane')
Insert Into SelectStateSelectCity Values(32,1003,'Cairns')
Insert Into SelectStateSelectCity Values(41,2001,'Agoura Hills')
Insert Into SelectStateSelectCity Values(42,2001,'Alhambra')
Insert Into SelectStateSelectCity Values(51,2002,'Accokeek')
Insert Into SelectStateSelectCity Values(52,2002,'Alexandria')
Insert Into SelectStateSelectCity Values(61,2003,'New York City')
Insert Into SelectStateSelectCity Values(62,2003,'Houston')
Insert Into SelectStateSelectCity Values(71,3001,'chennai')
Insert Into SelectStateSelectCity Values(72,3001,'Erode')
Insert Into SelectStateSelectCity Values(81,3002,'Tvm')
Insert Into SelectStateSelectCity Values(82,3002,'Trissur')
Insert Into SelectStateSelectCity Values(91,3003,'Halegannada')
Insert Into SelectStateSelectCity Values(92,3003,'Mysore')
/////


In the web.config file create the connection string in <connectionString> element as:

<connectionStrings>
<add name="connectionstring" connectionString="Data Source=192.168.100.30\sqlserver; DataBase=AshokSample; User Id=sa; Password=IIT;"/>
</connectionStrings>


Design View:

<head runat="server">
<title>country,state,city</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
SelectCountry
</td>
<td>
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged"
Width="100px">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
SelectState
</td>
<td>
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlState_SelectedIndexChanged"
Width="100px">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
SelectCity
</td>
<td>
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true" Width="100px">
</asp:DropDownList>
</td>
</tr>
</table>
</div>
</form>
</body>


Code View:

In the Code behind file (.aspx.cs) write the code as:

First include the following namespaces:

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

//Creating and initializing connection object.

string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dropdownlist();

}
}
#region dropdownlist
private void dropdownlist()
{
SqlConnection con = new SqlConnection(strconn);
con.Open();
SqlCommand cmd = new SqlCommand("select * from SelectCountry",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlCountry.DataSource = ds;
ddlCountry.DataTextField = "County";
ddlCountry.DataValueField = "CountryId";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("--Select--", "0"));
ddlState.Items.Insert(0,new ListItem("--Select--", "0"));
ddlCity.Items.Insert(0,new ListItem("--Select--", "0"));
}
#endregion

#region CountrySelectedIndexChanged
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
int CountryId = Convert.ToInt32(ddlCountry.SelectedValue);
SqlConnection con = new SqlConnection(strconn);
con.Open();
SqlCommand cmd = new SqlCommand("select * from SelectCountrySelectState where CountryId=" + CountryId, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds= new DataSet();
da.Fill(ds);
con.Close();
ddlState.DataSource = ds;
ddlState.DataTextField = "State";
ddlState.DataValueField = "StateId";
ddlState.DataBind();
ddlState.Items.Insert(0, new ListItem("--Select--", "0"));
}
#endregion

#region StateSelectedIndexChanged
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
int StateId = Convert.ToInt32(ddlState.SelectedValue);
SqlConnection con = new SqlConnection(strconn);
con.Open();
SqlCommand cmd = new SqlCommand("select * from SelectStateSelectCity where StateId =" + StateId, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlCity.DataSource = ds;
ddlCity.DataTextField = "City";
ddlCity.DataValueField = "CityId";
ddlCity.DataBind();
ddlCity.Items.Insert(0, new ListItem("--Select--", "0"));

}
#endregion
}

Comments or Responses

Login to post response