Risho
Risho

Reputation: 2647

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

This is the error I get:

ddlRankEdit' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value

I have a form wiht several dropdownlists that are nested in a panel that is by default set to invisible. When a user selects a record from a separate list box the selected index changed event sets the panel to visible and makes a data call. That's when the error happens. See the code below, I added XXX where it stalls.

<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;
    }

}

Upvotes: 4

Views: 14364

Answers (3)

Safavi
Safavi

Reputation: 344

you can use this codes for use dropdownlist in editemplate when you dont need to use a datasource :

<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>

Upvotes: 1

yalameenmca
yalameenmca

Reputation: 31

modify your code like this:

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

Upvotes: 1

competent_tech
competent_tech

Reputation: 44931

The error message is telling you exactly what is happening: the value, for example stored in mr.rankid, is not present in the dropdownlist.

You need to figure out whether or not the dropdownlist contains the correct value or the value you are trying to assign does not exist in the list of available values.

Update

Since it is the visibility of the containing panel that seems to be causing the problems, it would be better to hide the panel using CSS than setting the Visible property to false, which will prevent it from rendering to the page.

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

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

Upvotes: 3

Related Questions