Click here to Skip to main content
11,637,662 members (72,612 online)
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 334
1 OriginalGriff 295
2 stibee 288
3 jyo.net 270
4 himanshu agarwal 189
0 OriginalGriff 9,031
1 Sergey Alexandrovich Kryukov 8,763
2 Mika Wendelius 6,999
3 F-ES Sitecore 2,388
4 Suvendu Shekhar Giri 2,320


Advertise | Privacy | Mobile
Web01 | 2.8.150728.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2015
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