Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#ASP.NET
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 18-Jan-13 10:16am
Comments
richcb at 18-Jan-13 16:19pm
   
Does your example not work?
Annnaa at 18-Jan-13 16:31pm
   
noo the second dropdownlist doesnt take any value remains empty
richcb at 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;"
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

according to your code..the values does not populate to the dropdownlist.
 
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 716
1 OriginalGriff 442
2 Tadit Dash 400
3 sanket saxena 337
4 Peter Leow 228
0 Sergey Alexandrovich Kryukov 11,890
1 OriginalGriff 7,335
2 Peter Leow 5,003
3 Abhinav S 3,943
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web02 | 2.8.140421.2 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid