Earlier today, while I was trying to bind a collection to a drop down list, I got the following exception:
‘DropDownList1’ has a SelectedValue which is invalid because it does not exist in the list of items.
After inspecting my code, it turned out that the culprit was a rather benign line of code. Since this exception generated is so obscure, I am going to show you the code in its simplest form that would cause this exception so that you can solve similar problems easier.
Suppose I have a drop down list in my aspx page that is bound to a DataTable (the DataSource is not important in the scenario when the exception occurs, it can be an object data source as well, to illustrate the point, I will use a DataTable):
Where the data is populated through method GetData()
In the Page_Load method we can bind the DropDownList as follows:
Indeed, this is what we have expected. The problem comes when you try to assign the displayed text to the DropDownList. To illustrate, the following code will cause the exception mentioned earlier:
If the text is assigned after the DropDownList is bound however, the code will execute just fine but the assignment will have no effect of course.
Interesting enough, if the above code is placed within a try..catch block, the DataBinding will work just fine even though an exception had been thrown!