Shipra Jain

Shipra Jain

  • NA
  • 3
  • 966

2 Dropdown lists and a label interdependent

Jul 31 2014 10:36 AM
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....

Answers (3)