Ryan O'Hara Ryan O'Hara - 1 year ago 102
ASP.NET (C#) Question

DataBinding asp.net DropDownList from a List of ListItems causing System.ArgumentOutOfRangeException Error

I am attempting to DataBind an asp:DropDownList with a Collections.Generic.List of System.Web.UI.WebControls.ListItems. The DataBind() is throwing this error.

System.ArgumentOutOfRangeException: 'ddlPlatinumOptions' has a SelectedValue which is invalid because it does not exist in the list of items.


<asp:DropDownList ID="ddlPlatinumOptions" runat="server" AutoPostBack="true" width="100%"></asp:DropDownList>


public void BindPlatinumOptions()
ddlPlatinumOptions.DataTextField = "Text";
ddlPlatinumOptions.DataValueField = "Value";
ddlPlatinumOptions.DataSource = _platinumOptions;
ddlPlatinumOptions.DataBind(); // Throwing Error


MattressProtectionInfo standard = RF_ProtectionPlan.GetMattressPlanInfo(MattressId, false);
MattressProtectionInfo premium = RF_ProtectionPlan.GetMattressPlanInfo(MattressId, true);
List<ListItem> plans = new List<ListItem>();
if (standard != null)
plans.Add(new ListItem(standard.Price.ToString("C") + " - Standard 5-Year Platinum Protection", standard.ProductID.ToString()));
if (premium != null)
plans.Add(new ListItem(premium.Price.ToString("C") + " - Premium 5-Year Platinum Protection", premium.ProductID.ToString()));

_view.PlatinumOptions = plans;

Data Example

  • Value = "21696" Text = "$99.95 - Standard 5-Year Platinum Protection"

  • Value = "21702" Text = "$119.95 - Premium 5-Year Platinum Protection"

Thing I have tried

  • Nulling datasource and Databinding before my data to clear out anything (broke on dataBind as well)

  • relocating position of DataTextField and DataValueField (waste of time - no change)

  • declaring a selected index of 0 before the databind

  • ddlPlatinumOptions.Items.Clear();

  • ddlPlatinumOptions.ClearSelection();

I am grabbing at straws. It appears as if the databind is trying to select something inside of the dropdownlist that isn't there.

Is there an error in my code I'm not seeing? Any Ideas?

Answer Source

Well, it's an out of range exception.. Are you setting the SelectedIndex to a default value at any point before you are binding to the DropDownList?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download