'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
I have found many people have this exception in their dropdownlist databinding. It's annoying when upgrade an application from asp.net 1.1 to 2.0. Code used to work now breaks.
This exception is caused when a drop down list has been bound to a datasource, with a item selected, the .net runtime tries to bind the drop down list to a new datasource with different data, and the selected value is not in the new datasource.
There is one work around which needs to set the AppendDataBoundItems property to true. This works if the drop down list is bound only once. If it's bound several times, the item list will grow with repeatitive values.
The only workable solution I have found is to do this:
In the stored procedure, put a union select top item for the list as
select Text, Value from Customer
union
select '', null
In the client side, whenever before the list is bound, put a line of code like this:
Dropdownlist1.SelectedValue = ''
This would solve the problem.
Friday, 22 December 2006
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment