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

Discussion in 'ASP .Net' started by J055, Nov 12, 2007.

  1. J055

    J055 Guest

    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>
     
    J055, Nov 12, 2007
    #1
    1. Advertising

  2. J055

    Guest

    On Nov 12, 2:13 pm, "J055" <> wrote:
    > 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.
     
    , Nov 13, 2007
    #2
    1. Advertising

  3. 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:
    >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.
    >Date: Mon, 12 Nov 2007 16:05:56 -0800
    >
    >On Nov 12, 2:13 pm, "J055" <> wrote:
    >> 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.
    >
    >
     
    Steven Cheng[MSFT], Nov 13, 2007
    #3
  4. J055

    J055 Guest

    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


    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > 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:
    >>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.
    >>Date: Mon, 12 Nov 2007 16:05:56 -0800
    >>
    >>On Nov 12, 2:13 pm, "J055" <> wrote:
    >>> 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.
    >>
    >>

    >
     
    J055, Nov 13, 2007
    #4
  5. 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.




    --------------------
    >From: "J055" <>
    >References: <>

    <>
    <>
    >Subject: Re: System.ArgumentOutOfRangeException: 'DropDownList' has a

    SelectedValue which is invalid because it does not exist in the list of
    items.
    >Date: Tue, 13 Nov 2007 09:54:31 -0000


    >
    >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], Nov 14, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGFiYmxlcg==?=
    Replies:
    2
    Views:
    9,245
    =?Utf-8?B?RGFiYmxlcg==?=
    Apr 14, 2006
  2. sree reddy
    Replies:
    2
    Views:
    1,768
    David C
    Jun 22, 2007
  3. Jason
    Replies:
    0
    Views:
    2,831
    Jason
    Aug 7, 2007
  4. Brett
    Replies:
    4
    Views:
    1,228
    Allen Chen [MSFT]
    Sep 23, 2008
  5. APA
    Replies:
    0
    Views:
    173
Loading...

Share This Page