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

Question
-
User-839555754 posted
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?Tuesday, November 15, 2005 10:52 AM
Answers
-
User1709878158 posted
'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.- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 15, 2005 1:34 PM -
User314722437 posted
I much better way than catching an exception is to first check if your value exists in the dropdownlist on databound.
Bartek
I had try the link, but is not work. the code still jump to the exception handling
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, February 10, 2010 12:43 AM -
User1847364595 posted
This worked for us:
protected void PreventErrorOnbinding(object sender, EventArgs e) { DropDownList theDropDownList = (DropDownList)sender; theDropDownList.DataBinding -= new EventHandler(PreventErrorOnbinding); theDropDownList.AppendDataBoundItems = true; ListItem li = new ListItem("Make a selection >>",""); theDropDownList.Items.Insert(0, li); try { theDropDownList.DataBind(); } catch (ArgumentOutOfRangeException) { theDropDownList.SelectedValue = ""; } }
On the control(s) you add ondatabinding="PreventErrorOnbinding"Since the selected value is set to "" it always selects the firts item in the dropdownlist.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, March 18, 2010 9:55 AM
All replies
-
User1709878158 posted
'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.- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 15, 2005 1:34 PM -
User-839555754 posted
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);
}}
Tuesday, November 15, 2005 3:14 PM -
User-1067288845 posted
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'
Wednesday, August 9, 2006 11:59 PM -
User-1153868280 posted
Can you elaborate where this code goes, in the codebehind or wired to a particular event?Wednesday, January 3, 2007 5:39 PM -
Saturday, January 27, 2007 2:14 AM
-
User-2062274770 posted
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).
Tuesday, January 30, 2007 3:47 PM -
User1292384883 posted
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.
Wednesday, April 11, 2007 3:12 PM -
User-1969155842 posted
Yeah, this solution works and I'm using it, but I think it's "not the fastest" way to achieve this by catching exception.
I had already found an solution for my problem: Override the DataBinding method and catch the exception.
Thursday, August 7, 2008 2:34 AM -
User-2109426844 posted
I much better way than catching an exception is to first check if your value exists in the dropdownlist on databound.
Bartek
Sunday, August 17, 2008 12:16 AM -
User2113927143 posted
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...Monday, June 29, 2009 9:44 AM -
User-353618039 posted
Where did you put this code? An event of the ddl or somewhere else?
Thursday, July 2, 2009 3:19 PM -
User293174396 posted
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
Thursday, July 2, 2009 3:39 PM -
User1292384883 posted
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.
Friday, September 11, 2009 1:28 PM -
User-1214621198 posted
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>Monday, September 28, 2009 12:08 PM -
User314722437 posted
I much better way than catching an exception is to first check if your value exists in the dropdownlist on databound.
Bartek
I had try the link, but is not work. the code still jump to the exception handling
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, February 10, 2010 12:43 AM -
User1847364595 posted
This worked for us:
protected void PreventErrorOnbinding(object sender, EventArgs e) { DropDownList theDropDownList = (DropDownList)sender; theDropDownList.DataBinding -= new EventHandler(PreventErrorOnbinding); theDropDownList.AppendDataBoundItems = true; ListItem li = new ListItem("Make a selection >>",""); theDropDownList.Items.Insert(0, li); try { theDropDownList.DataBind(); } catch (ArgumentOutOfRangeException) { theDropDownList.SelectedValue = ""; } }
On the control(s) you add ondatabinding="PreventErrorOnbinding"Since the selected value is set to "" it always selects the firts item in the dropdownlist.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, March 18, 2010 9:55 AM -
User1285153939 posted
@justbourlier - you saved my day ! Using an empty item works like a charm ! Thank you so much !
Thursday, June 10, 2010 8:01 AM -
User918508837 posted
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>The best solution that I've seen...
Monday, September 6, 2010 6:11 PM -
User-1118420479 posted
Try this:
ListItem lstItem = ddlItems.Items.FindByValue(value);
if (lstItem != null)
ddlItems.SelectedValue = lstItem.Value;Thursday, October 28, 2010 10:58 AM -
User587896088 posted
Try this hope it will Solve your problem
ListItem item = ddlWCCode.Items.FindByValue(employmentDetail.WCCodeID.ToString());
if (item != null)
{
ddlWCCode.SelectedIndex = ddlWCCode.Items.IndexOf(item);
}
else
{
ddlWCCode.SelectedValue = "0";
}
Friday, April 8, 2011 4:57 AM -
User-1068921096 posted
check this
http://www.codeproject.com/Tips/179184/ASP-dropdownlist-missing-value-error.aspx
Monday, April 11, 2011 11:42 AM -
User-1721781818 posted
Hey,
I am using a DropDownList in a ListView Edit Template with the following code:
<asp:DropDownList ID="ModuleType" runat="server" OnDataBinding="PreventErrorOnbinding"
DataSourceID="ModuleTypeDataSource" DataTextField="Type" OnDataBound="FixDuplicateBinding"
DataValueField="ModuleTypeID" SelectedValue='<%# Bind("ModuleTypeID") %>'>
</asp:DropDownList>
I also use a linqdatasource with a WhereParameter to query:
<asp:LinqDataSource ID="ModuleTypeDataSource" runat="server" ContextTypeName="TrainingDB.TrainingDBLinqDataClassDataContext"
TableName="ModuleTypes" Where="IsDeleted == @IsDeleted" >
<WhereParameters>
<asp:Parameter DefaultValue="false" Name="IsDeleted" Type="Boolean" />
</WhereParameters>
</asp:LinqDataSource>If a ModuleType is deleted, the exception occurs and our Please select item is added and selected.
With or without exception we have duplicate data in the control as we bind twice.
I was able to remove the duplicate data by modifying the previous posted code this way:private string selectedValue; protected void PreventErrorOnbinding(object sender, EventArgs e) { DropDownList theDropDownList = (DropDownList)sender; theDropDownList.DataBinding -= new EventHandler(PreventErrorOnbinding); theDropDownList.AppendDataBoundItems = true; selectedValue = ""; try { theDropDownList.DataBind(); } catch (ArgumentOutOfRangeException) { theDropDownList.Items.Clear(); theDropDownList.Items.Insert(0, new ListItem("Please select", "")); theDropDownList.SelectedValue = ""; } }
protected void FixDuplicateBinding(object sender, EventArgs e) { DropDownList theDropDownList = (DropDownList)sender; selectedValue = theDropDownList.SelectedValue; var distinctItems = (from i in theDropDownList.Items.Cast<ListItem>() group i by i.Value into g select new ListItem { Text = g.First().Text, Value = g.First().Value }).ToArray(); theDropDownList.Items.Clear(); theDropDownList.Items.AddRange(distinctItems); theDropDownList.SelectedValue = selectedValue; }
We store the SeletedValue in a variable, use linq to get all distinct ListItems, then clear the list and add the items back in.
Thursday, July 14, 2011 12:45 PM