has a SelectedValue which is invalid because it does not exist in the list of items. How do you debug?

The value coming from <%# Bind("Name") %>, which is passed to the SelectedValue property, does not match an item in its collection. Most likely causes:

  • DropDownList has no items because the evaluation happens before the list gets bound
  • The list is bound but is missing this particular value
  • The value returned could be null

I realize this is an old thread, but Google brought this up as the first option for this problem - with a fairly generic search term.

Anyway, the problems I had with this and the way I solved it are as follows. The problem will either arise, like Brian Mains said for the following reasons :

  • DropDownList has no items because the evaluation happens before the list gets bound
  • The list is bound but is missing this particular value
  • The value returned could be null

The problem i was experiencing, even though it wasn't very clear as i didn't get errors on some of the other DropDowns i was using the same method for, was that on load of the page, i was trying to use this code-behind to add an item to the DropDownList :

drpNationality.Text = GlobalScript.CountDatabaseRecords("SELECT [nationality_desc] FROM [tbl_people] INNER JOIN [tbl_lkup_nationality] AS nationality ON [nationality] = [nationality_id] WHERE [person_id] ='" + Session["ID"] + "'");

And here is the DropDown HTML (which had items populated by a DataSource) :

<label>Nationality:</label>
<asp:DropDownList ID="drpNationality" runat="server" DataSourceID="Nationality_Datasource" DataTextField="nationality_desc" DataValueField="nationality_id">
</asp:DropDownList>

Now, the problem I was getting was that the data hadn't been bound to the control at the time of Load, when i was trying to add items in the code-behing. Because I was trying to pre-select a value from the database at start-up for the user (which did exist in the list), I wasn't too bothered if the item essentially appeared twice in there.

So my work-around was the following.

I changed the code behind to the following, so that the item was added to the DropDownList on execution of the Load Event code and then selected :

var = GlobalScript.CountDatabaseRecords("SELECT [nationality] FROM [tbl_people] INNER JOIN [tbl_lkup_nationality] AS nationality ON [nationality] = [nationality_id] WHERE [person_id] ='" + Session["ID"] + "'");
drpNationality.Items.Add(var);
drpNationality.Text = var;

But in order for the item which was chosen in the code-behind to stay once the page is fully loaded and not be overwritten by the DataSource, you must change the HTML to the following :

<label>Nationality:</label>
<asp:DropDownList ID="drpNationality" runat="server" DataSourceID="Nationality_Datasource" DataTextField="nationality_desc" DataValueField="nationality_id" AppendDataBoundItems="True">
</asp:DropDownList>

Now, when the page is loaded, the value from the database should be pre-selected in the drop down, and all the DataSource items should also be added.

Hope this helps.


DataBind stinks. I tried all the suggestions mentioned and none of them worked. Ended up setting SelectedValue to null, clearing all the items (myDDL.Items.Clear()), and then iterating through my list and manually adding new ListItems - myDDL.Items.Add(new ListItem(myListEntry.Text,myListEntry.Value))