Coding
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;
public partial class list : System.Web.UI.Page
{
Class1 cls = new Class1();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
sel();
}
}
private void sel()
{
DataSet ds = new DataSet();
ds = cls.getds("select State,StateId from State");
ListItem li = new ListItem();
li.Value = "0";
li.Text = "-Select-";
DropDownList1.Items.Add(li.ToString());
foreach (DataRow dr in ds.Tables[0].Rows)
{
ListItem li1 = new ListItem();
li1.Text = dr["State"].ToString();
DropDownList1.Items.Add(li1.ToString());
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Items.Clear();
DataSet ds = new DataSet();
ds = cls.getds("select City,CityId from City where StateId='" + DropDownList1.SelectedIndex.ToString() + "'");
ListItem li3 = new ListItem();
li3.Value = "0";
DropDownList2.Items.Add(li3.ToString());
foreach (DataRow dr in ds.Tables[0].Rows)
{
ListItem li2 = new ListItem();
li2.Value = dr["CityId"].ToString();
li2.Text = dr["City"].ToString();
DropDownList2.Items.Add(li2.ToString());
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList3.Items.Clear();
DataSet ds = new DataSet();
ds = cls.getds("select Location,LocationId from Location where CityId='" + DropDownList2.SelectedIndex.ToString() + "'");
ListItem li7 = new ListItem();
li7.Text = "-select";
li7.Value = "0";
DropDownList3.Items.Add(li7.ToString());
foreach (DataRow dr in ds.Tables[0].Rows)
{
ListItem li8 = new ListItem();
li8.Value = dr["LocationId "].ToString();
li8.Text = dr["Location"].ToString();
DropDownList3.Items.Add(li8.ToString());
}
}
}


Satyapriya NayakRank #47 · 31.5K reputationPosted Apr 19, 2012, 6:01 AM EDT
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Country_state_city._Default" %>
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
selvi subramanianRank #2,007 · 723 reputationPosted Apr 19, 2012, 12:03 AM EDT
SenthilkumarRank #387 · 6.5K reputationPosted Apr 19, 2012, 12:06 AM EDT
There are problem in the binding drop down list1.
li1.Value= dr["StateId"].ToString();
In the index change event,
Try this.
selvi subramanianRank #2,007 · 723 reputationPosted Apr 19, 2012, 12:09 AM EDT
selvi subramanianRank #2,007 · 723 reputationPosted Apr 19, 2012, 12:10 AM EDT
It shows like this
selvi subramanianRank #2,007 · 723 reputationPosted Apr 19, 2012, 12:19 AM EDT
if i change the codings u mentioned above the output come like this
SenthilkumarRank #387 · 6.5K reputationPosted Apr 19, 2012, 12:42 AM EDT
Can you check this line
ds = cls.getds("select City,CityId from City where StateId='" + DropDownList1.SelectedIndex.ToString() + "'");
If stateid is int column in sql server then
ds = cls.getds("select City,CityId from City where StateId=" + DropDownList1. SelectedItem.Value.ToString());
Try this...
selvi subramanianRank #2,007 · 723 reputationPosted Apr 19, 2012, 2:45 AM EDT
selvi subramanianRank #2,007 · 723 reputationPosted Apr 19, 2012, 4:09 AM EDT
SenthilkumarRank #387 · 6.5K reputationPosted Apr 19, 2012, 8:53 PM EDT
Database Design:
In code file:
selvi subramanianRank #2,007 · 723 reputationPosted Apr 19, 2012, 9:49 PM EDT
Kunal VaishyaRank #990 · 2K reputationPosted Apr 19, 2012, 10:22 PM EDT
ddl2.Items.Add(New ListItem(dr["Location"].ToString(), dr["LocationId "].ToString()))