Can anyone help me with a C# loop to populate an ASP:DropDownList. I am using the following code to bind the data to my dropdown. However, the first result doesn’t populate in the drop down list. There should be 3 however, there is only 2 in the dropdown list.
Any help is great.
SqlDataReader dr;
dr = sqlcmd.ExecuteReader();
dropdown.DataSource = dr;
while (dr.Read())
{
dropdown.DataValueField = "ID";
dropdown.DataTextField = "Description";
dropdown.DataBind();
}
Since you’re binding the data reader to the drop down, you don’t need to loop at it explicitly (the call to
dr.Read()is effectively eating the first element).Try this:
UPDATE
To add a default item, do this after the code above: