Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

I decided to write this sample demo because I always encounter this kind of problem in the ASPNET Forum . So here's a sample (one way) solution on how to populate the second DropDownList based on the value selected on the first DropDownList using the ADO.NET way.

Assuming that DropDownList1 contains the list of countries and we need to populate the list of States in a particular country based on the first DropDownList selection.

protected void PopulateDropDownList1(){
    string queryString = "SELECT * FROM Table1";
    SqlClient.SqlConnection connection = new SqlClient.SqlConnection("Your Connection String here");
    SqlClient.SqlCommand command = new SqlClient.SqlCommand(queryString, connection);
       
    connection.Open();
  
    DataTable dt = new DataTable();
    SqlDataAdapter ad = new SqlDataAdapter(command);
    ad.Fill(dt);
    if (dt.Rows.Count > 0) {

    DropDownList1.DataSource = dt;
    DropDownList1.DataTextField = "CountryName";
    DropDownList1.DataValueField = "CountryName";
    DropDownList1.DataBind();
    }
   
    connection.Close();
  
}
protected void PopulateDropDownList2(string country)
{
   
    string queryString = "SELECT States FROM FROM Table2 WHERE Country = @country";
    SqlClient.SqlConnection connection = new SqlClient.SqlConnection("YOUR CONNECTION STRING HERE");
    SqlClient.SqlCommand command = new SqlClient.SqlCommand(queryString, connection);
    command.Parameters.AddWithValue("@country", country);
    connection.Open();
  
    DataTable dt = new DataTable();
    SqlDataAdapter ad = new SqlDataAdapter(command);
    ad.Fill(dt);

    DropDownList1.Items.Clear();
    if (dt.Rows.Count > 0) {

    DropDownList2.DataSource = dt;
    DropDownList2.DataTextField = "States";
    DropDownList2.DataValueField = "States";
    DropDownList2.DataBind();
    }
    connection.Close();
   
}

protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   
PopulateDropDownList2(DropDownList1.SelectedItem.Value.ToString());
   
}
protected void Page_Load(object sender, System.EventArgs e)
{
   
    if (!Page.IsPostBack) {
       
        PopulateDropDownList1();
       
    }
}

NOTE: Don't forget to add the following Namespaces below for you to make it work

Using System.Data;

Using System.Data.SqlClient;

Also don't forget to set AutoPostBack to TRUE in your first DropDownList to fire up the SelectedIndexChanged event

 

Another Solution:

You can also use Cascading DropDownList .. see below for demo

Cascading DropDownList Sample Demo

That simple!

Technorati Tags: ,,
Posted on Thursday, September 11, 2008 10:56 AM ADO.NET , ASP.NET , C# | Back to top


Comments on this post: Manually Binding DropDownList based on the value selected on the first DropDownList

# re: Manually Binding DropDownList based on the value selected on the first DropDownList
Requesting Gravatar...
Hi Vinz Thanks for this sample code Excellent

but iam getting Problem while doing code like above that

protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{

PopulateDropDownList2(DropDownList1.Value.ToString());

}
iam getting folwoing error and also nothing is selected in second DropDownlist

System.Web.UI.WebControls.DropDownList' does not contain a definition for 'Value'
Best Regards
Left by Waheed on Mar 01, 2009 11:46 PM

# re: Manually Binding DropDownList based on the value selected on the first DropDownList
Requesting Gravatar...
Hi Thanks Vinz for reply ,
it is now working graet effort keep it up.
happy to learn new ways of programming .

Best Reagrds
Left by waheed on Mar 02, 2009 5:12 PM

# re: Manually Binding DropDownList based on the value selected on the first DropDownList
Requesting Gravatar...
iam getting Problem while doing code like above that

protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{

PopulateDropDownList2(DropDownList1.Value.ToString());

}

nothing is selected in second DropDownlist .

please correct this error as soon as possible...its very urgent...
Best Regards
Left by simmi jaiswal on Aug 19, 2010 12:45 PM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net