Filtering records from 1st combobox to 2nd combobox and 2nd to 3rd combobox
i have 3 forms in my C# application. in 1st form "country combo". 2nd form "state combo" in 3rd form "district combo". i want to filter record from 1st combo to 2nd and 2nd to 3rd combo. like i select country "india" in 1st combo. then in 2nd combo it's corresponding "state" should display. after selecting state in 3rd combo it's corresponding "district" is display
Sam HobbsPosted Apr 19, 2012, 9:26 PM
Note that this is often called cascading, as in cascading comboboxes. Do you use TableAdapters? I posted a sample that uses TableAdapters in a thread a few months ago. Otherwise there are articles in this web site that decribe other ways to do it.
Satyapriya NayakPosted Apr 19, 2012, 9:39 AM
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
Dorababu MekaPosted Apr 19, 2012, 8:21 AM
Do the same for dropdown2 and get districts
Dorababu MekaPosted Apr 19, 2012, 7:51 AM
In first drop down do this
select StateName from tablename where countryID='"+ddl1.selectedvalue+"'
you will get the required state names fill the to a dataset and bind to ddl2, like that for ddl3 too
This will work if you are maintaining a relation between each table
Kunal VaishyaPosted Apr 19, 2012, 7:45 AM
First Set
i Amuse that
Combo1 Country
Combo2 State
Combo3 City
TableCity
CID
CNAme
SID
TableState
SID
SName
CNId
TableCountry
CNID
CNNAme
First When you select the combo value of COuntry the on Selected value Change you can filter State and combo value of State the on Selected value Change you can filter City
you can also youse selected index changed event to do like this
Use
Arvind YadavPosted Apr 19, 2012, 7:29 AM
Dorababu MekaPosted Apr 19, 2012, 7:18 AM