How to get past dropdownlist has a SelectedValue which is invalid because it does not exist in the list of items error

  • Thread starter Thread starter mscheaffer
  • Start date Start date
M

mscheaffer

Guest
dropDownList.Items.Clear();
dropDownList.SelectedIndex = -1;

dataTable dt = SomeMethodThatIsNotImportantToYou();

dropDownList.DataSource = dt;
dropDownList.DataTextField = "Text";
dropDownList.DataValueField = "ID";
dropDownList.DataBind();


This code fails on data bind with the error:

dropdownlist has a SelectedValue which is invalid because it does not exist in the list of items error.


When user loads the drop down's selected value property is set to what the user has in the database. When the user is changed I am reloading the dropdown with different values and then selecting a new value for the new user. I am not even able to get to the part where it selects a new value for the new user. It fails just trying to bind the new data. Even though I cleared the items and set the selectedindex to -1. I can us the immidiate window right before the databind call and it shows selectedIndex = -1, SelectedValue = "" and SelectedItem = null. But as soon as you call DataBind the SelectedIndex, SelectedValue, and SelectedItem all go back to their previous values and then an exception is thrown. What can I do?

By the way, DwopDownList.ClearSelection does ABSOLUTELY NOTHING.

Continue reading...
 
Back
Top