<p>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...</p>

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

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

<p>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.</p>

<p>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."</p>

<p>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.</p>

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

[{'answer_id': 38625, 'author': 'tbreffni', 'author_id': 637, 'author_profile': 'https://Stackoverflow.com/users/637', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 38655, 'author': 'Josh Hinman', 'author_id': 2527, 'author_profile': 'https://Stackoverflow.com/users/2527', 'pm_score': 3, 'selected': False, 'text': '<p>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.</p>\n\n<p>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.</p>\n\n<pre><code>protected void ddSpecialty_PreRender(object sender, EventArgs e)\n{\n    if (!ddSpecialty.Items.Contains(new ListItem(registration.Specialty)))\n        ddSpecialty.Items.Add(registration.Specialty);\n    ddSpecialty.SelectedValue = registration.Specialty;\n}\n</code></pre>\n'}, {'answer_id': 38684, 'author': 'David', 'author_id': 4141, 'author_profile': 'https://Stackoverflow.com/users/4141', 'pm_score': 3, 'selected': True, 'text': '<p>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.</p>\n\n<p>I ended up creating a subclass of the ddl, intercepting the error from there hacking a fix.</p>\n\n<p>Not tidy but it worked for my needs.  I put the code up on my blog in case it\'s of help. <a href="http://david.safitech.com/?p=53" rel="nofollow noreferrer">link text</a></p>\n'}, {'answer_id': 556158, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Try this:    </p>\n\n<pre><code>if (ddl.Items.Contains(new ListItem(selectedFacility)))\n    ddl.SelectedValue = selectedFacility;\n</code></pre>\n'}, {'answer_id': 783436, 'author': 'Joshua Shannon', 'author_id': 69077, 'author_profile': 'https://Stackoverflow.com/users/69077', 'pm_score': 3, 'selected': False, 'text': "<p>I've become very fond of the following little snippet for setting DropDownList values:</p>\n\n<p><strong>For non-DataBound (eg Items added manually):</strong></p>\n\n<pre><code>ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(value));\n</code></pre>\n\n<p><strong>For DataBound</strong>:</p>\n\n<pre><code>ddl.DataBound += (o,e) =&gt; ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(value));\n</code></pre>\n\n<p>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.</p>\n"}, {'answer_id': 5623869, 'author': 'cneah', 'author_id': 621840, 'author_profile': 'https://Stackoverflow.com/users/621840', 'pm_score': 1, 'selected': False, 'text': '<p>Check this:</p>\n\n<p><a href="http://www.codeproject.com/Tips/179184/ASP-dropdownlist-missing-value-error.aspx" rel="nofollow">http://www.codeproject.com/Tips/179184/ASP-dropdownlist-missing-value-error.aspx</a></p>\n'}, {'answer_id': 7522066, 'author': 'rspring1975', 'author_id': 960073, 'author_profile': 'https://Stackoverflow.com/users/960073', 'pm_score': 0, 'selected': False, 'text': "<p>Ran into this myself.  Oddly, <code>ddl.ClearSelection();</code> didn't work.  Had to use <code>ddl.SelectedValue = null</code></p>\n\n<p>Also noticed, that this must come AFTER I clear the items from the list <code>ddl.Items.Clear();</code> which also seems weird. Setting the SelectedValue to null, then clearing the items still threw the error.</p>\n\n<p>Once this is done, re-bind the list and re-select with new value.</p>\n"}, {'answer_id': 9032372, 'author': 'HGMamaci', 'author_id': 960887, 'author_profile': 'https://Stackoverflow.com/users/960887', 'pm_score': 0, 'selected': False, 'text': '<p>I have made a workaround after having this problem very often. Unfortunate that MS still did not recovered this issue.</p>\n\n<p>Anyway, my workaround is as follows. </p>\n\n<p>1) I bind the data to the ToolTip property of the DropDownList</p>\n\n<pre><code>&lt;asp:DropDownList ID="edtDepartureIDKey" runat="server" CssClass="textbox" \nToolTip=\'&lt;%# Eval("DepartureIDKey") %&gt;\' DataSource="&lt;%# DLL1DataSource() %&gt;" DataTextField="DisplayField" DataValueField="IDKey"\nonprerender="edtDepartureIDKey_PreRender"&gt;\n</code></pre>\n\n<p>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</p>\n\n<pre><code>protected void edtDepartureIDKey_PreRender(object sender, EventArgs e)\n{\n    DropDownList ddl = (sender as DropDownList);\n    if (ddl.Items.FindByValue(ddl.ToolTip) == null)\n    {\n        //I am pulling Departure Data through the ID which is saved in ToolTip, and insert it into the 1st row of the DropDownList\n        TODepartureData v = new TODepartureData(DBSERVER.ConnStrName);\n        TODeparture d = v.Select(Convert.ToInt32(ddl.ToolTip));\n        ddl.Items.Insert(0, new ListItem(d.DeptCode, ddl.ToolTip));\n    }\n    ddl.Items.FindByValue(ddl.ToolTip).Selected = true;\n}\n</code></pre>\n\n<p><img src="https://i.stack.imgur.com/YnigI.jpg" alt="how it looks"></p>\n'}]
['https://Stackoverflow.com/questions/38612', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4096/']