2017-04-26 92 views
0

我希望能够删除一行,当我点击该gridview上的删除按钮。我有aspx页面和代码,以及应用程序代码。 DeletePaymentCondition运行存储过程以删除行。但不知何故,总码不起作用从gridview删除行sql

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; 
} 
+0

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

回答

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注入攻击

+0

很长一段时间没有完成网页表单,但是如果你点击gridviewrow上的删除,你不应该重新绑定数据。我认为它会自动删除该行并将其余的数据留在那里。 –

+0

仍然会删除该行..可能我的.cs或.aspx代码有错误 –

+0

在运行ExecuteNonQuery之后设置一个断点并检查'rowsAffected'变量,它的值应该大于零。在ExecuteNonQuery之前检查'ids'的值。表中有该ID的记录吗? – bradbury9

0

我得到的解决方案。我对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(); 
    } 
+1

很高兴帮助。如果答案对您有帮助,我会建议将其标记为有帮助 – bradbury9

+0

另外,我必须将DataKeyNames =“lblUserId”放在gridview中才能使用。但再次感谢! –