Reputation: 1108
I've been having a problem lately with some specific dropdownlists in my site. I use the following class to fill some of my dropdownlists
public static void LoadDdl(string strDescription, string strValue, DataTable dtSource, DropDownList objDropDownList, bool blnAddSelect)
{
objDropDownList.DataTextField = strDescription;
objDropDownList.DataValueField = strValue;
objDropDownList.DataSource = dtSource;
objDropDownList.DataBind();
if (blnAddSelect)
{
objDropDownList.Items.Insert(0, new ListItem("Select...", "-1"));
objDropDownList.SelectedValue = "-1";
}
}
The exception is thrown when I get to the databind:
'ddlAccountTypes' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: 'ddlAccountTypes' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[ArgumentOutOfRangeException: 'ddlAccountTypes' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value]
System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) +1604222
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +107
System.Web.UI.WebControls.ListControl.PerformSelect() +34
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
Utilities.WebUtilities.LoadDdl(String strDescription, String strValue, DataTable dtSource, DropDownList objDropDownList, Boolean blnAddSelect) in C:\Users\Victor\Desktop\Clubcard\Src\Utilities\WebUtilities.cs:364
ClubCard.Administration.addClient.LoadDdls() +86
ClubCard.Administration.addClient.Page_Load(Object sender, EventArgs e) +130
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
I believe the method is correct because most of the time it works just fine. However there is one specific page that is not working because of this. The first thing that comes to mind, is that the page has a glitch which makes it enter twice to the method (!IsPostback) whenever it loads, so the second time it enters, probably the DropDownList already has defined data. So my question here is, what could be going wrong with this method or is there a way that I can turn it around?
Regards
Upvotes: 1
Views: 7642
Reputation: 682
I had to work on a similar code in which I am changing the datasource of a dropdownlist more than once in the page load. The trick was to set the SelectedValue to null instead of -1 before setting the datasource property.
I know this question was already answered but it might help others who are having the same issue.
Upvotes: 1
Reputation: 1539
It could be that inserting a ListItem in position 0 is overwriting the databound item in position 0. If the SelectedValue is actually that item that is being overwritten then you would get an error telling you that that SelectedValue is invalid.
Upvotes: 3