Has a value of SelectedValue, which is invalid because it does not exist in the list of items. How do you debug?

I'm having problems binding asp.net to a drop down list and I don’t know how to debug. I checked other questions about this on the stack, but nothing helped. As far as I can see, the "name" that he should choose is on the list.

<asp:DropDownList ID="dd1" runat="server" DataSourceID="ADataSource" DataTextField="Name" DataValueField="Name" SelectedValue='<%# Bind("Name") %>' Width="255" AppendDataBoundItems="true" TabIndex="3" Font-Size="small" EnableViewState="true" > <asp:ListItem Text="Select" Value="" /> </asp:DropDownList> 

Below is the error

System.ArgumentOutOfRangeException was not handled by the user code Message = 'dd1' has a value of SelectedValue, which is invalid because it does not exist in the list of items. Parameter name: value Source = System.Web ParamName = value Stack Trace: in System.Web.UI.WebControls.ListControl.PerformDataBinding (IEnumerable data source) in System.Web.UI.WebControls.ListControl.OnDataBinding (EventArgs e) in System .Web.UI.WebControls.ListControl.PerformSelect () in System.Web.UI.WebControls.BaseDataBoundControl.DataBind () in System.Web.UI.Control.DataBindChildren () in System.Web.UI.Control.DataBind (Boolean raiseOnDataBinding) in System.Web.UI.Control.DataBind () in System.Web.UI.Control.DataBindChildren () in System.Web.UI.Control.DataBind (Boolean raiseOnDataBinding) in System.Web.UI.Control.DataBind () in System.Web.UI.Control.DataBindChildren () in System.Web.UI.Control.DataBind (Boolean raiseOnDataBinding) in System.Web.UI.Contr ol.DataBind () in System.Web.UI.Control.DataBindChildren () in System.Web.UI.Control.DataBind (Boolean raiseOnDataBinding) in System.Web.UI.WebControls.DetailsView.CreateChildControls (IEnumerable dataSource, Boolean dataBinding) in System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding (IEnumerable data) in System.Web.UI.WebControls.DetailsView.PerformDataBinding (IEnumerable data) in System.Web.UI.WebControls.DataBoundControl.OnDataSourceVieweSenseViewe .Web.UI.DataSourceView.Select (DataSourceSelectArguments arguments, callback DataSourceViewSelectCallback) in System.Web.UI.WebControls.DataBoundControl.PerformSelect () in System.Web.UI.WebControls.BaseDataBoundControl.DataBind () in System. .WebControls.DetailsView.DataBind () in storeUpdate.GvStoresSelect_SelectedIndexCha nged (object sender, EventArgs e) on line 233 in System.EventHandler.Invoke (object sender, EventArgs e) in System.Web.UI.WebControls.GridView.OnSelectedIndexChanged (EventArgs e) in System.Web.UI.WebControls.GridView .HandleSelect (Int32 rowIndex) in System.Web.UI.WebControls.GridView.HandleEvent (EventArgs e, Boolean causeValidation, String validationGroup) in System.Web.UI.WebControls.GridView.OnBubbleEvent (object source, EventArgs e) in System. Web.UI.Control.RaiseBubbleEvent (source object, EventArgs arguments) in System.Web.UI.WebControls.GridViewRow.OnBubbleEvent (source object, EventArgs e) in System.Web.UI.Control.RaiseBubbleEvent (source object, EventArgs arguments) in System.Web.UI.WebControls.LinkButton.OnCommand (CommandEventArgs e) in System.Web.UI.WebControls.LinkButton.RaisePostBackEvent (eventArgument string) in System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent (eventArgument string) in System.Web.UiPage.iePage (IPostBackEventHandler sourceControl, String eventArgument) in System.Web.UI.Page.RaisePostBackEvent (NameValueCollection postData) in System.Web.UI.Page.ProcessRequestMain (Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsceptionAsfternerception)

+6
source share
7 answers

The value coming from <%# Bind("Name") %> , which is passed to the SelectedValue property, does not match the element in its collection. Most likely causes:

  • There are no elements in the DropDownList because the evaluation takes place before the list is bound.
  • List linked but missing value
  • Return value may be null
+6
source

I understand that this is an old thread, but Google presented this as the first option for this problem - with a fairly general search term.

In any case, the problems that I encountered, and how I solved it, are as follows. The problem will either arise, such as Brian Mines, for the following reasons:

  • There are no elements in the DropDownList because the evaluation takes place before the list is bound.
  • List linked but missing value
  • Return value may be null

The problem I encountered, although it was not very clear, because I did not get errors in some other DropDowns with which I used the same method, was that when loading the page I tried to use this code to add an element to DropDownList:

 drpNationality.Text = GlobalScript.CountDatabaseRecords("SELECT [nationality_desc] FROM [tbl_people] INNER JOIN [tbl_lkup_nationality] AS nationality ON [nationality] = [nationality_id] WHERE [person_id] ='" + Session["ID"] + "'"); 

And here is the DropDown HTML code (which contains the elements populated by the DataSource):

 <label>Nationality:</label> <asp:DropDownList ID="drpNationality" runat="server" DataSourceID="Nationality_Datasource" DataTextField="nationality_desc" DataValueField="nationality_id"> </asp:DropDownList> 

Now the problem was that the data was not bound to the control at boot time, when I tried to add elements to the code. Since I tried to pre-select the value from the database at startup for the user (who existed in the list), I was not too concerned if the item essentially appeared twice there.

So, my work was as follows.

I changed the code to the following, so the item was added to the DropDownList when the Load Event code was executed, and then selected:

 var = GlobalScript.CountDatabaseRecords("SELECT [nationality] FROM [tbl_people] INNER JOIN [tbl_lkup_nationality] AS nationality ON [nationality] = [nationality_id] WHERE [person_id] ='" + Session["ID"] + "'"); drpNationality.Items.Add(var); drpNationality.Text = var; 

But in order to ensure that the element that was selected in the code remains after the page is completely loaded and the DataSource is not overwritten, you must change the HTML to the following:

 <label>Nationality:</label> <asp:DropDownList ID="drpNationality" runat="server" DataSourceID="Nationality_Datasource" DataTextField="nationality_desc" DataValueField="nationality_id" AppendDataBoundItems="True"> </asp:DropDownList> 

Now, when the page loads, the value from the database should be preselected in the drop-down list, and all DataSource elements should also be added.

Hope this helps.

+2
source

I had the same problem. This is what happened. I initially set a value for something that wasn't on my drop-down list. If initially you were not among the values ​​that you want to display in the drop-down list, this will not work. For example, if you originally had Fred in the data, you need to have Fred in this drop-down list.

+1
source

DataBind stinks. I tried all the suggestions mentioned and none of them worked. Finished setting SelectedValue to null, clearing all items ( myDDL.Items.Clear() ), and then repeating in my list and adding new ListItems - myDDL.Items.Add(new ListItem(myListEntry.Text,myListEntry.Value))

+1
source

In my case, the dropdown list cleanup method was initially set as ddlname.text = string.empty .

I reset to ddlname.items.clear() , and this fixed the error.

0
source

If the popup menu controls the Text property is assigned with any value before the data source is assigned, this error will occur.

Example:

 StatusDropDown.Text = "some value"; StatusDropDown.DataSource = statusDatatable; 

During the execution of the second line, you will get this error.

0
source

I had a similar problem - the main problem was the format of the SQL database base field referenced by my RadioButtonList. I converted the DB field from NCHAR to VARCHAR ... I think NCHAR adds additional content to the field that triggered the error when evaluating RadiolButton. You may need to reset and load data that has been added as NCHAR to VARCHAR. It worked for me .. hope it works for you.

0
source

All Articles