Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server
How to bind or populate data into a Dropdown List based on first or another Dropdown List in Asp.net C#
=======================================================================
 
Dear Experts !!

am working on asp.net,C#,Sqlserver2005

I have Two Dropdownlists.... Dropdownlist1 and Dropdownlist 2

In first dropdownlist it shows list of companies..... in second dropdownlist it shows Departments.

These two will comes from database.

Suppose when the user select the company from 1st dropdownlist so,in the other dropdownlist it should show related departments to that company.

Please help me how to do this.

Thanks.
Posted 9-Jun-12 1:53am
Comments
Nandakishorerao at 11-Dec-12 0:49am
   
what have you tried ? post it.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You can do it easily,
 
Code for binding database values to company drop down :
 

Getting values from data base, you will have those details in ViewState["Company"] and bind to your drop down as shown in below code.

ddlCompany.DataSource = ViewState["Company"];
               ddlCompany.DataTextField = "description";
               ddlCompany.DataValueField = "value";
               ddlCompany.DataBind();
 

To build Departments :
 
You will have one input field for Departments dropdown in your stored procedure,
give it the input value as
 
cmnDetails.companyId = ddlCompany.SelectedValue;
 
In my code I used three tier architecture, hope you understand as I can't place all the code here. But if you need it I will post .
 
There is another way to solve is using Cascading dropdown concept.
 
Thank you
  Permalink  
v2
Comments
Member 10445950 at 18-Feb-14 8:02am
   
please use by using three tier architecture
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

http://madhavareddydotnetsolutions.blogspot.in/[^]
 
This linl is help full to you check it.
  Permalink  
Comments
Member 10755393 at 22-Apr-14 2:03am
   
hi,
this is vijay can anyone tell me how to work with 3 dropdownlist in 3tierArchitecture.
i.e iam having 3 dropdownlist "ddlcountry", "ddlstate", "ddlcity". if i select "india" in ddlcountry i should get states of india in ddlstate and if i select the "AP" in ddlstate then i should get the cities of AP in ddlcity.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

see
 

AutoComplete With DataBase and AjaxControlToolkit

 
this is not exactly what you want, however the use of ajax is the way to go.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

you can use following things:
protected void Page_Load(object sender, EventArgs e)
   {
 
        if (!IsPostBack)
        {
             FillDropDownList();
        }
    }
 
Make company drop down Auto postback true.
 

   private void FillDropDownList()
         {
             DataSet ds = new DataSet();
             SqlDataAdapter myda = new SqlDataAdapter("Select company  FROM Tablename", connection Object);
        myda.Fill(ds);
        drop_company.DataSource = ds;
        drop_company.DataValueField = "company";
        drop_company.DataBind();
        drop_company.Items.Insert(0, new ListItem("Select", "0"));
    }
 
Double click on Company`s DropDown.;
 protected void drop_company_SelectedIndexChanged(object sender, EventArgs e)
    {
 
        DataSet ds = new DataSet();
        SqlDataAdapter myda = new SqlDataAdapter("Select department FROM tablename where company='"+drop_company.SelectedItem.Value+"'",connection_Object);
        myda.Fill(ds);
        drop_dept.DataSource = ds;
        drop_dept.DataValueField = "department";
        drop_dept.DataBind();
        drop_dept.Items.Insert(0, new ListItem("Select", "0"));
 
    }
 

Hope This will Help You, if not please Post it.
  Permalink  
v2
Comments
a2ulthakur at 30-Oct-12 0:56am
   
its saying drop_comany does not exist in current context ..how to define it
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,649
1 Sergey Alexandrovich Kryukov 6,280
2 CPallini 5,230
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 4 Oct 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100