sqldatasource update() 不起作用

发布于 2024-10-31 19:53:51 字数 2763 浏览 2 评论 0原文

我正在使用 sqldatasource 来执行更新。

我有一个交易表,用于存储用户选择购买的各个商品(商品名称、商品价格、订单 ID 等)

我有一个订单表,用于存储订单的值(帐户名称、订单总额等)

这是我的 SqlDataSource如 .aspx 页面中所定义:

        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:NH_SWAGConnectionString %>" 

        InsertCommand="INSERT INTO [tblOrders] ([OrderDate], [OrderTotal], [OrderAccount], [OrderCostCentre]) VALUES (@OrderDate, @OrderTotal, @OrderAccount, @OrderCostCentre); SELECT @OrderNewID = SCOPE_IDENTITY()" 
        SelectCommand="SELECT * FROM [tblOrders]" 
        UpdateCommand="UPDATE [tblOrders] SET [OrderDate] = @OrderDate, [OrderTotal] = @OrderTotal, [OrderAccount] = @OrderAccount, [OrderCostCentre] = @OrderCostCentre WHERE [OrderID] = @original_OrderID AND [OrderDate] = @original_OrderDate AND [OrderTotal] = @original_OrderTotal AND [OrderAccount] = @original_OrderAccount AND [OrderCostCentre] = @original_OrderCostCentre" 
        OldValuesParameterFormatString="original_{0}" 

        oninserting="SqlDataSource2_Inserting"
        oninserted="SqlDataSource2_Inserted" onupdated="SqlDataSource2_Updated" 
        onupdating="SqlDataSource2_Updating">

        <InsertParameters>
            <asp:Parameter Direction="Output" Name="OrderNewId" Type="Int32" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="OrderDate" Type="DateTime" />                                
            <asp:Parameter Name="OrderAccount" Type="String" />
            <asp:Parameter Name="OrderCostCentre" Type="String" />
            <asp:Parameter Name="original_OrderID" Type="Int32" />
            <asp:Parameter Name="original_OrderDate" Type="DateTime" />
            <asp:Parameter Name="original_OrderTotal" Type="Decimal" />
            <asp:Parameter Name="original_OrderAccount" Type="String" />
            <asp:Parameter Name="original_OrderCostCentre" Type="String" />
            <%--<asp:ControlParameter ControlID="lblOrderTotal" Name="OrderTotal" 
                PropertyName="Text" />--%>
        </UpdateParameters>
    </asp:SqlDataSource>

您可以看到我屏蔽了将 OrderID 附加到文本框的代码。我这样做是因为那部分不起作用。然后,我尝试修改代码隐藏中的参数:

        //Update Order Table with Total of Order
    //SqlDataSource2.UpdateParameters["OrderTotal"].DefaultValue = (string)OrderTotal.ToString();        
    SqlDataSource2.UpdateParameters.Add("OrderTotal", (string)OrderTotal.ToString());
    SqlDataSource2.Update();

页面不会出错,但订单数据库中对 OrderTotal 的更新不会更新。它保持在“0”。有人能为我解释一下吗?我已经尝试了2天了。是的,这是我最后一次使用 SqlDataSource - 似乎每个人都推荐不同的方法(DAC?) - 下一个项目我将遵循每个人的建议:)

谢谢大家

I am using sqldatasource to perform an update.

I have a Transactions table which stores individual items that the user selects for purchase (item name, item price, orderID, etc)

I have an Orders table which stores values for the Order (account name, order total, etc)

Here is my SqlDataSource as defined in the .aspx page:

        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:NH_SWAGConnectionString %>" 

        InsertCommand="INSERT INTO [tblOrders] ([OrderDate], [OrderTotal], [OrderAccount], [OrderCostCentre]) VALUES (@OrderDate, @OrderTotal, @OrderAccount, @OrderCostCentre); SELECT @OrderNewID = SCOPE_IDENTITY()" 
        SelectCommand="SELECT * FROM [tblOrders]" 
        UpdateCommand="UPDATE [tblOrders] SET [OrderDate] = @OrderDate, [OrderTotal] = @OrderTotal, [OrderAccount] = @OrderAccount, [OrderCostCentre] = @OrderCostCentre WHERE [OrderID] = @original_OrderID AND [OrderDate] = @original_OrderDate AND [OrderTotal] = @original_OrderTotal AND [OrderAccount] = @original_OrderAccount AND [OrderCostCentre] = @original_OrderCostCentre" 
        OldValuesParameterFormatString="original_{0}" 

        oninserting="SqlDataSource2_Inserting"
        oninserted="SqlDataSource2_Inserted" onupdated="SqlDataSource2_Updated" 
        onupdating="SqlDataSource2_Updating">

        <InsertParameters>
            <asp:Parameter Direction="Output" Name="OrderNewId" Type="Int32" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="OrderDate" Type="DateTime" />                                
            <asp:Parameter Name="OrderAccount" Type="String" />
            <asp:Parameter Name="OrderCostCentre" Type="String" />
            <asp:Parameter Name="original_OrderID" Type="Int32" />
            <asp:Parameter Name="original_OrderDate" Type="DateTime" />
            <asp:Parameter Name="original_OrderTotal" Type="Decimal" />
            <asp:Parameter Name="original_OrderAccount" Type="String" />
            <asp:Parameter Name="original_OrderCostCentre" Type="String" />
            <%--<asp:ControlParameter ControlID="lblOrderTotal" Name="OrderTotal" 
                PropertyName="Text" />--%>
        </UpdateParameters>
    </asp:SqlDataSource>

You can see I blocked out the code that attaches the OrderID to a Textbox. I did this because that portion wasn't working. I then tried to modify the Parameter in Code-Behind:

        //Update Order Table with Total of Order
    //SqlDataSource2.UpdateParameters["OrderTotal"].DefaultValue = (string)OrderTotal.ToString();        
    SqlDataSource2.UpdateParameters.Add("OrderTotal", (string)OrderTotal.ToString());
    SqlDataSource2.Update();

The page doesn't error out, but the Update to OrderTotal in the Orders Db doesn't update. It stays at '0'. Can anyone shed some light on this for me? I've been trying for 2 days now. And yes, this is the last time I will be using SqlDataSource - It seems everyone recommends a different approach (DAC?) - Next project I will follow everyone's suggestion :)

Thanks all

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

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

发布评论

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

评论(3

故事与诗 2024-11-07 19:53:51

试试这个:

protected void SqlDataSource2_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
    e.Command.Parameters[0].Value = OrderTotal;
    e.Command.Parameters[1].Value = OrderNewId;
}

它对我有用。

Try this:

protected void SqlDataSource2_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
    e.Command.Parameters[0].Value = OrderTotal;
    e.Command.Parameters[1].Value = OrderNewId;
}

It has been worked for me.

清音悠歌 2024-11-07 19:53:51

将 OrderTotal 添加到参数列表中,然后在更新之前在代码中执行以下操作:

SqlDataSource2.UpdateParameters["OrderTotal"].DefaultValue = OrderTotal.ToString();
SqlDataSource2.Update();

使用 DefaultValue 属性将此值传递给查询。

或者,点击更新事件,并将新值添加到事件参数值集合中。这会被传递到后端。

HTH。

Add the OrderTotal to the list of parameters, and then do in code before the update:

SqlDataSource2.UpdateParameters["OrderTotal"].DefaultValue = OrderTotal.ToString();
SqlDataSource2.Update();

Using the DefaultValue property passes this value to the query.

Or, tap into the Updating event, and add the new value to the event argument collection of values. This gets passed to the backend.

HTH.

川水往事 2024-11-07 19:53:51

好吧,不知道为什么它不会自动更新,但我只是在手动更新之前设置了 updatecommand,这似乎有效:

SqlDataSource2.UpdateCommand = "UPDATE tblOrders SET [OrderTotal] = " + (string)OrderTotal.ToString() + " WHERE OrderID = " + OrderNewID;

OK, well not sure why it wouldn't automatically update, but I just set the updatecommand before updating manually, and that seemed to have worked:

SqlDataSource2.UpdateCommand = "UPDATE tblOrders SET [OrderTotal] = " + (string)OrderTotal.ToString() + " WHERE OrderID = " + OrderNewID;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文