System.ArgumentOutOfRangeException: 'DropDownList' has a SelectedValue which is invalid because it d

J

J055

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

<asp:DropDownList ID="TitleDropDownList" runat="server" SelectedValue='<%#
Bind("Title") %>' OnDataBound="TitleDropDownList_DataBinding">

<asp:ListItem Selected="True" Value="">Select...</asp:ListItem>

<asp:ListItem>Mr</asp:ListItem>

<asp:ListItem>Mrs</asp:ListItem>

<asp:ListItem>Miss</asp:ListItem>

<asp:ListItem>Ms</asp:ListItem>

<asp:ListItem>Dr</asp:ListItem>

<asp:ListItem>Prof</asp:ListItem>

</asp:DropDownList>
 
L

lisa.shmulevich

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

<asp:DropDownList ID="TitleDropDownList" runat="server" SelectedValue='<%#
Bind("Title") %>' OnDataBound="TitleDropDownList_DataBinding">

<asp:ListItem Selected="True" Value="">Select...</asp:ListItem>

<asp:ListItem>Mr</asp:ListItem>

<asp:ListItem>Mrs</asp:ListItem>

<asp:ListItem>Miss</asp:ListItem>

<asp:ListItem>Ms</asp:ListItem>

<asp:ListItem>Dr</asp:ListItem>

<asp:ListItem>Prof</asp:ListItem>

</asp:DropDownList>

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.
 
S

Steven Cheng[MSFT]

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.




--------------------
From: (e-mail address removed)
Newsgroups: microsoft.public.dotnet.framework.aspnet
Subject: Re: System.ArgumentOutOfRangeException: 'DropDownList' has a
SelectedValue which is invalid because it does not exist in the list of
items.
 
J

J055

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
 
S

Steven Cheng[MSFT]

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 <select> 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.




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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top