【问题标题】:delete row from gridview sql从gridview sql中删除行
【发布时间】:2017-04-28 04:47:07
【问题描述】:

当我单击该网格视图上的删除按钮时,我希望能够删除一行。我有 aspx 页面和后面的代码以及应用程序代码。 DeletePaymentCondition 运行存储过程以删除该行。但不知何故,整体代码不起作用

aspx

    <asp:GridView ID="gridview1" runat="server" HorizontalAlign="left" AutoGenerateColumns="false" CssClass="table table-bordered " GridLines="None" 
        AllowSorting="True" OnRowDeleting="OnRowDeleting">  
         <Columns>
             <asp:TemplateField ItemStyle-HorizontalAlign="left" HeaderText="Payment Condition" HeaderStyle-CssClass="OGColor" HeaderStyle-ForeColor="white" SortExpression="monthToQuarters">
                <ItemTemplate>
                      <span style="font-size:12px; color: #2980b9; text-align:left">
                      <asp:Label ID="lblUserId" runat="server" Visible="true" Text="<%# bind('payConditionId')%>"/>
</span>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150"/>           
        </Columns>
    </asp:GridView>

cs



protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label lblEmpID = (Label)gridPayment.Rows[e.RowIndex].FindControl("lblUserId");  //This is Table Id load on Label1

        int id = Convert.ToInt32(lblEmpID.Text.ToString());


        dsPayment = objcommission.Delete(id);
        gridPayment.DataSource = dsPayment.Tables[0];
        gridPayment.DataBind();

    }

应用代码

public DataSet DeletePayment(int id)
{
    DataSet dsGetAllPayment;
    dsGetAllPaymentCondition = SqlHelper.ExecuteDataset(OGconnection, CommandType.Text, "Delete FROM tblPay where pay ='" + id + "'");
    return dsGetAllPayment;
}

【问题讨论】:

  • “payConditionId”字段的数据类型是什么?我以为DeletePaymentCondition 有一个存储过程,但它看起来像一个内联 sql 语句。

标签: c# asp.net gridview


【解决方案1】:

您应该执行两种不同的 SQL,一种用于删除,另一种用于检索新数据。

DELETE 应该在 NonQuery 中执行,因为它不返回行(仅返回受影响的行数)。

public DataSet DeletePaymentCondition(int ids)
{
    int rowsAffected = SqlHelper.ExecuteNonQuery(OGconnection, CommandType.Text, "Delete FROM [Accounting].[dbo].[tblPayConditions] where payConditionId ='" + ids + "'");
    DataSet dsGetAllPaymentCondition = SqlHelper.ExecuteDataSet(OGconnection, CommandType.Text, "Select * FROM [Accounting].[dbo].[tblPayConditions]");
    return dsGetAllPaymentCondition;
}

作为一个好的实践,您应该考虑将其更改为参数化查询。在这种情况下,由于整数转换是安全的,但是在带有字符串参数的类似代码中,您将容易受到 SQL 注入攻击

【讨论】:

  • 很久没做web表单了,但是点击gridviewrow上的delete的时候应该不用重新绑定数据吧?我以为它会自动删除该行并将其余数据留在那里。
  • 仍然删除该行..也许我的 .cs 或 .aspx 代码有问题
  • 设置断点并在ExecuteNonQuery 运行后检查rowsAffected 变量,它的值应该大于零。还要检查idsExecuteNonQuery 之前的值。表中是否有具有该 ID 的记录?
【解决方案2】:

我得到了解决方案。我对 cs 文件以及 bradbury9 提供的代码进行了更改。

protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        int index = Convert.ToInt32(gridPaymentCondition.DataKeys[e.RowIndex].Value.ToString());

        dsPaymentCondition = objcommission.DeletePaymentCondition(index);
        gridPaymentCondition.DataSource = dsPaymentCondition.Tables[0];

        updatePaymentConditionsWithoutRefresh();
    }

【讨论】:

  • 很高兴为您提供帮助。如果答案对您有帮助,我建议将其标记为有帮助
  • 我还必须将 DataKeyNames="lblUserId" 放在 gridview 中才能正常工作。但再次感谢!
猜你喜欢
  • 2016-05-25
  • 2016-01-30
  • 2014-02-12
  • 1970-01-01
  • 1970-01-01
  • 2015-02-23
  • 2010-10-10
  • 2018-12-08
相关资源
最近更新 更多