【问题标题】:update sql column using a hyperlink in asp.net使用 asp.net 中的超链接更新 sql 列
【发布时间】: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


    【解决方案1】:

    这是一个完整的例子。希望对您有所帮助:

    .ASPX:

     <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="CustomerID" />
                <asp:BoundField DataField="ProductID" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="btnApprove" runat="server" Text="Approve" CommandName="UpdateProduct" CommandArgument='<%#Eval("CustomerID")+","+ Eval("ProductID") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    

    背后的代码:

      public partial class GridViewRowCommandExample : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if(!Page.IsPostBack)
                {
                    var p1 = new Product { CustomerID = 1, ProductID = 11 };
                    var p2 = new Product { CustomerID = 2, ProductID = 22 };
    
                    GridView1.DataSource = new List<Product> { p1, p2 };
                    GridView1.DataBind();
                }
            }
    
            protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "UpdateProduct")
                {
                    string[] parameters = e.CommandArgument.ToString().Split(',');
                    int customerID = Int32.Parse(parameters[0]);
                    int productID = Int32.Parse(parameters[1]);
                    //Now that you know which row to update run the SQL update statement 
                }
            }
        }
    
        public class Product
        {
            public int CustomerID { get; set; }
            public int ProductID { get; set; }
        }
    

    【讨论】:

    • 谢谢您,先生。先生,您介意查看我的更新吗?
    • 首先不要使用按钮字段,使用模板和链接按钮,因为您需要指定 CommandArgument 属性并使用您需要更新的记录的 id 填充此属性。看看我是怎么做到的完成它。其次,您指定了一个名为 @ProductID 的参数,但您的 SqlCommand 使用 Id,这就是您收到错误的原因。您需要确保参数名称匹配
    • 我使用了项目模板。现在它什么都不做,先生?
    • 请将其更改为 WHERE ProductID=@ProductID
    • 因为 CommandName 参数不匹配。一个称为“Approve”,.aspx 中的一个称为 CommandName="UpdateProduct"。在 RowCommand 中放置一个断点并检查 e.CommandName 属性
    【解决方案2】:

    首先在 GridView 控件中放置一个带有“ButtonFieldName”的按钮字段,然后尝试 GridView 控件的 RowCommand 事件。

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ButtonFieldName")
        {
           // Your Logic goes here
        }
    }
    

    【讨论】:

    • 谢谢。我怎样才能让它更新特定的行?例如客户 ID 10?
    猜你喜欢
    • 2011-08-16
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    相关资源
    最近更新 更多