Click here to Skip to main content
15,908,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I binded data to a DropDownList at run time code as follows:
C#
cmd = new SqlCommand("select RoleName from Roles where Active=1", con);
con.Open();
da = new SqlDataAdapter (cmd);
da.Fill (ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = ds.Tables[0].Columns[0].ToString ();
DropDownList1.DataBind();
con.Close();

At runtime i am trying to find SelectedIndex and SelectedItem, it showing index is 0(Zero) always eventhough actual index is not 0(Zero), SelectedItem also same that first Item only showing rather then actual Item.
Please explain the reason for this and give me needful advise to get currect this.

Thanks in advance
K Uday
Posted
Updated 17-Aug-11 21:41pm
v2

Try to load the dropdown if it is not postback
C#
if(!IsPostBack)
{
cmd = new SqlCommand("select RoleName from Roles where Active=1", con);
con.Open();
da = new SqlDataAdapter (cmd);
da.Fill (ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = ds.Tables[0].Columns[0].ToString ();
DropDownList1.DataBind();
con.Close();

}
 
Share this answer
 
Make sure that you have written the Binding code inside the if(!IsPostBack).
 
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