Answered by:
Error

Question
-
User1754860325 posted
I get the following error:
'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
aspx.cs codeMembershipUser currentUser = Membership.GetUser(); Guid customerId = (Guid)currentUser.ProviderUserKey; DataTable dt = new DataTable();
Page.DataBind();
SqlConnection conn = new SqlConnection(@"Data Source=GMBOAT-PC\EMS_AFRICA;Integrated Security=True; User ID=xxxx; password=xxxx"); SqlCommand sqlCmd = new SqlCommand("SELECT * FROM Staff WHERE Userid = @username", conn); SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); sqlCmd.Parameters.AddWithValue("@username", customerId); sqlDa.Fill(dt); if (dt.Rows.Count > 0) { TitleDropDownList.SelectedValue = dt.Rows[0]["Title"].ToString(); txtSurname.Text = dt.Rows[0]["Firstname"].ToString(); txtFirstname.Text = dt.Rows[0]["Surname"].ToString(); txtIDNum.Text = dt.Rows[0]["IDNum"].ToString(); DropDownList1.SelectedValue = dt.Rows[0]["ESSID"].ToString(); txtStaffNum.Text = dt.Rows[0]["ESNumber"].ToString(); }In my aspx page I have a few dropdownlists and when ever I change one of the dropdowns it gives me the above
error message.Tuesday, July 30, 2013 6:30 PM
Answers
-
User1143442848 posted
I get the following error:
'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
Hi gmboat,
The reason of the error is that the value given to DropDownList1.SelectedValue is not in the ListItem of the DropDownList.
So please try to check the
TitleDropDownList.SelectedValue = dt.Rows[0]["Title"].ToString();
and
DropDownList1.SelectedValue = dt.Rows[0]["ESSID"].ToString();
Here is an example about how to set the SelectedValue and it works well in my local machine.
Code in .aspx
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList> </div> </form> </body> </html>
code in .cs
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DropDownList ddlName = (DropDownList)this.FindControl("DropDownList1"); ddlName.Items.Add("1234"); ddlName.Items.Add("Lisa"); ddlName.Items.Add("Zhang"); ddlName.SelectedValue = "1234"; } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { DropDownList ddlName = (DropDownList)this.FindControl("DropDownList1"); ddlName.SelectedValue = "Zhang"; }
Best Regards,
Lisa Zhang
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 1, 2013 4:07 AM
All replies
-
User653228039 posted
Well, based on the wording of the error, I would assume that you are setting DropDownList1.SelectedValue to an invalid value because it doesn't exist in the list of items...
Possibly this line:DropDownList1.SelectedValue = dt.Rows[0]["ESSID"].ToString();
Are you sure that the values you are getting from the ESSID column of that table are in the DropDownList1's list of items? Value, not Text...
Tuesday, July 30, 2013 6:39 PM -
User1754860325 posted
Yes I am sure. It gave no errors when I only had the top 2 rows of dropdowns and text boxes to display info of person who is loged in.
The rest of the page which I had done after testing the top 2 rows is for measurments of the user that I am working on now is now giving the error once I click on the save button.
Tuesday, July 30, 2013 6:43 PM -
User653228039 posted
Where do you populate the DropDownList with items? I know I used to have problems with getting their value (even though it appears you are trying to set its value) in events until I realized Page_Load executes before every event handler.
Tuesday, July 30, 2013 7:13 PM -
User1143442848 posted
I get the following error:
'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
Hi gmboat,
The reason of the error is that the value given to DropDownList1.SelectedValue is not in the ListItem of the DropDownList.
So please try to check the
TitleDropDownList.SelectedValue = dt.Rows[0]["Title"].ToString();
and
DropDownList1.SelectedValue = dt.Rows[0]["ESSID"].ToString();
Here is an example about how to set the SelectedValue and it works well in my local machine.
Code in .aspx
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList> </div> </form> </body> </html>
code in .cs
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DropDownList ddlName = (DropDownList)this.FindControl("DropDownList1"); ddlName.Items.Add("1234"); ddlName.Items.Add("Lisa"); ddlName.Items.Add("Zhang"); ddlName.SelectedValue = "1234"; } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { DropDownList ddlName = (DropDownList)this.FindControl("DropDownList1"); ddlName.SelectedValue = "Zhang"; }
Best Regards,
Lisa Zhang
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 1, 2013 4:07 AM -
User-1091252720 posted
This error occurs when you assign value to SelectedValue property which doen't exist either of value property of ListItems in DropDownList. In order to resolve this, I think that you should create static extension or any useful method to handle checking the existence. Here you can try static extension as following code:
public static void SetSelectedValue(this DropDownList dropDownList, string selectedValue) { if (dropDownList != null) { var listItem = dropDownList.Items.FindByValue(selectedValue ?? String.Empty); if (listItem != null) { /***************************************** * Should clear selection first to prevent error "Multple Selection" can occur *****************************************/ dropDownList.ClearSelection(); listItem.Selected = true; } } }
Then, use it in your code as follows:
if(dt.Rows[0]["Title"] != null) { TitleDropDownList.SetSelectedValue(dt.Rows[0]["Title"].ToString()); } if(dt.Rows[0]["ESSID"] != null) { DropDownList1.SetSelectedValue(dt.Rows[0]["ESSID"].ToString()); }
Hope this will help you!!
Thursday, August 1, 2013 6:10 AM