SelectedValue, which is invalid because it does not exist in the list of items

This is the error I get:

ddlRankEdit 'has a SelectedValue value, which is not valid because it does not exist in the list of items. Parameter Name: Value

I have a form with several dropdownlists that are nested in a panel that is set to invisible by default. When the user selects an entry from a separate list, the selected event with a modified index sets the visibility of the panel and makes a data call. This happens when an error occurs. See the code below, I added XXX where it throws.

<asp:DropDownList runat="server" ID="ddlRankEdit" CssClass="txtfield" DataSourceID="ODCRanks" DataTextField="Rank" DataValueField="ID" AppendDataBoundItems="True"> <asp:ListItem Text="--- Select a Rank ---" Value="-1" /> </asp:DropDownList> <asp:ObjectDataSource ID="ODCRanks" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetRanks" TypeName="RanksTableAdapters.RankTableAdapter"></asp:ObjectDataSource> protected void lboxManageMembers_SelectedIndexChanged(object sender, EventArgs e) { pnlReviewMemberDetails.Visible = false; pnlUnlockUserAccount.Visible = false; pnlAdmins.Visible = false; pnlCreateAdmins.Visible = false; lblNote.Visible = false; pnlManageMenbers.Visible = true; MembershipUser user = Membership.GetUser(); DataSetTableAdapters.MemberInfoTableAdapter da = new DataSetTableAdapters.MemberInfoTableAdapter(); Guid _memberId = Guid.Empty; _memberId = new Guid(lbxManageMembers.SelectedValue); DataSet.MemberInfoDataTable dt = da.GetMember(_memberId); if (dt.Rows.Count == 1) { DataSet.MemberInfoRow mr = dt[0]; XXX ddlRankEdit.SelectedValue = Convert.ToString(mr.rankid); XXX ddlPatrolEdit.SelectedValue = Convert.ToString(mr.patrolid); XXX ddlPositionEdit.SelectedValue = Convert.ToString(mr.bsaposid); txtFirstNameEdit.Text = mr.firstname; txtLastNameEdit.Text = mr.lastname; txtEmailEdit.Text = user.Email; txtAddressEdit.Text = mr.address; txtPhoneEdit.Text = mr.phone; txtCellPhoneEdit.Text = mr.altphone; txtAltEmailEdit.Text = mr.altemail; txtMotherFirstNameEdit.Text = mr.parentfn; txtMotherLastNameEdit.Text = mr.parentln; txtMotherWorkPhoneEdit.Text = mr.parentworkphone; txtMotheHomePhoneEdit.Text = mr.parentworkphone; txtMotherCellkPhoneEdit.Text = mr.parentscellphone; txtMotherTwitterEdit.Text = mr.parenttwitter; txtMotherEmailEdit.Text = mr.parentemail; txtMotherAltEmailEdit.Text = mr.parentemailalt; txtFatherFirstNameEdit.Text = mr.parent2fn; txtFatherLastNameEdit.Text = mr.parent2ln; txtFatherWorkPhoneEdit.Text = mr.parent2workphone; txtFatherHomePhoneEdit.Text = mr.parent2workphone; txtFatherCellPhoneEdit.Text = mr.parent2cellphone; txtFatherTwitterEdit.Text = mr.parent2twitter; txtFatherEmailEdit.Text = mr.parent2email; txtFatherAltEmailEdit.Text = mr.parent2emailalt; } } 
+4
source share
3 answers

The error message tells you exactly what is happening: the value, for example, stored in mr.rankid, is not in the drop-down list.

You need to find out if the drop-down list contains the correct value or the value you are trying to assign does not exist in the list of available values.

Update

Since the visibility of the panel, which appears to be causing problems, it would be better to hide the panel using CSS than setting the Visible property to false, which would prevent it from being displayed on the page.

This can be done with code similar to the following in code:

 Panel1.Style.Add(HtmlTextWriterStyle.Visibility, "Hidden"); Panel1.Style.Add(HtmlTextWriterStyle.Display, "None"); 
+3
source

change your code as follows:

 if (dataTable1.Rows[0]["columnName"].ToString() != "" && dataTable1.Rows[0]["columnName"] != null) DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(dataTable1.Rows[0]["columnName"].ToString())); 
+1
source

you can use these codes to use dropdownlist in the editemplate when you do not need to use a data source:

 <asp:TemplateField HeaderText="state" SortExpression="state"> <EditItemTemplate> <asp:DropDownList ID="DropDownList4" runat="server" Style="position: relative" AppendDataBoundItems="true" SelectedValue='<%# Bind("state") %>' > <asp:ListItem Value="approved">approved</asp:ListItem> <asp:ListItem Value="notapproved">notapproved</asp:ListItem> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("state") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> 
+1
source

Source: https://habr.com/ru/post/1391025/


All Articles