locked
'DropDownList2' has a SelectedValue which is invalid because it does not exist in the list of items RRS feed

  • Question

  • User-501297529 posted

    I received this error when I try to edit the Form View. It is a dropdown list for States. I thought adding AppendDataBoundItems="true" and that doesn't help. I need assistance on what I can do to fix this error. here is the code for the dropdownlist:

        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" AppendDataBoundItems="true" 
                        CssClass="style12" Font-Names="Arial Unicode MS" Font-Size="Small" 
                        SelectedValue='<%# Bind("STATE") %>' TabIndex="10" Width="53px">
                        <asp:ListItem></asp:ListItem>
                        <asp:ListItem>AL</asp:ListItem>
                        <asp:ListItem>AZ</asp:ListItem>
                        <asp:ListItem>AK</asp:ListItem>
                        <asp:ListItem>CA</asp:ListItem>
                        <asp:ListItem>AR</asp:ListItem>
                        <asp:ListItem>CT</asp:ListItem>
                        <asp:ListItem>CO</asp:ListItem>
                        <asp:ListItem>DC</asp:ListItem>
                        <asp:ListItem>DE</asp:ListItem>
                        <asp:ListItem>GA</asp:ListItem>
                        <asp:ListItem>FL</asp:ListItem>
                        <asp:ListItem>ID</asp:ListItem>
                        <asp:ListItem>HI</asp:ListItem>
                        <asp:ListItem>IN</asp:ListItem>
                        <asp:ListItem>IL</asp:ListItem>
                        <asp:ListItem>KS</asp:ListItem>
                        <asp:ListItem>IA</asp:ListItem>
                        <asp:ListItem>LA</asp:ListItem>
                        <asp:ListItem>KY</asp:ListItem>
                        <asp:ListItem>MD</asp:ListItem>
                        <asp:ListItem>ME</asp:ListItem>
                        <asp:ListItem>MI</asp:ListItem>
                        <asp:ListItem>MA</asp:ListItem>
                        <asp:ListItem>MS</asp:ListItem>
                        <asp:ListItem>MN</asp:ListItem>
                        <asp:ListItem>MT</asp:ListItem>
                        <asp:ListItem>MO</asp:ListItem>
                        <asp:ListItem>NV</asp:ListItem>
                        <asp:ListItem>NE</asp:ListItem>
                        <asp:ListItem>NJ</asp:ListItem>
                        <asp:ListItem>NH</asp:ListItem>
                        <asp:ListItem>NY</asp:ListItem>
                        <asp:ListItem>NM</asp:ListItem>
                        <asp:ListItem>ND</asp:ListItem>
                        <asp:ListItem>NC</asp:ListItem>
                        <asp:ListItem>OK</asp:ListItem>
                        <asp:ListItem>OH</asp:ListItem>
                        <asp:ListItem>PA</asp:ListItem>
                        <asp:ListItem>OR</asp:ListItem>
                        <asp:ListItem>SC</asp:ListItem>
                        <asp:ListItem>RI</asp:ListItem>
                        <asp:ListItem>TN</asp:ListItem>
                        <asp:ListItem>SD</asp:ListItem>
                        <asp:ListItem>UT</asp:ListItem>
                        <asp:ListItem>TX</asp:ListItem>
                        <asp:ListItem>VA</asp:ListItem>
                        <asp:ListItem>VT</asp:ListItem>
                        <asp:ListItem>WV</asp:ListItem>
                        <asp:ListItem>WA</asp:ListItem>
                        <asp:ListItem>WY</asp:ListItem>
                        <asp:ListItem>WI</asp:ListItem>
                    </asp:DropDownList>
    Friday, July 15, 2011 4:34 PM

Answers

  • User-1856974186 posted

    The error is because the bound value (ie Bind("STATE")) doesn't match any of the values in the list. If the value is empty it should match the first entry, the blank one. Try changing this to:

      <asp:ListItem Value="" Text="-- select state --" />

    That shouldn't change anything, since the value should be the same, but if it doesn't work then at least it's been eliminated.

    Next step would be to work out what the value being return is? If it's from SQL then it could be null, in which case you could coalesce it to an empty string. Eg, in your query have:

      SELECT ... COALESCE(STATE, '') AS STATE, ...

    Coalesce returns the first non-null value, so if state is null it will return the empty string. That should then match.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 18, 2011 5:10 AM

All replies

  • User-699953111 posted

    The error ocurrs because the value on selectedvalue is incorrect. Maybe the value not exists or is null. Set the value on code-behind for best debug.

    Friday, July 15, 2011 4:47 PM
  • User-501297529 posted

    I don't understand what you are trying to say. The value on the selected value is correct. I get this error when a record has been entered that has no value entered for State and then when you go back to Edit in the FormView the error occurs.

    Friday, July 15, 2011 5:47 PM
  • User-1177472553 posted
     SelectedValue='<%# Bind("STATE") %>'
    how many values are present in the binding Data source ?? 
    if it has only 1 value, verify the value in the "State"  property 
    of ur data source object and check if the same is present in the dropdownlist .. and that is what the error says

    Friday, July 15, 2011 10:57 PM
  • User-501297529 posted

    OK, so you are saying that there are more values in the database than the 50 states I have in the dropdown and that is what is causing the error? Is it better to change that field to a textbox or remove all of the hard coded state values and let it bind to the database?

    Saturday, July 16, 2011 11:44 AM
  • User-501297529 posted

    Anyone with any ideas on this.

    Sunday, July 17, 2011 5:51 PM
  • User-1856974186 posted

    The error is because the bound value (ie Bind("STATE")) doesn't match any of the values in the list. If the value is empty it should match the first entry, the blank one. Try changing this to:

      <asp:ListItem Value="" Text="-- select state --" />

    That shouldn't change anything, since the value should be the same, but if it doesn't work then at least it's been eliminated.

    Next step would be to work out what the value being return is? If it's from SQL then it could be null, in which case you could coalesce it to an empty string. Eg, in your query have:

      SELECT ... COALESCE(STATE, '') AS STATE, ...

    Coalesce returns the first non-null value, so if state is null it will return the empty string. That should then match.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 18, 2011 5:10 AM
  • User-699953111 posted

    I try to say, that the value is binding is not valid, because the value is null or the value is not exists

    For example, the datasource get value 'PP' thats not exists in your combo. This crash ur selectedValue

    Monday, July 18, 2011 9:56 AM