asp:DropDownList Error: ‘DropDownList1’ has a SelectedValue which is invalid because it does not exist in the list of items

I have a asp.net 2.0 web site with numerous asp:DropDownList controls.
The DropDownList control contains the standard info city, state, county etc… info.
In addition to the standard codes the site also has custom codes that the users can configure themselves.
For example a animal dropdown may contain the values Dog, Cat, Fish, ect…

I am popluating the DropDownList from a SQL 2005 table that I created e.g. tblCodes

Everything works great and users are able to add orders using the numerous DropDownList controls to choose items from the list.

The problem occurrs if a user wants to change one of their custom dropdowns. For example a user would like to change the verbage
on a animal type control from Dog to K9. This is where the problem starts.

For all new orders the drop down works fine. When the user retrieved an old order
I get the following error in the C# codebehind
“‘DropDownList1’ has a SelectedValue which is invalid because it does not exist in the list of items.”

What’s happening is the old order has a database field value of Dog and the DropDownList no longer has Dog in its list since the user changed it to K9.

Any ideas on a workaround?
Is there a way to make the asp:DropDownList accept items not seeded in its list?
Is there another control I could use?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I solved this exact same problem just two days ago. First, I moved the code that set the SelectedValue to a PreRender handler for the DropDownList. Then, I add logic to first check to see if the value is in the drop down list. If not, I add it.

Here’s my code. ddSpecialty is my drop-down list, populated with “specialties” from the database. registration.Specialty is the specialty that the user chose, which may or may not be in the drop down, since that particular specialty may have been deleted since they last chose it.

protected void ddSpecialty_PreRender(object sender, EventArgs e)
{
    if (!ddSpecialty.Items.Contains(new ListItem(registration.Specialty)))
        ddSpecialty.Items.Add(registration.Specialty);
    ddSpecialty.SelectedValue = registration.Specialty;
}

Method 2

I’ve become very fond of the following little snippet for setting DropDownList values:

For non-DataBound (eg Items added manually):

ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(value));

For DataBound:

ddl.DataBound += (o,e) => ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(value));

I sure do wish though that ListControls in general didn’t throw errors when you try to set values to somthing that isn’t there. At least in Release mode anyways it would have been nice for this to just quietly die.

Method 3

Your SelectedValue should be a unique id of some sort, that doesn’t change. The Text value that gets displayed to the user is something seperate, and can change if necessary without affecting your application, because you associate the id with your Order, not the displayed string value.

Method 4

I’m not sure it’s the same issue, but I had a similar sounding issue with trying to bind a DropDownList that I wanted to contain in a GridView. When I looked around I found a lot of people asking similar questions, but no robust solutions. I did read conflicting reports about whether you could intercept databinding, etc events. I tried most of them but I couldn’f find a way of intercepting or pre-empting the error.

I ended up creating a subclass of the ddl, intercepting the error from there hacking a fix.

Not tidy but it worked for my needs. I put the code up on my blog in case it’s of help. link text

Method 5

Check this:

http://www.codeproject.com/Tips/179184/ASP-dropdownlist-missing-value-error.aspx

Method 6

Ran into this myself. Oddly, ddl.ClearSelection(); didn’t work. Had to use ddl.SelectedValue = null

Also noticed, that this must come AFTER I clear the items from the list ddl.Items.Clear(); which also seems weird. Setting the SelectedValue to null, then clearing the items still threw the error.

Once this is done, re-bind the list and re-select with new value.

Method 7

Try this:

if (ddl.Items.Contains(new ListItem(selectedFacility)))
    ddl.SelectedValue = selectedFacility;

Method 8

I have made a workaround after having this problem very often. Unfortunate that MS still did not recovered this issue.

Anyway, my workaround is as follows.

1) I bind the data to the ToolTip property of the DropDownList

<asp:DropDownList ID="edtDepartureIDKey" runat="server" CssClass="textbox" 
ToolTip='<%# Eval("DepartureIDKey") %>' DataSource="<%# DLL1DataSource() %>" DataTextField="DisplayField" DataValueField="IDKey"
onprerender="edtDepartureIDKey_PreRender">

2) On the prerender event i check the availibilty of the data, and if it is not in the list I simply add it, then set the selectedindex to the data valuei which I saved in ToolTip property

protected void edtDepartureIDKey_PreRender(object sender, EventArgs e)
{
    DropDownList ddl = (sender as DropDownList);
    if (ddl.Items.FindByValue(ddl.ToolTip) == null)
    {
        //I am pulling Departure Data through the ID which is saved in ToolTip, and insert it into the 1st row of the DropDownList
        TODepartureData v = new TODepartureData(DBSERVER.ConnStrName);
        TODeparture d = v.Select(Convert.ToInt32(ddl.ToolTip));
        ddl.Items.Insert(0, new ListItem(d.DeptCode, ddl.ToolTip));
    }
    ddl.Items.FindByValue(ddl.ToolTip).Selected = true;
}

how it looks


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x