UpdateParameter не работает - PullRequest
2 голосов
/ 27 мая 2011

Я пытаюсь установить параметр обновления вручную.Команда выполняется успешно, и я могу получить данные обратно, чтобы подтвердить, что параметр был установлен правильно.Однако мой оператор обновления SQL не будет выполнен.Если я вручную определяю значение по умолчанию или вручную вводю значение в операторе обновления, он работает нормально, но если я пытаюсь установить его с помощью команды, он не работает.

Вот мой код:

ИСТОЧНИК ДАННЫХ SQL

<asp:SqlDataSource ID="reconcileDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
     OnUpdating="reconcileGrid_Updating"
    SelectCommand="GetReconcileItems" SelectCommandType="StoredProcedure"
    UpdateCommand="UPDATE item SET Stat = @Status WHERE ItemID = @ID">

    <UpdateParameters>
        <asp:Parameter Name="ID" />
        <asp:Parameter Name="TransType" Type="String" />
        <asp:Parameter Name="LocationID" Type="String" />
        <asp:Parameter Name="Description" Type="String" />
        <asp:Parameter Name="Status" Type="String" />
        <asp:Parameter Name="TransferLocation" Type="String" />
    </UpdateParameters>

</asp:SqlDataSource>

КОД C #

    protected void reconcileGrid_Updating(object sender, SqlDataSourceCommandEventArgs e)
    {
      DropDownList ddlReconcileStatus = (DropDownList)reconcileGrid.Rows[reconcileGrid.EditIndex].Cells[0].FindControl("ddlReconcileStatus");
      DropDownList ddlTransferLocation = (DropDownList)reconcileGrid.Rows[reconcileGrid.EditIndex].Cells[0].FindControl("ddlTransferLocation");

      // Set the Stat Value
      reconcileDataSource.UpdateParameters["Status"].DefaultValue = ddlReconcileStatus.SelectedValue.ToString();
}

GRIDVIEW

<asp:UpdatePanel ID="reconcileUpdatePanel" runat="server">
        <ContentTemplate>
            <asp:GridView ID="reconcileGrid" runat="server" AutoGenerateColumns="False" 
                DataKeyNames="ID" DataSourceID="reconcileDataSource" >
                <Columns>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" ReadOnly="true" />
                    <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" ReadOnly="true"/>
                    <asp:BoundField DataField="last_known_location" HeaderText="Last Known Location" SortExpression="last_known_location" ReadOnly="true" />
                    <asp:TemplateField HeaderText="Status">
                        <ItemTemplate>
                            <asp:Label ID="lblStatus" runat="server" Text='<%# evalStatus(Eval("Stat")) %>'></asp:Label></ItemTemplate><EditItemTemplate>
                            <asp:DropDownList ID="ddlReconcileStatus" runat="server" OnSelectedIndexChanged="ddlReconcileStatus_SelectedIndexChanged" AutoPostBack="true" >
                                <asp:ListItem Value="3" Text="Allocated"></asp:ListItem><asp:ListItem Value="4" Text="Transferred"></asp:ListItem></asp:DropDownList></EditItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Transfer Location">
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlTransferLocation" runat="server" 
                                DataSourceID="ddlTransferLocationDataSource" DataTextField="Name" 
                                DataValueField="ID" Enabled="false" ></asp:DropDownList>
                        <asp:SqlDataSource 
                                ID="ddlTransferLocationDataSource" runat="server" 
                                ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                                SelectCommand="SELECT [ID], [Name] FROM [TransferLocation]">
                        </asp:SqlDataSource>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>

1 Ответ

1 голос
/ 27 мая 2011

Прежде всего, я запутался, что ваша команда обновления принимает только два параметра:

UpdateCommand="UPDATE item SET Stat = @Status WHERE ItemID = @ID"

В то время как ваша коллекция параметров содержит шесть параметров:

<UpdateParameters>
    <asp:Parameter Name="ID" />
    <asp:Parameter Name="TransType" Type="String" />
    <asp:Parameter Name="LocationID" Type="String" />
    <asp:Parameter Name="Description" Type="String" />
    <asp:Parameter Name="Status" Type="String" />
    <asp:Parameter Name="TransferLocation" Type="String" />
</UpdateParameters>

На самом деле проблема в том, что вы передаете параметры в событии SQLDataSource.ItemUpdating, но вы должны передать эти параметры в событии обновления GridView.

...