Skip to content

2 Dropdown lists and a label interdependent

Shipra Jain

Asked Jul 31, 2014, 6:36 AM EDT3 answers
Share
LinkedInX
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Items.Clear();
string spec = DropDownList1.SelectedValue.ToString();

con.Open();
string qry = "select Name,Timing from Team where Speciality=@spec";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.Parameters.AddWithValue("@spec", spec);
SqlDataReader dr = cmd.ExecuteReader();

if (dr.HasRows)
{
while (dr.Read())
{
DropDownList2.Items.Add(dr[0].ToString());

}
}
con.Close();


}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
//string doc = DropDownList2.SelectedValue.ToString();
con.Open();
string qry2 = "select Timing from Team where Name='"+ DropDownList2.SelectedValue.ToString() +"'";
SqlCommand cmd1 = new SqlCommand(qry2, con);
//cmd1.Parameters.AddWithValue("@doc", doc);
SqlDataReader dr1 = cmd1.ExecuteReader();

if (dr1.HasRows)
{

Label8.Text = dr1[0].ToString();

}
con.Close();
}
 
 
 
What I am trying to do is:
There are 2 dropdown lists. The values in 2nd gets changed based on the value selected in 1st list.
Now, I need to display the timings(in database) for the value now selected in dropdownlist 2.
But I am getting incorrect values...
 
Kindly respond me with the way to correct it asap....

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    dropdown 1 is changing values in dropdown 2 rite.

    why are you writing code in dropdown2 to save data into db, you can do this in dropdown 1 itself or at click of any button
    0
  • 0
    The 2nd dropdown list has a code for the Label whose value changes when the dropdown list content changes....
    0
  • 0
    Shipra, can you please attach your solution here? Also tell me example, what value should pop-up at what event?


    twitter @sumitjolly
    0