'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

Last post 09-28-2009 12:08 PM by justbourlier. 14 replies.

Sort Posts:

  • Crying [:'(] 'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

    11-15-2005, 10:52 AM
    • Member
      150 point Member
    • supersven
    • Member since 04-24-2005, 9:22 PM
    • Posts 33

    Crying <img src=" src="/emoticons/emotion-9.gif"> Can someone help me with the following problem.

    The dropdownlist raises always the following exception when the binded field has a value that doesn't exist in the list of values.

    'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

    As it is possible for the user to change the values of the list (in a codetable) it is possible that an earlier selected item not exists anymore. Then i would select an default item.

    (I tried already to create an custom dropdown control an override the databinding procedure but then the postback information get lost.)

    How can i handle this exception?

     

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

    11-15-2005, 1:34 PM
    • Participant
      1,047 point Participant
    • msamford
    • Member since 03-25-2004, 6:33 PM
    • Houston TX USA
    • Posts 210

    'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

    I am assuming you are trying to programatically set the selectedvavlue. If I understand you correctly, try this to see if it will work:

    add this to your code

    try
    dropdownlist.selectedvalue = theselectionthepersonmakes
    catch ex as exception
    present them with a statement that the selected value no longer exists.
    end try

    I do not know what you are using this for, so I am not sure if it will work for you, but this should prevent the page from erroring out on them.

    Programmer
    Compcentrics

    The two most powerfull words on the internet are "Search" and "Engine"
  • Betreft: Re: 'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

    11-15-2005, 3:14 PM
    • Member
      150 point Member
    • supersven
    • Member since 04-24-2005, 9:22 PM
    • Posts 33
    No, i bounded the dropdownlist at a objectdatasource which should set automatic the selected value to the databinded fieldvalue.

    I had already found an solution for my problem: Override the DataBinding method and catch the exception.

    protected override void OnDataBinding(EventArgs e)
    {
       
    try{base.OnDataBinding(e);}

       catch (ArgumentOutOfRangeException ex){
          this.ClearSelection();
          
    ListItem li = new ListItem("Value not exists in valuelist", "-99");
          
    li.Selected = true;
       
       this.Items.Insert(0, li);
       }

    }

  • Re: Betreft: Re: 'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

    08-09-2006, 11:59 PM
    • Member
      33 point Member
    • bobyang75
    • Member since 08-03-2006, 1:03 AM
    • Posts 7

    I have the same problem but how do you make it works. thanks. I got these errors.

     

    Error 1 'Form1' does not contain a definition for 'ClearSelection'  

    Error 2 'System.Web.UI.WebControls.ListItem' does not contain a definition for 'ClearSelection'  


    Error 3 'System.Collections.IDictionary' does not contain a definition for 'Insert'  

  • Re: Betreft: Re: 'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

    01-03-2007, 5:39 PM
    • Member
      581 point Member
    • JimAmigo
    • Member since 01-28-2004, 1:40 PM
    • Posts 252
    Can you elaborate where this code goes, in the codebehind or wired to a particular event?
    Jim
  • Re: Betreft: Re: 'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

    01-27-2007, 2:14 AM
    • Member
      184 point Member
    • bdc604
    • Member since 05-22-2006, 8:35 PM
    • Posts 41

    asp dropdownlist sucks :: the blog

    http://aspddlsucks.spaces.live.com/

  • Re: Betreft: Re: 'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

    01-30-2007, 3:47 PM
    • All-Star
      18,370 point All-Star
    • PeterBrunone
    • Member since 06-19-2002, 9:15 AM
    • I'm standing behind you.
    • Posts 3,683
    • TrustedFriends-MVPs

        You might consider EasyListBox, which not only doesn't throw an error, but also keeps the value if you've set it before the list is populated.  Then when the items are added, it tries again to see if it can select the value you want.

        If cost is a problem, you can get a really cheap license through DotNetCharity right now ($10/$20 for a single-website version).
     

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • Re: Betreft: Re: 'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

    04-11-2007, 3:12 PM
    • Member
      64 point Member
    • baboso#4
    • Member since 07-07-2006, 8:56 PM
    • Posts 97

    Just FYI

    I had the same problem in a gridview template with dropdownlist and all it took was for me to enable the postback on the dropdownlist.  Go figure.

  • Re: Betreft: Re: 'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

    08-07-2008, 2:34 AM
    • Member
      6 point Member
    • NPtr
    • Member since 02-18-2008, 2:50 PM
    • Posts 3

    supersven:

    I had already found an solution for my problem: Override the DataBinding method and catch the exception.
     Yeah, this solution works and I'm using it, but I think it's "not the fastest" way to achieve this by catching exception.

  • Re: Betreft: Re: 'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

    08-17-2008, 12:16 AM
    • Member
      48 point Member
    • bartekm
    • Member since 05-01-2007, 11:50 AM
    • Sydney
    • Posts 26

    I much better way than catching an exception is to first check if your value exists in the dropdownlist on databound.

    Details here: http://blog.evonet.com.au/post/2008/06/07/dropdownlist1-has-a-SelectedValue-which-is-invalid-because-it-does-not-exist-in-the-list-of-items.aspx

    Bartek

    Bartek Marnane
    Evonet Consulting Pty Ltd
    ASP.NET Development Blog:
    http://blog.evonet.com.au
  • Re: 'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

    06-29-2009, 9:44 AM

    A workaround that worked fine for me was setting the SelectedValue to null...

    DropDownList1.DataSource = MyDataSource;
    DropDownList1.DataValueField = "ID";
    DropDownList1.DataTextField = "Name";
    DropDownList1.DataBind();

    This throws the -- 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value --

    If I simply add the "DropDownList1.SelectedValue = null;" it works fine:

    DropDownList1.DataSource = MyDataSource;
    DropDownList1.DataValueField = "ID";
    DropDownList1.DataTextField = "Name";
    DropDownList1.SelectedValue = null;
    DropDownList1.DataBind();


    Hope this works fine for U too guys...

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

    07-02-2009, 3:19 PM
    • Member
      25 point Member
    • SPDmma
    • Member since 12-08-2008, 4:30 AM
    • Posts 72

    Where did you put this code? An event of the ddl or somewhere else?

    Thanks,

    Shawn
  • Re: Betreft: Re: 'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

    07-02-2009, 3:39 PM
    • Member
      309 point Member
    • anu0987
    • Member since 02-09-2006, 1:17 PM
    • Providence, RI, USA
    • Posts 186

    Most of these kind of issue with drop down is due to it not getting binded again after post back. So your dropdown has no value when you call the event with dropdown.Selected value. Bind the dropdown again in

    if  page.Ispostback then

    'bind dd

    Mark this as an answer if you find my it helpful.

    Thanks,
    Anubhuti
  • Re: Betreft: Re: 'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

    09-11-2009, 1:28 PM
    • Member
      64 point Member
    • baboso#4
    • Member since 07-07-2006, 8:56 PM
    • Posts 97

    Lots of times, the individual is binding an object or SQLDataSource to a dropdown list.   When the datasource tries to read the value from the dropdownlist the first time, there is no value set on the dropdownlist therefore you get an exception.   One way to bypass this is to set a default value on the dropdownlist on it's databound event like this "ddl.items.insert(0, new Item("Select Item", "1");".  1 is the value in this case, "Select Item"  is what the ddl displays to the user.  The value in this case is a row ID.  The default value should be a valid data type that  the datasource can read even if it doesn't exist.

  • Re: Betreft: Re: 'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

    09-28-2009, 12:08 PM
    • Member
      38 point Member
    • justbourlier
    • Member since 04-10-2008, 11:39 AM
    • Posts 19

    As the person aboved mentioned, this is generally when you're using "Bind" on your dropdown box, and it's a null value.   The easiest way I know of to get around this is to add <ListItem Text="no value" value="" />  as an item in your dropdown list, and set AppendDataBoundItems = true

     

    This will then select the "no value" list item for database values that are null.  This is much simpler than the solutions above, and less complexity means less chances for these to go awry.

     An excellent article on this subject is below.

    http://msdn.microsoft.com/en-us/library/ms366709.aspx


    Here is an example of what I mean.

    <asp:DropDownList ID="ddlEmployees" AppendDataBoundItems="true" SelectedValue='<%#Bind("EmployeeId") %>' runat="server" DataSourceId="ldsGetEmployees" DataTextField="FullName" DataValueField="EmployeeId">
                    <asp:ListItem Text="none" Value=""></asp:ListItem>
    </asp:DropDownList>

Page 1 of 1 (15 items)