hi friends iam new this topic
iam using asp.net aspx page iam desiging 3 dropdowns.
first for countries, second for states, third for districts at run time iam selecting country from dropdownlist now i want display that country corresponding
states only in states dropdownlist after that iam selecting state from dropdown i want to display that state corresponding districts only in districts dropdown list.
please help meeeeeeeeeee friends...
please......
Loading
Satyapriya NayakPosted Jan 4, 2013, 4:34 AM
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Country_state_city
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindcountry();
}
}
protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{
bindstate();
}
protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
{
bindcity();
}
public void bindcountry()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select CountryId,CountryName from Country";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "Country");
ddlcountry.Items.Clear();
ddlcountry.Items.Add("--Select--");
ddlcountry.DataValueField = "CountryId";
ddlcountry.DataTextField = "CountryName";
ddlcountry.DataSource = ds;
ddlcountry.DataMember = "Country";
ddlcountry.DataBind();
con.Close();
}
public void bindstate()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select StateId,StateName from State where Country_Id='" + ddlcountry.SelectedValue + "'";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "State");
ddlstate.Items.Clear();
ddlstate.Items.Add("--Select--");
ddlstate.DataValueField = "StateId";
ddlstate.DataTextField = "StateName";
ddlstate.DataSource = ds;
ddlstate.DataMember = "State";
ddlstate.DataBind();
con.Close();
}
public void bindcity()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select CityId,CityName from City where State_Id ='" + ddlstate.SelectedValue + "'";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "City");
ddlcity.Items.Clear();
ddlcity.Items.Add("--Select--");
ddlcity.DataValueField = "CityId";
ddlcity.DataTextField = "CityName";
ddlcity.DataSource = ds;
ddlcity.DataMember = "City";
ddlcity.DataBind();
con.Close();
}
}
}
Thanks
If this post helps you mark it as answer
Dorababu MekaPosted Jan 4, 2013, 3:34 AM
http://www.aspsnippets.com/Articles/Cascading-DropDownList-for-CountryStateCity-in-ASPNet.aspx
Neha SharmaPosted Jan 4, 2013, 2:43 AM
Don't be panic,it's so easy you just need to concentrate on your table structure it ll be like:
for country :
CId
Cname
For state:
Sid
Sname
CId
here you can define CId as foreign key
For city
CIId
CIname
Sid
here you can define Sid as foreign key.
Now please let me know did you get what i mean to explain here.
hope things are now clear.
arjunPosted Jan 4, 2013, 1:44 AM
1.load countries first from DB
2.go to countries dropdown selectedIndexChanged event
3.in that get the county name
4.pass that name to DB SELECT command
select state from countryMaste where country='"+dropdown1.selectedtext+"'
5.store states in a datatable
6.then bind the datatable to state dropdown
7.u ll get result