Binding one DropDownList to another DropDownList

Posted by Fundaravi under ASP.NET on 5/11/2012 | Points: 10 | Views : 19507 | Status : [Member] | Replies : 3
I have 3 DropDownlists. one is for country and second one is for state and third one is for city. country,state,city are the 3 tables in database. if i select country dropdownlist second dropdownlist with state values , if i select state dropdonwlist have to fill the city dropdownlist.. Give me with code in asp.net
Binding one DropDownList to another DropDownList

Ravindra


Responses

Posted by: Sakthi.Singaravel on: 5/11/2012 [Member] Silver | Points: 25

Up
0
Down
DataSet DS = new DataSet();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlCommand Cmd = new SqlCommand("Select * From tblcountry", Conn);
SqlDataAdapter DA = new SqlDataAdapter(Cmd);
DA.Fill(DS, "country");
DropDownList1.DataSource = DS.Tables["country"];
DropDownList1.DataTextField = "countryname";
DropDownList1.DataValueField = "countryid";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "Select");
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue != "Select")
{
SqlCommand Cmd1 = new SqlCommand("Select * From tblstate Where countryid=" + DropDownList1.SelectedValue + "", Conn);
SqlDataAdapter DA1 = new SqlDataAdapter(Cmd1);
DA1.Fill(DS, "state");
DropDownList2.DataSource = DS.Tables["state"];
DropDownList2.DataTextField = "statename";
DropDownList2.DataValueField = "stateid";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, "Select");
}
}protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList2.SelectedValue != "Select")
{
SqlCommand Cmd1 = new SqlCommand("Select * From tblcity Where stateid=" + DropDownList2.SelectedValue + "", Conn);
SqlDataAdapter DA1 = new SqlDataAdapter(Cmd1);
DA1.Fill(DS, "city");
DropDownList3.DataSource = DS.Tables["city"];
DropDownList3.DataTextField = "cityname";
DropDownList3.DataValueField = "cityid";
DropDownList3.DataBind();
DropDownList3.Items.Insert(0, "Select");
}
}


Regards,
Singaravel M

Fundaravi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Amit.Jain on: 5/11/2012 [Member] Starter | Points: 25

Up
0
Down
You can either use Ajax CascadingDropDownlist
http://www.csharpaspnetarticles.com/2009/02/ajax-cascading-dropdownlist-database.html

or Use code to create cascading dropdowns
http://www.csharpaspnetarticles.com/2009/03/populate-dropdown-based-selection-other.html

Fundaravi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sriramnandha on: 5/23/2012 [Member] Starter | Points: 25

Up
0
Down
TABLE CREATION IN SQL SERVER:

CREATE TABLE COUNTRY(COUNTRYID INT,COUNTRYNAME VARCHAR(90))


CREATE TABLE STATEDETAILS(STATEID INT ,COUNTRYID INT,STATENAME VARCHAR(90))

CREATE TABLE CITY(CITYID INT,STATEID INT ,CITYNAME VARCHAR(90))


ASPX:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CountrySateCity.WebForm1" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataTextField="countryname" DataValueField="countryname" Height="49px"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="266px">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
DataTextField="statename" DataValueField="statename" Height="20px"
Width="255px">
</asp:DropDownList>

</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>


ASPX.CS

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace CountrySateCity
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
loadcountry();
}
}


public void loadcountry()
{
string con="Server=SRIRAM-BF522A69;UID=sa;PWD=asdfgf;DataBase=Test";
SqlConnection con_country = new SqlConnection(con);
string country = "select countryname from countrylist";
SqlDataAdapter adp_con = new SqlDataAdapter(country, con);
DataSet ds_con = new DataSet();
adp_con.Fill(ds_con);
DropDownList1.DataSource = ds_con;
DropDownList1.DataBind();

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string string_state ="CONNECTIONSTRING";
SqlConnection con_state = new SqlConnection(string_state);
string state_query = "select statename from statelist inner join countrylist on statelist.countryid=countrylist.countryid where countryname='" + DropDownList1.SelectedItem.Text + "'";
con_state.Open();
SqlDataAdapter adp_state = new SqlDataAdapter(state_query, con_state);
DataSet ds_state = new DataSet();
adp_state.Fill(ds_state);
DropDownList2.DataSource = ds_state;
DropDownList2.DataBind();

}
}
}



REGARDS


sriram

Fundaravi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response