dropdownlist-how to check the value from in list in the table
error in the below code-connot imlicitly convert type string to 'System.Web.UI.WebControls.ListItem'
Code:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection cn=myClass.DBCon();
SqlCommand cmd=new SqlCommand("select CourseName from Course",cn);
SqlDataReader dr=cmd.ExecuteReader();
while (dr.Read())
{
if (DropDownList1.SelectedItem = dr["CourseName"].ToString())
{
//code
}
}
}
Re: dropdownlist-how to check the value from in list in the table
The problem here is that DropDownList1.SelectedItem is of Type ListItem and does not immediately return the selected Value..
you have several options here ... If your looking for the DDL's selected text you can use :..
DropDownList1.SelectedItem.Text
Or if your looking for the Value behind you can use either:.
DropDownList1.SelectedItem.Value
DropDownList1.SelectedValue
NOTE : All the above returned types are string....