Reputation: 6904
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.
Upvotes: 0
Views: 702
Reputation: 7147
Shamelessly stealing Adam Maras comment and posting as an answer:
What that means is that somewhere, later on in the page's lifecycle, something is calling ddlCountry.DataBind()
again, which causes the control to add the contents of the data source to the list a second time.
Upvotes: 4