Click here to Skip to main content
12,547,806 members (33,114 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


8 bookmarked

ASP dropdownlist missing value error

, 3 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.

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

This error is caused when the value is not in currently in the list items.

A solution:

In your ASPX page:

<asp:DropDownList ID="yourdropdownlist" runat="server" DataSourceID="SqlDataSource1"
   DataTextField="textfield" DataValueField="valuefield" AppendDataBoundItems="true"
   SelectedIndex='<%# GetSelectedIndex("yourdropdownlist",Eval("somevalue").ToString()) %>'
   SelectedValue='<%# Bind("somevalue") %>'>

We pass the id of dropdownlist and the bound value to GetSelectedIndex in code behind which inserts the item in the list if item is not found in the list to prevent the error. you could then add validation during updating or inserting events to prevent the value if not allowed or use mapping to transform the illegal value.

In the code behind:

public int GetSelectedIndex(string id, string value)
    DropDownList list = (DropDownList)FindControlRecursive(Page.Master, Convert.ToString(id));
    int index = list.Items.IndexOf(list.Items.FindByValue(value));
    if (index == -1)
    // the value was not found so we add it 
     list.Items.Insert(0, new ListItem(value, value));
        return 0;
        return index;

Can't remember where I found this snippet but it finds a control recursively and returns the first one found:

public Control FindControlRecursive(Control Root, string Id)
    if (Root.ID == Id)
        return Root;
    foreach (Control Ctl in Root.Controls)
        Control FoundCtl = FindControlRecursive(Ctl, Id);
        if (FoundCtl != null)
            return FoundCtl;
    return null;

Note: When Using with Formview, use only in edit mode. In insert mode you don't need the selectedindex.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Cyrus Neah
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionThe only solution I found that works! Pin
renaultF111-Jul-16 21:02
memberrenaultF111-Jul-16 21:02 
QuestionThe 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive. Pin
JohannQ3-Apr-13 12:28
memberJohannQ3-Apr-13 12:28 
AnswerRe: The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive. Pin
Cyrus Neah3-Apr-13 13:46
memberCyrus Neah3-Apr-13 13:46 
GeneralThis works great for changing a value in the database that i... Pin
timBaker19566-Oct-11 13:08
membertimBaker19566-Oct-11 13:08 
GeneralRe: This works great for changing a value in the database that i... Pin
Cyrus Neah16-May-13 17:11
memberCyrus Neah16-May-13 17:11 
Generalglad it was of some use. Pin
Cyrus Neah19-May-11 7:09
memberCyrus Neah19-May-11 7:09 
GeneralGreat article, In my case, I had a datalist control, and the... Pin
tasmisr19-May-11 6:54
membertasmisr19-May-11 6:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 3 Apr 2013
Article Copyright 2011 by Cyrus Neah
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid