Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to fill a second dropdownlist based on the selection made in the first one.
i want to use a condition:
for example:
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
       {

           if (dropdownlis1t.SelectedValue == "1")
           {
               SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[""].ConnectionString);
               con.Open();

               SqlCommand cmd = new SqlCommand("Select [Code] from Table where Index= '3' OR Index= '2'", con);
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataSet ds = new DataSet();
               da.Fill(ds);
               con.Close();
               dropdownlist2.DataSource = ds;
               dropdownlist2.DataTextField = "Code";
               dropdownlist2.DataValueField = "Code";
               dropdownlist2.DataBind();

           }
           else
               if (dropdownlist1.SelectedValue == "2")
               {

                   SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[""].ConnectionString);
                   con.Open();

                     SqlCommand cmd = new SqlCommand("Select [Code] from Table where Index= '1'", con);
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataSet ds = new DataSet();
               da.Fill(ds);
               con.Close();
               dropdownlist2.DataSource = ds;
               dropdownlist2.DataTextField = "Code";
               dropdownlist2.DataValueField = "Code";
               dropdownlist2.DataBind();
               }
Posted
Comments
Richard C Bishop 18-Jan-13 16:19pm    
Does your example not work?
lovitaxxxx 18-Jan-13 16:31pm    
noo
the second dropdownlist doesnt take any value
remains empty
Richard C Bishop 18-Jan-13 16:48pm    
In your sqlcommand, you need to remove the single tick marks from the numbers in the where clause. Your dropdownlist2 was not getting any data because your query is bad is my guess. Try that and let me know. "Select [code] from table where Index = 1;"

according to your code..the values does not populate to the dropdownlist.

C#
dropdownlist2.DataTextField = "Code";
                dropdownlist2.DataValueField = "Code";


because, it will not hold the values but it just assigns the values to the
dropdownlist textfield and value field.

to populate.this is one of the method

prepare the dataset;
dataset dv;
int n = dv.Tables["tblcountry"].Rows.Count;

     DropDownList.Items.Clear();
     DropDownList.Items.Add("--Select--");

     for (int i = 0; i <= n - 1; i++)
     {
         DropDownList.DataSource = dv;
         ListItem li = new ListItem();
         li.Text = dv.Tables["tblcountry"].Rows[i][1].ToString();
         li.Value = dv.Tables["tblcountry"].Rows[i][0].ToString();
         DropDownList.Items.Add(li);
     }

it works try this method.
 
Share this answer
 
I think you need to perform a callback whenever, the first drop down changes. It's being a long time I wrote code to do that. This link could be helpful.
http://support.microsoft.com/kb/910450[^]
 
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