shmulik hazan
shmulik hazan

Reputation: 193

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

I have this code that present a gridview that retrieved the data from 2 datasources. each line present an order details and the last column present the order status. the order status should be updateable in a dropdownlist .

<asp:GridView runat="server" AutoGenerateColumns="False" DataSourceID="OrderDataSource"
    CssClass="DataTables">
    <Columns>
        <asp:CommandField ShowEditButton="true" />
        <asp:BoundField DataField="oID" HeaderText="oId" SortExpression="oId" ReadOnly="true" />
        <asp:BoundField DataField="DateOpened" HeaderText="DateOpened" SortExpression="DateOpened"
            ReadOnly="true" />
        <asp:BoundField DataField="rName" HeaderText="rName" SortExpression="rName" ReadOnly="true" />
        <asp:BoundField DataField="DateOfArrival" HeaderText="DateOfArrival" SortExpression="DateOfArrival"
            ReadOnly="true" />
        <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity"
            ReadOnly="true" />
        <asp:BoundField DataField="sName" HeaderText="sName" SortExpression="sName" ReadOnly="true" />
        <asp:TemplateField HeaderText="osName" SortExpression="osName">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="StatusDataSource"
                    DataTextField="osName" DataValueField="osID" SelectedValue='<%# Bind("osName") %>'>
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("osName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="OrderDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:igroup9_prodConnectionString %>"
    SelectCommand="spGetOrdersListForProject" SelectCommandType="StoredProcedure"
    UpdateCommand="Update[Orders] set [osID] =@osID where [oID]=@oID">
    <SelectParameters>
        <asp:ControlParameter ControlID="ProjectIDHolder" DefaultValue="" Name="ProjectID"
            PropertyName="Value" Type="Int32" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="osID" Type="Int32" />
        <asp:Parameter Name="oID" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="StatusDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:igroup9_prodConnectionString %>"
    SelectCommand="spGetOrderStatus" SelectCommandType="StoredProcedure"></asp:SqlDataSource>

The problem is that when i'm trying to edit a column in the gridview i get this error: (after i click the edit button)

Server Error in '/Maestro' Application.

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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

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

Upvotes: 0

Views: 5657

Answers (1)

mason
mason

Reputation: 32738

Error seems clear to me. The values in the DropDownList don't contain the value that you're trying to set as the selected value. This is probably because the values are from the osID column and the selected value is from the osName column. Whatever the selected value ends up being, it should come from the data source which means you should probably use the same column (or a foreign key relationship).

You should probably change it to...

SelectedValue='<%# Bind("osID") %>'

and make sure that spGetOrdersListForProject returns the osID.

Upvotes: 1

Related Questions