Error filling DropdownList

Discussion in 'ASP .Net' started by tshad, Nov 11, 2005.

  1. tshad

    tshad Guest

    Here is the DropDown:
    <asp:DropDownList ID="ScreenTest" runat="server" />

    Here is the code:
    ScreenTest.DataSource=objCmd.ExecuteReader
    ScreenTest.DataValueField="ScreenTemplateMasterID"
    ScreenTest.DataTextField= "ScreenName"
    ScreenTest.databind()
    ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",0))

    Here is the error
    ******************************************************************************************************************
    Specified argument was out of the range of valid values. Parameter name:
    value
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.ArgumentOutOfRangeException: Specified argument
    was out of the range of valid values. Parameter name: value

    Source Error:


    Line 68: ScreenTest.DataValueField="ScreenTemplateMasterID"
    Line 69: ScreenTest.DataTextField= "ScreenName"
    Line 70: ScreenTest.databind()
    <------- The error
    Line 71: ScreenTest.Items.Insert(0, new ListItem("Select Screen
    Test",""))
    Line 72:
    ********************************************************************************************************************

    Here is the trace of the DataReader Just before (Normally this is not there
    as it would empty the reader)

    ScreenName = Adminstrative Assistant
    ScreenTemplateMasterID = 2
    ScreenName = Network Analyst
    ScreenTemplateMasterID = 1
    ScreenName = Sales Clerk
    ScreenTemplateMasterID = 4
    ScreenName = Sales Manager
    ScreenTemplateMasterID = 3

    I assume the error is on the ScreenTemplateMasterID, but I don't see any
    illegal values there.

    What could have caused this?

    Thanks,

    Tom
     
    tshad, Nov 11, 2005
    #1
    1. Advertising

  2. tshad

    Greg Burns Guest

    "tshad" <> wrote in message
    news:...
    > Here is the DropDown:
    > <asp:DropDownList ID="ScreenTest" runat="server" />
    >
    > Here is the code:
    > ScreenTest.DataSource=objCmd.ExecuteReader
    > ScreenTest.DataValueField="ScreenTemplateMasterID"
    > ScreenTest.DataTextField= "ScreenName"
    > ScreenTest.databind()
    > ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",0))


    Maybe you dropdown already has those values? Are you binding twice? Not
    sure you would get that error or not.

    Try adding this prior to your databind:
    ScreenItems.Items.Clear()

    Your code says this:
    ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",0))

    But stack trace has this:
    ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",""))

    Regardless, I would turn on Option Strict and you see that this:
    ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",0))

    Needs to be:
    ScreenTest.Items.Insert(0, new ListItem("Select Screen Test","0"))

    Or maybe it really is just:
    ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",""))

    and that was a typo. :^)

    Greg
     
    Greg Burns, Nov 11, 2005
    #2
    1. Advertising

  3. tshad

    tshad Guest

    "Greg Burns" <> wrote in message
    news:...
    > "tshad" <> wrote in message
    > news:...
    >> Here is the DropDown:
    >> <asp:DropDownList ID="ScreenTest" runat="server" />
    >>
    >> Here is the code:
    >> ScreenTest.DataSource=objCmd.ExecuteReader
    >> ScreenTest.DataValueField="ScreenTemplateMasterID"
    >> ScreenTest.DataTextField= "ScreenName"
    >> ScreenTest.databind()
    >> ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",0))

    >
    > Maybe you dropdown already has those values? Are you binding twice? Not
    > sure you would get that error or not.
    >

    No.

    If that were the case I would have gotten the traces twice.

    > Try adding this prior to your databind:
    > ScreenItems.Items.Clear()


    I tried that.

    Didn't change anything.
    >
    > Your code says this:
    > ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",0))
    >
    > But stack trace has this:
    > ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",""))
    >
    > Regardless, I would turn on Option Strict and you see that this:
    > ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",0))
    >
    > Needs to be:
    > ScreenTest.Items.Insert(0, new ListItem("Select Screen Test","0"))
    >
    > Or maybe it really is just:
    > ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",""))
    >
    > and that was a typo. :^)
    >


    I had copied the error already and did a couple of other tests to see if I
    could narrow it down. That was why that line was different. Actually, the
    Double-Quotes is how I originally had it.

    I also thought that maybe I had accidently created another object with that
    name, but that wasn't the case either.

    I took the SP and tried run it from Query Analyser and got the same results
    as my trace.

    ScreenTemplateMasterID ScreenName
    ---------------------- --------------------------------------------------
    2 Adminstrative Assistant
    1 Network Analyst
    4 Sales Clerk
    3 Sales Manager

    (4 row(s) affected)

    This doesn't make any sense.

    It was working fine before.

    Thanks,

    Tom
    > Greg
    >
     
    tshad, Nov 11, 2005
    #3
  4. Hi tshad,

    When you DataBind the dropdown, it trys to reset itself to its previously
    selected value.
    Call ClearSelection() on it before you bind it.

    Tim

    "tshad" wrote:

    > Here is the DropDown:
    > <asp:DropDownList ID="ScreenTest" runat="server" />
    >
    > Here is the code:
    > ScreenTest.DataSource=objCmd.ExecuteReader
    > ScreenTest.DataValueField="ScreenTemplateMasterID"
    > ScreenTest.DataTextField= "ScreenName"
    > ScreenTest.databind()
    > ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",0))
    >
    > Here is the error
    > ******************************************************************************************************************
    > Specified argument was out of the range of valid values. Parameter name:
    > value
    > Description: An unhandled exception occurred during the execution of the
    > current web request. Please review the stack trace for more information
    > about the error and where it originated in the code.
    >
    > Exception Details: System.ArgumentOutOfRangeException: Specified argument
    > was out of the range of valid values. Parameter name: value
    >
    > Source Error:
    >
    >
    > Line 68: ScreenTest.DataValueField="ScreenTemplateMasterID"
    > Line 69: ScreenTest.DataTextField= "ScreenName"
    > Line 70: ScreenTest.databind()
    > <------- The error
    > Line 71: ScreenTest.Items.Insert(0, new ListItem("Select Screen
    > Test",""))
    > Line 72:
    > ********************************************************************************************************************
    >
    > Here is the trace of the DataReader Just before (Normally this is not there
    > as it would empty the reader)
    >
    > ScreenName = Adminstrative Assistant
    > ScreenTemplateMasterID = 2
    > ScreenName = Network Analyst
    > ScreenTemplateMasterID = 1
    > ScreenName = Sales Clerk
    > ScreenTemplateMasterID = 4
    > ScreenName = Sales Manager
    > ScreenTemplateMasterID = 3
    >
    > I assume the error is on the ScreenTemplateMasterID, but I don't see any
    > illegal values there.
    >
    > What could have caused this?
    >
    > Thanks,
    >
    > Tom
    >
    >
    >
     
    =?Utf-8?B?dGlta2xpbmc=?=, Nov 12, 2005
    #4
  5. tshad

    tshad Guest

    "timkling" <> wrote in message
    news:...
    > Hi tshad,
    >
    > When you DataBind the dropdown, it trys to reset itself to its previously
    > selected value.
    > Call ClearSelection() on it before you bind it.


    Is that different than ScreenTest.Items.Clear()?

    Thanks,

    >
    > Tim
    >
    > "tshad" wrote:
    >
    >> Here is the DropDown:
    >> <asp:DropDownList ID="ScreenTest" runat="server" />
    >>
    >> Here is the code:
    >> ScreenTest.DataSource=objCmd.ExecuteReader
    >> ScreenTest.DataValueField="ScreenTemplateMasterID"
    >> ScreenTest.DataTextField= "ScreenName"
    >> ScreenTest.databind()
    >> ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",0))
    >>
    >> Here is the error
    >> ******************************************************************************************************************
    >> Specified argument was out of the range of valid values. Parameter name:
    >> value
    >> Description: An unhandled exception occurred during the execution of the
    >> current web request. Please review the stack trace for more information
    >> about the error and where it originated in the code.
    >>
    >> Exception Details: System.ArgumentOutOfRangeException: Specified argument
    >> was out of the range of valid values. Parameter name: value
    >>
    >> Source Error:
    >>
    >>
    >> Line 68: ScreenTest.DataValueField="ScreenTemplateMasterID"
    >> Line 69: ScreenTest.DataTextField= "ScreenName"
    >> Line 70: ScreenTest.databind()
    >> <------- The error
    >> Line 71: ScreenTest.Items.Insert(0, new ListItem("Select Screen
    >> Test",""))
    >> Line 72:
    >> ********************************************************************************************************************
    >>
    >> Here is the trace of the DataReader Just before (Normally this is not
    >> there
    >> as it would empty the reader)
    >>
    >> ScreenName = Adminstrative Assistant
    >> ScreenTemplateMasterID = 2
    >> ScreenName = Network Analyst
    >> ScreenTemplateMasterID = 1
    >> ScreenName = Sales Clerk
    >> ScreenTemplateMasterID = 4
    >> ScreenName = Sales Manager
    >> ScreenTemplateMasterID = 3
    >>
    >> I assume the error is on the ScreenTemplateMasterID, but I don't see any
    >> illegal values there.
    >>
    >> What could have caused this?
    >>
    >> Thanks,
    >>
    >> Tom
    >>
    >>
    >>
     
    tshad, Nov 12, 2005
    #5
  6. Yeah, it is different.

    Whether you clear the Items collection, or you just call DataBind() (which
    clears the collection for you first anyway), the dropdown "remembers" what
    the selected value is. When you rebind it, it tries to select an item with
    the same value, if there is none, it throws an exception. Calling
    ClearSelection(), explicitly clears any selected value, so that it will not
    try to reset the selected item while DataBinding.

    Tim


    "tshad" wrote:

    > "timkling" <> wrote in message
    > news:...
    > > Hi tshad,
    > >
    > > When you DataBind the dropdown, it trys to reset itself to its previously
    > > selected value.
    > > Call ClearSelection() on it before you bind it.

    >
    > Is that different than ScreenTest.Items.Clear()?
    >
    > Thanks,
    >
    > >
    > > Tim
    > >
    > > "tshad" wrote:
    > >
    > >> Here is the DropDown:
    > >> <asp:DropDownList ID="ScreenTest" runat="server" />
    > >>
    > >> Here is the code:
    > >> ScreenTest.DataSource=objCmd.ExecuteReader
    > >> ScreenTest.DataValueField="ScreenTemplateMasterID"
    > >> ScreenTest.DataTextField= "ScreenName"
    > >> ScreenTest.databind()
    > >> ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",0))
    > >>
    > >> Here is the error
    > >> ******************************************************************************************************************
    > >> Specified argument was out of the range of valid values. Parameter name:
    > >> value
    > >> Description: An unhandled exception occurred during the execution of the
    > >> current web request. Please review the stack trace for more information
    > >> about the error and where it originated in the code.
    > >>
    > >> Exception Details: System.ArgumentOutOfRangeException: Specified argument
    > >> was out of the range of valid values. Parameter name: value
    > >>
    > >> Source Error:
    > >>
    > >>
    > >> Line 68: ScreenTest.DataValueField="ScreenTemplateMasterID"
    > >> Line 69: ScreenTest.DataTextField= "ScreenName"
    > >> Line 70: ScreenTest.databind()
    > >> <------- The error
    > >> Line 71: ScreenTest.Items.Insert(0, new ListItem("Select Screen
    > >> Test",""))
    > >> Line 72:
    > >> ********************************************************************************************************************
    > >>
    > >> Here is the trace of the DataReader Just before (Normally this is not
    > >> there
    > >> as it would empty the reader)
    > >>
    > >> ScreenName = Adminstrative Assistant
    > >> ScreenTemplateMasterID = 2
    > >> ScreenName = Network Analyst
    > >> ScreenTemplateMasterID = 1
    > >> ScreenName = Sales Clerk
    > >> ScreenTemplateMasterID = 4
    > >> ScreenName = Sales Manager
    > >> ScreenTemplateMasterID = 3
    > >>
    > >> I assume the error is on the ScreenTemplateMasterID, but I don't see any
    > >> illegal values there.
    > >>
    > >> What could have caused this?
    > >>
    > >> Thanks,
    > >>
    > >> Tom
    > >>
    > >>
    > >>

    >
    >
    >
     
    =?Utf-8?B?dGlta2xpbmc=?=, Nov 12, 2005
    #6
    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. DavidC
    Replies:
    2
    Views:
    1,780
    DavidC
    Jan 18, 2010
  2. deepak kumar
    Replies:
    13
    Views:
    352
    deepak kumar
    Nov 5, 2004
  3. David Londeck

    Trouble Filling DropDownList in Datagrid

    David Londeck, Mar 28, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    121
    Brock Allen
    Mar 28, 2005
  4. Mohsen Pahlevanzadeh
    Replies:
    0
    Views:
    147
    Mohsen Pahlevanzadeh
    Sep 9, 2013
  5. MRAB
    Replies:
    0
    Views:
    134
Loading...

Share This Page