更新参数不起作用

发布于 2024-11-09 20:12:40 字数 4059 浏览 1 评论 0原文

我正在尝试手动设置更新参数。命令执行成功,我可以拉回数据以确认参数设置正确。但是我的 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>

I am trying to set an update parameter manually. The command executes successfully and I can pull the data back to confirm that the parameter was set correctly. However my SQL update statement will not execute. If I manually define a default value or manually enter a value in the update statement it works fine, but if I try to set it through a command it does not work.

Here is my code:

SQL DATA SOURCE

<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# CODE

    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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎歌 2024-11-16 20:12:40

首先,我很困惑您的 Update Command 仅需要两个参数:

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 更新事件中传递这些参数。

First off, I am confused that your Update Command takes only two parameters:

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

Whereas your parameter collection contains six parameters:

<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>

Actually, the problem is that you are passing parameters in the SQLDataSource.ItemUpdating event, but you have to pass these parameters in the GridView update event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文