Click here to Skip to main content
15,916,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to set data in dropdownlist2 on selection of dropdwonlist1.
For example I want to list of all city on selection of state.My code is working after click on submit button.
code is

C#
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM AddedItem where Item_Type='" + DropDownList2.SelectedItem.Text + "'", con);
            dr = cmd.ExecuteReader();
            DropDownList3.DataSource = dr;
            DropDownList3.DataValueField = "Item_Name";
            DropDownList3.DataBind();
            DropDownList3.SelectedIndex = -1;
            con.Close();
        }


 protected void Button1_Click(object sender, EventArgs e)
        {
            string st = "";
          
            if (IsPostBack)
            {
                try
                {
                 
                    SqlCommand cmd1 = new SqlCommand("SELECT Item_Id from AddedItem where Item_Name='" + DropDownList3.SelectedItem.Text + "'", con);
                    SqlDataReader dr = cmd1.ExecuteReader(CommandBehavior.SingleRow);
                    if (dr.Read())
                    {
                     
                        st = dr[0].ToString();
                        dr.Close();
                    }
                    SqlCommand cmd = new SqlCommand("INSERT INTO IssuedItem VALUES('" + st + "','" + DropDownList1.SelectedItem.Text + "','" + DropDownList2.SelectedItem.Text + "','"+DropDownList3.SelectedItem.Text+"','"+TextBox1.Text+"','"+TextBox2.Text+"')", con);
              
                    cmd.ExecuteNonQuery();
                }
                catch (SqlException ex)
                {

                }
                finally
                {
                    con.Close();

                }
            }

        }
Posted
Updated 16-Nov-11 17:58pm
v2

My code is working after click on submit button
Replace the code which is in buttonclick event to DropDownList1_SelectedIndexChanged event. You will be done.
 
Share this answer
 
am a bit lost here..
dropdownlist1 is your state dropdown
and dropdownlist 2 is your city dropdown right?
so why dont you populate your state dropdownlist on page load
and in the selectedindexchanged event of your state dropdownlist u can populate your city dropdownlist based on the state selected
 
Share this answer
 
according to my understanding what you have to do is repeating the same logic that you did for drop down list 2 and nothing more...

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM AddedItem where Item_Type='" + DropDownList1.SelectedItem.Text + "'", con);
            dr = cmd.ExecuteReader();
            DropDownList2.DataSource = dr;
            DropDownList2.DataValueField = "Item_Name";
            DropDownList2.DataBind();
            DropDownList2.SelectedIndex = -1;
            con.Close();
        }


DropDownList1_SelectedIndexChanged

if not fired then set the drop down list property "autopostback= true"
 
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