SelectedValue which is invalid because it does not exist in the list of items SelectedValue which is invalid because it does not exist in the list of items asp.net asp.net

SelectedValue which is invalid because it does not exist in the list of items


Apparently the solution I posted wasn't entirely effective... Eventually in my application I changed to this:

listOrgs.Items.Clear();listOrgs.SelectedIndex = -1;listOrgs.SelectedValue = null;listOrgs.ClearSelection();     // Clears the selection to avoid the exception (only one of these should be enough but in my application I needed all..)listOrgs.DataSource = new Organization().DTListAll(SiteID);listOrgs.DataTextField = "OrganizationName"; listOrgs.DataValueField = "OrganizationID"; listOrgs.DataBind();


I kept getting this error.
Weird thing is that before I set the datasource and rebind after deleting an item the selected index = -1.

If I explicitly set the selectedIndex = -1; then it works and doesn't throw an error.

So even though it was already -1 setting it to -1 stops it from erroring.

Weird eh?


Try setting listOrgs.SelectedValue = "0" after you refresh the DataSource

At the moment you are trying to select the first item in an empty list.