Click here to Skip to main content
15,885,799 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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
Comments
Nandakishore G N 11-Dec-12 0:49am    
what have you tried ? post it.

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.

C#
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

C#
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
 
Share this answer
 
v2
Comments
Member 10445950 18-Feb-14 8:02am    
please use by using three tier architecture
see


AutoComplete With DataBase and AjaxControlToolkit


this is not exactly what you want, however the use of ajax is the way to go.
 
Share this answer
 
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.
 
Share this answer
 
v2
Comments
a2ulthakur 30-Oct-12 0:56am    
its saying drop_comany does not exist in current context ..how to define it
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900