【发布时间】:2016-06-25 13:06:46
【问题描述】:
我有一个 sql 数据源内部连接了 2 个表 (product table[product quantity column] 和 customer product table[ProductID,CustomerID,TotalProducts, UpdatedProducts])
目前我使用 sql 按钮将这段代码放在后面:
using (SqlConnection scn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"))
{
scn.Open();
SqlCommand cmd = new SqlCommand("update o set o.Updatedproduct = p.ProductQuantity - o.Totalproduct from CustomerProducts o inner join Products p on o.ProductID = p.ProductID", scn);
cmd.ExecuteNonQuery();
}
虽然它工作正常并且在单击按钮时,它会更新updated products column 中的所有字段,但我想做的是使用超链接执行相同的功能。此外,它只会更新特定字段。这是一张更清晰的图像:
更新: 到目前为止,我得到了这个。 html:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnApprove" runat="server" Text="Approve" CommandName="UpdateProduct" CommandArgument='<%#Eval("CustomerID")+","+ Eval("ProductID") %>' />
</ItemTemplate>
</asp:TemplateField>
后面的代码:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Approve")
{
using (SqlConnection scn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"))
{
scn.Open();
SqlCommand cmd = new SqlCommand("update o set o.Updatedproduct = p.ProductQuantity - o.Totalproduct from CustomerProducts o inner join Products p on o.ProductID = p.ProductID WHERE WHERE ProductID=@ProductID", scn);
cmd.Parameters.AddWithValue("@ProductID", ID);
cmd.ExecuteNonQuery();
}
}
}
这里什么都没有发生
【问题讨论】:
标签: c# asp.net sql-server hyperlink sql-update