I am using this query to populate a drop down list on my ASP.NET page:
using (CountriesRegionsDataContext db = new CountriesRegionsDataContext())
{
ddlCountry.Items.Clear();
ddlCountry.DataSource = from c in db.CountryCodes orderby c.CountryName select c;
ddlCountry.DataTextField = "CountryName";
ddlCountry.DataValueField = "CountryCode1";
ddlCountry.AppendDataBoundItems = true;
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("- Select a country -", "0"));
}
The data source is a simple SQL table which contains no duplicates. However when my page renders, the bound data is repeated twice in the drop down list control (a-za-z).
Does anyone have an idea as to what might be going on? Using distinct() doesn't seem to solve the problem (and there is no duplicate data in the table), and I tried adding ToList() to the end of my query and that doesn't seem to be fixing it either.
ddlCountry.DataBind()again, which causes the control to add the contents of the data source to the list a second time.DataBindis being called later on... why not just let it happen? SetAppendDataBoundItemstotrueand remove the call toDataBind. (Also, figure out whyDataBindis being called later. That doesn't sound like expected behavior to me.)