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

24,182

Solution 1

After scratching my head for several hours, finally, I found the elegant solution for the trailing spaces problem.

In my Table definition, In Country field definition I was using nchar(100) instead of nvarchar(100). The former always has 100 characters (thus the trailing spaces), the later can have up to 100 characters, but doesn't fill up the space (thus matches the item in DropDownlist).

In nutshell, <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("Country")%>'> is working fine now.

I hope it helps future readers.

Solution 2

"So, one solution is to remove the spaces (Trim) from String so that value get matched in DropDownList fields."

I'm a little confused as to why this would make a difference.

Could you show us the raw HTML which this DropDownList control has created, and which row you're attempting to edit.

To view the raw HTML..

  • Open your webpage in Chrome
  • Hit F12 to show Developer Options
  • Now, right-click on the Drop Down List on your webpage, and select "Inspect Element"
  • In the Elements tab of Chrome's Developer window, you'll see a select element, which ASP.Net has created for you. Expand this element, to see the option elements.
  • Have a look down the value attributes for each of these.

You should see a more complicated version of this...

<select id="listOfPeople">
   <option value=""></option>
   <option value="10">Mike</option>
   <option value="17">Geoffery</option>
   <option value="18">Edward</option>
</select>

... and the error you're seeing suggests that when you click on one of the rows, its value isn't matching one of the values listed in the options.

Update

After looking at your .aspx file, I see that you display a list of Country names, but none have a Value:

<asp:DropDownList ID="DropDownList1" runat="server"  SelectedValue='<%# Eval("Country").ToString().Trim()%>'>
    <asp:ListItem>Select Country</asp:ListItem>
    <asp:ListItem>India</asp:ListItem>
    <asp:ListItem>USA</asp:ListItem>
    <asp:ListItem>UK</asp:ListItem>
    <asp:ListItem>China</asp:ListItem>
    <asp:ListItem>North Korea</asp:ListItem>
    <asp:ListItem>Kazakhstan</asp:ListItem>
</asp:DropDownList>

Shouldn't they look like this...

<asp:ListItem Value="North Korea">North Korea</asp:ListItem>

...rather than this...?

<asp:ListItem>North Korea</asp:ListItem>

(Again - use Chrome to check whether your drop down list of country names do have a Value in them. If they don't, this will explain the error you're seeing.)

Update 2

I'm stealing the following tip from one of the many other StackOverflow questions which have asked this same question:

Try adding the following into your edittemplate to see the current value:

<asp:Label ID="lblCountryName" runat="server" Text='<% #Bind("Country") %>'></asp:Label>

Taken from here

This should tell you the Value it's trying to look for, and cannot find, in the list of option items, created by your DropDownList control.

Share:
24,182
GorvGoyl
Author by

GorvGoyl

Personal site &amp; blog: https://gourav.io CV: https://gourav.io/cv

Updated on July 09, 2022

Comments

  • GorvGoyl
    GorvGoyl almost 2 years

    I'm using TemplateField in GridView to implement edit/delete from database.

    I'm Querying Data with the SqlDataSource Control.

    when I edit the Table from page I get following error:

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

    This is due to the data from dB is not matching any of the DropDownList Country name.

    I understand the problem from this question (solution is not given there!)

    I think when I insert data to dB, it automatically adds redundant spaces to data(like name of country)

    So, one solution is to remove the spaces (Trim) from String so that value get matched in one of the DropDownList items.

    because <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Eval("Country")%>'
    doesn't match any list item of DropDownList

    but

    <asp:DropDownList ID="DropDownList1" runat="server"  SelectedValue='<%# Eval("Country").ToString().Trim()%>'>
    

    matches the list item of DropDownList.

    Now I want to update the data back to the database but instead of country name null value get stored. (I think it is the limitation of Eval())

    I can update the data back to the dB using Bind() but Bind doesn't support ToString().Trim() so I can't trim the string to match one of the items from DropDownList.

    How can I update back the data to dB?

    my raw code