.NET Framework - System.ArgumentOutOfRangeException: 'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

Asked By J055 on 12-Nov-07 02:13 PM
Hi

I have a DropDownList databound in a FormView and getting data from an
ObjectDataSource. The data sometimes contains some values which are not in
the list of items and so gives me an ArgumentOutOfRangeException on binding.
I would like to intercept this value and check if it is in the list of items
first so I can null it or replace with an empty string. What's the best way
to do this?

Thanks
Andrew

Bind("Title") %>' OnDataBound="TitleDropDownList_DataBinding">




lisa.shmulevic replied on 12-Nov-07 07:05 PM
One way to do this is to validate your data in the business/data layer
class that is bound to your ObjectDataSource.

Hope this helps,

Lisa Shmulevich.
stchen replied on 12-Nov-07 11:17 PM
Thaniks for Lisa's input.

Hi Andrew,

Regarding on the problem you mentioned, I agree with Lisa that it is
recommend to preprocess the data earlier than databinding stage.
Dropdownlist is a simple databound control which doesn't provide many
powerful databinding related events(like GridView , DataGrid ..). I think
it will be better to filter or format the data at business or database
layer.

BTW, is it possible that you query the list from the database table(through
some select DISTINCT ...... query)? Thus, you can cache the query in
ASP.NET when bind to the dropdownlist and the list of items can match those
ones in  database.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.




--------------------
SelectedValue which is invalid because it does not exist in the list of
items.
in
binding.
items
way
SelectedValue='<%#
J055 replied on 13-Nov-07 04:54 AM
Hi Steven and Lisa

Thanks for the suggestions. I'm going to do more checking of the data at the
business layer as you suggest. I do think there is a good argument for
allowing the DropDownList to accept a value which is not in the list of
items. Perhaps a boolean property which can change the selected index to 0?
Just a thought.

Thanks
Andrew
stchen replied on 13-Nov-07 10:08 PM
Thanks for your reply Andrew,

So far the List control doesn't provide a property to let unmatched item be
put to "0" index. Also, normally html  element doesn't care about
match or unmatch since it just have "selectindex". However, ASP.NET server
control(dropdownlist) need to do the checking to ensure the databound value
match any of the existing item in it. Anyway, if you meet any further
problem or anything need help, welcome to discuss here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.




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

the
0?