Search code examples
asp.netdata-binding

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


I am having problems with asp.net binding to a dropdownlist and I have no clue on how to debug. I checked out the other questions about this on stack but nothing has helped. As far as I can see the "name" it should select is in 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>

Following is the error

System.ArgumentOutOfRangeException was unhandled by user code Message='dd1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value Source=System.Web ParamName=value StackTrace: at System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) at System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) at System.Web.UI.WebControls.ListControl.PerformSelect() at System.Web.UI.WebControls.BaseDataBoundControl.DataBind() at System.Web.UI.Control.DataBindChildren() at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) at System.Web.UI.Control.DataBind() at System.Web.UI.Control.DataBindChildren() at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) at System.Web.UI.Control.DataBind() at System.Web.UI.Control.DataBindChildren() at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) at System.Web.UI.Control.DataBind() at System.Web.UI.Control.DataBindChildren() at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) at System.Web.UI.WebControls.DetailsView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) at System.Web.UI.WebControls.DetailsView.PerformDataBinding(IEnumerable data) at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) at System.Web.UI.WebControls.DataBoundControl.PerformSelect() at System.Web.UI.WebControls.BaseDataBoundControl.DataBind() at System.Web.UI.WebControls.DetailsView.DataBind() at storeUpdate.GvStoresSelect_SelectedIndexChanged(Object sender, EventArgs e) in line 233 at System.EventHandler.Invoke(Object sender, EventArgs e) at System.Web.UI.WebControls.GridView.OnSelectedIndexChanged(EventArgs e) at System.Web.UI.WebControls.GridView.HandleSelect(Int32 rowIndex) at System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) at System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) at System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) at System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) InnerException:


Solution

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

    • DropDownList has no items because the evaluation happens before the list gets bound
    • The list is bound but is missing this particular value
    • The value returned could be null