Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I keep getting the above error in the title line and it makes no sense, because I am using a sample table with only 5 records and each record has a value as per the drop down menu.

This is my code used to declare the drop down list. I have joined two tables in my SQL data source to reflect what I want populated in the grid view and have hidden columns as necessary. I am using the same data source to populate the drop down list any help would be most appreciated

<asp:DropDownList ID="DropDownListCurrency" runat="server" 
                  CausesValidation="True" DataSourceID="GridView" 
                  DataTextField="Currency" DataValueField="Currency_ID" 
                  AppendDataBoundItems="True"> 

          <asp:ListItem Value="0" Text="&lt;Select&gt;" Enabled="True"></asp:ListItem>
</asp:DropDownList>

enter image description here

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    try 
    {
        GridViewRow row = GridView1.SelectedRow; 

        AccountNumber.Text = (string)row.Cells[0].Text;
        ....

        DropDownListCurrency.SelectedValue = (string)row.Cells[8].Text;
    }
    catch (Exception ex)
    {
        Console.WriteLine("{0} Exception caught.", ex);
    }        
}
like image 710
New2This Avatar asked Nov 04 '13 14:11

New2This


People also ask

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

'DropDownList' has a SelectedValue which is invalid because it does not exist in the list of items. As it is possible for the user to change the values of the list (in a codetable) it is possible that an earlier selected item not exists anymore.

Which control is used for a DropDownList?

Certify and Increase Opportunity. The DropDownList Web server control allows users to select an item from a predefined list. It differs from the ListBox Web server control in that the list of items remains hidden until users click the drop-down button.

How do I make a drop-down list not selectable?

We use <select> and <option> elements to create a drop-down list and use disabled attribute in <select> element to disable the drop-down list element. A disabled drop-down list is un-clickable and unusable.


1 Answers

Attempt to find the value in the drop down list before attempting to set the SelectedValue, like this:

if (DropDownListCurrency.Items.FindByValue(row.Cells[8].Text.ToString().Trim()) != null)
{
    DropDownListCurrency.SelectedValue = row.Cells[8].Text.ToString().Trim();
}

Note: The Trim() call will remove any leading or trailing spaces in your text box text, which could be a cause for a match not being found.

So your full code should be this:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    try 
    {
        GridViewRow row = GridView1.SelectedRow; 

        AccountNumber.Text = (string)row.Cells[0].Text;
         ....

        if (DropDownListCurrency.Items.FindByValue(row.Cells[8].Text.ToString().Trim()) != null)
        {
            DropDownListCurrency.SelectedValue = row.Cells[8].Text.ToString().Trim();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("{0} Exception caught.", ex);
    }
}
like image 76
Karl Anderson Avatar answered Oct 14 '22 09:10

Karl Anderson