locked
"SelectedValue which is invalid because it does not exist in the list of items" Error Message RRS feed

  • Question

  • User-977162190 posted

    I'm struggling to get my DetailsView to work. It contains a dropdownbox in a Template Field so that the user can choose from a list of suppliers in the database. Slight added complication is that I want to display the full name of the supplier but write back the short supplier code to the database during update.

     Here is my code for the DetailsView:

     

    <asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSourceDetails" 
    AutoGenerateRows="False" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" 
    DataKeyNames="ID" Visible="False">
    <Fields>
        <asp:BoundField HeaderText="ID" DataField="ID" ReadOnly="True"/>
        <asp:BoundField HeaderText="Name" DataField="NAME" SortExpression="NAME" />
        <asp:BoundField HeaderText="Description" DataField="DESCRIPTION" />
        <asp:BoundField HeaderText="CAS No" DataField="CAS_NO" />
        <asp:BoundField HeaderText="IUPAC Name" DataField="IUPAC_NAME" />
        <asp:BoundField HeaderText="VOC" DataField="VOC" />    
        <asp:TemplateField HeaderText="Supplier">
            <ItemTemplate><%# Eval("SUPPLIER") %></ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownListSuppliers" runat="Server" DataSourceID="SqlDataSourceSuppliers" 
                DataTextField="DESCRIPTION" DataValueField="ID" SelectedValue='<%# Bind("SUPPLIER") %>'>
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>        
        <asp:BoundField HeaderText="Supplier Code" DataField="SUPPLIER_CODE" />
        <asp:BoundField HeaderText="Created" DataField="CREATED" ReadOnly="True"/>
        <asp:TemplateField HeaderText="Physical State">
            <ItemTemplate><%#Eval("PHYSICAL_STATE")%></ItemTemplate>           
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownListPhysStates" runat="Server" DataSourceID="SqlDataSourcePhysStates" 
                DataTextField="DESCRIPTION" DataValueField="ID" SelectedValue='<%# Bind("PHYSICAL_STATE") %>'></asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="Alarm Level" DataField="ALARM_LEVEL" />
    </Fields>
    </asp:DetailsView>

      Here are the SqlDataSources referenced in the code above:

     

    <asp:SqlDataSource 
        ID="SqlDataSourceDetails" 
        runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionStringJanus %>"
        ProviderName="<%$ ConnectionStrings:ConnectionStringJanus.ProviderName %>"  
        FilterExpression="ID = '{0}'"
        DeleteCommand="update substances set void = 'Y' where id = :ID"
        SelectCommand="select s.id, s.substance_name as name, s.description, s.cas_no, su.description as supplier, s.supplier_code, 
                       to_char(s.date_created, 'DD-MON-YYYY') as created, ps.description as physical_state, s.alarm_level, s.voc, s.iupac_name
                       from substances s, phys_states ps, suppliers su
                       where s.physical_state = ps.id
                       and su.id = s.supplier_id
                       and s.void = 'N'"
         UpdateCommand="update substances set substance_name = :name, description = :description, cas_no = :cas_no, 
                       supplier_id = :supplier, supplier_code = :supplier_code, physical_state = :physical_state, 
                       alarm_level = :alarm_level, iupac_name = :iupac_name, voc = :voc where id = :id">
    <UpdateParameters>
        <asp:Parameter Name="ID" Type="string" />
        <asp:Parameter Name="NAME" Type="string" />
        <asp:Parameter Name="DESCRIPTION" Type="string" />
        <asp:Parameter Name="CAS_NO" Type="string" />
        <asp:ControlParameter Name="SUPPLIER" ControlID="DropDownListSuppliers" PropertyName="SelectedValue" Type="string" />
        <asp:ControlParameter Name="PHYSICAL_STATE" ControlID="DropDownListPhysStates" PropertyName="SelectedValue" Type="string" />
        <asp:Parameter Name="SUPPLIER_CODE" Type="string" />
        <asp:Parameter Name="PHYSICAL_STATE" Type="string" />
        <asp:Parameter Name="ALARM_LEVEL" Type="string" />
        <asp:Parameter Name="IUPAC_NAME" Type="string" />
        <asp:Parameter Name="VOC" Type="string" />
    </UpdateParameters>
    <DeleteParameters>
        <asp:Parameter Name="ID" Type="String" />
    </DeleteParameters>
    <FilterParameters>
        <asp:ControlParameter Name="ID" ControlID="GridView1" PropertyName="SelectedValue" />
    </FilterParameters>
    </asp:SqlDataSource>
    
    <%--SQLDataSouce to populate dropdownlist in DetailsView for Suppliers--%>
    <asp:SqlDataSource 
        ID="SqlDataSourceSuppliers" 
        runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionStringJanus %>"
        ProviderName="<%$ ConnectionStrings:ConnectionStringJanus.ProviderName %>"  
        SelectCommand="select id, description from suppliers">
    </asp:SqlDataSource>
    
    <%--SQLDataSouce to populate dropdownlist in DetailsView for Physical States--%>
    <asp:SqlDataSource 
        ID="SqlDataSourcePhysStates" 
        runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionStringJanus %>"
        ProviderName="<%$ ConnectionStrings:ConnectionStringJanus.ProviderName %>"  
        SelectCommand="select id, description from phys_states">
    </asp:SqlDataSource>

    The DetailsView populates fine, it takes the selected item from a GridView and displays the details for that record. When I click on the edit button Ii get the error message of "'DropDownListSuppliers' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value
    ".

    If anybody can see where I have gone wrong I would be grateful. I have read elsewhere that this error sometimes occurs when NULLs are returned from the database. The queries reading the SUPPLIERS and PHYS_STATES tables both return rows with no NULLS.

    Thursday, May 28, 2009 10:46 AM

Answers

  • User472117617 posted

    The value you are storing/retrieving must match one of the list item values.  So if you dropdown has:

    Text="Alabama" value="AL"

    but your "Person" record has a State property with value="Alabama" you will get this error.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 28, 2009 10:54 AM