13

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:

Rami Far
  • 406
  • 1
  • 10
  • 29
chobo2
  • 83,322
  • 195
  • 530
  • 832

10 Answers10

10

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
Appulus
  • 18,630
  • 11
  • 38
  • 46
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
5

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

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
David P
  • 2,027
  • 3
  • 15
  • 27
4

I realize this is an old thread, but Google brought this up as the first option for this problem - with a fairly generic search term.

Anyway, the problems I had with this and the way I solved it are as follows. The problem will either arise, like Brian Mains said for the following reasons :

  • 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

The problem i was experiencing, even though it wasn't very clear as i didn't get errors on some of the other DropDowns i was using the same method for, was that on load of the page, i was trying to use this code-behind to add an item to the 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 (which had items populated by a DataSource) :

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

Now, the problem I was getting was that the data hadn't been bound to the control at the time of Load, when i was trying to add items in the code-behing. Because I was trying to pre-select a value from the database at start-up for the user (which did exist in the list), I wasn't too bothered if the item essentially appeared twice in there.

So my work-around was the following.

I changed the code behind to the following, so that the item was added to the DropDownList on execution of the Load Event code 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 for the item which was chosen in the code-behind to stay once the page is fully loaded and not be overwritten by the DataSource, 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 is loaded, the value from the database should be pre-selected in the drop down, and all the DataSource items should also be added.

Hope this helps.

chrismason954
  • 315
  • 2
  • 8
1

I had the same problem. Here is what was going on. I had originally set the value to something that wasn't in my drop down list. If what you had originally isn't among the values you want to display on the drop down list then it won't work. For example if you originally had Fred in the data, you need to have Fred in that drop down list.

Andy
  • 55
  • 1
  • 9
1

If the dropdown controls Text property is assigned with any value before assigning the datasource, this error will occur.

Example:

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

While executing the 2nd line, you will get that error.

Noel Segura Meraz
  • 2,265
  • 1
  • 12
  • 17
Ramasubbu
  • 11
  • 2
0

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

I reset it to ddlname.items.clear() and this resolved the error.

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
0

I ran into a similar problem - the basic issue was the format of the underlying SQL database field referenced by my RadioButtonList. I converted the DB field from NCHAR to VARCHAR ... I think NCHAR adds additional content to the field which threw an error on evaluation by the RadiolButton. You may have to drop and load data that was put in as NCHAR to VARCHAR. This worked for me .. hope it works for you.

James
  • 1
0

Check whether you are setting the value of the dropdown before databinding it with a value that is not in the data bound list.

In my case this occurred like I set the value of dropdown with "ABC" and the list I was binding with dropdown has got only values {"1", "2", "3" ,"4"}

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Jobin
  • 79
  • 3
  • 8
0

I solved the same issue adding the blow extra code before binding my datasource:

ddl.Items.Insert(0, "");
ddl.SelectedValue = "";
ddl.DataSource = null;
ddl.DataBind();

As dropdownlist was getting a selected value(for whatever reason), i tried to insert a new value(here an empty string) and set it as selected one , then null value binding to refresh the datasource.It solved the issue.

the-a-monir
  • 147
  • 1
  • 10
0

My solution is before setting the datasource and binding was clearing the dropdown.

private void ClearDropdown(DropDownList ddl)
{
    ddl.Items.Clear();
    ddl.SelectedValue = null;
    ddl.Text = null;
}
Sam Salim
  • 2,145
  • 22
  • 18