sqldatasource update() 不起作用
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
它对我有用。
Try this:
It has been worked for me.
将 OrderTotal 添加到参数列表中,然后在更新之前在代码中执行以下操作:
使用 DefaultValue 属性将此值传递给查询。
或者,点击更新事件,并将新值添加到事件参数值集合中。这会被传递到后端。
HTH。
Add the OrderTotal to the list of parameters, and then do in code before the 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.
好吧,不知道为什么它不会自动更新,但我只是在手动更新之前设置了 updatecommand,这似乎有效:
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: