使用c#在asp.net中删除行

本文关键字:删除行 net asp 使用 | 更新日期: 2023-09-27 18:19:00

所以这里我试图使用一些删除函数为我的datagridview,这是背后的代码:

 protected void gvTransaction_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        logID = Helper.GetLogID();
        try
        {
            LKLog.Write(logID, "Transaction.aspx.cs", "gvTransaction_RowDeleting", "Start Method", string.Empty, Helper.GetUserName());
            GridViewRow row = gvTransaction.Rows[e.RowIndex];
            orgID = CommonFunctions.StringToInt(gvTransaction.DataKeys[e.RowIndex].Values[0].ToString());
            siteID = CommonFunctions.StringToInt(gvTransaction.DataKeys[e.RowIndex].Values[1].ToString());
            int transactionID = CommonFunctions.StringToInt(gvTransaction.DataKeys[e.RowIndex].Values[2].ToString());
            clsTransaction.DeleteTransaction(logID, orgID, siteID, transactionID, true);
            string message = string.Format(Constants.DeleteMessage, "Transaction");
            LKLog.Write(logID, "Transaction.aspx.cs", "gvTransaction_RowDeleting", "End Method", string.Empty, Helper.GetUserName());
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Info", "alert('" + message + "');window.location ='Transaction.aspx';", true);
        }
        catch (Exception ex)
        {
            LKLog.Write(logID, "Transaction.aspx.cs", "gvTransaction_RowDeleting", ex.StackTrace, ex.Message, Helper.GetUserName());
            throw;
        }
    }

下面是类函数:

        public static void DeleteTransaction(decimal logID, int orgID, int siteID, int TransactionID, bool isActive)
    {
        LKLog.Write(logID, "clsTransaction.cs", "DeleteTransaction", "Start Method", string.Empty, Helper.GetUserName());
        using (SqlConnection conn = new SqlConnection(CommonFunctions.GetAppDBConnection(Constants.AppID, Constants.TMDDBConnection)))
        {
            try
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "spMSTransaction_Delete";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("OrgID", orgID));
                cmd.Parameters.Add(new SqlParameter("SiteID", siteID));
                cmd.Parameters.Add(new SqlParameter("TransactionID", TransactionID));
                cmd.Parameters.Add(new SqlParameter("IsActive", siteID));
                cmd.Parameters.Add(new SqlParameter("Username", Helper.GetUserName()));
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                LKLog.Write(logID, "clsTransaction.cs", "DeleteTransaction", ex.StackTrace, ex.Message, Helper.GetUserName());
                throw;
            }
            finally
            {
                conn.Close();
            }
        }
        LKLog.Write(logID, "clsTransaction.cs", "DeleteTransaction", "End Method", string.Empty, Helper.GetUserName());
    }

问题是,代码不工作(行不删除,它只是保持不变)是否有任何错误或错误的做法在使用代码?谢谢你的帮助

编辑存储过程:

ALTER PROC [dbo].[spMSTransaction_Delete] 
@OrgID int,
@SiteID int,
@TransactionID int,
@IsActive bit,
@Username varchar(50)
 AS 
  SET NOCOUNT ON
  SET XACT_ABORT ON  
BEGIN TRAN
UPDATE  [dbo].[MSTransaction]
SET     [IsActive] = @IsActive, 
        [ModifiedDate] = GETUTCDATE(), 
        [ModifiedBy] = @Username
WHERE   [OrgID] = @OrgID
        AND [SiteID] = @SiteID
        AND [TransactionID] = @TransactionID
COMMIT

使用c#在asp.net中删除行

在DeleteTransaction函数

cmd.Parameters.Add(new SqlParameter("IsActive", siteID));

应该是

 cmd.Parameters.Add(new SqlParameter("IsActive", isActive));

也许你甚至不应该从代码中传递这个。只需在存储过程中直接将IsActive设置为false,因为在调用该存储过程时,您总是希望将其设置为false。

尝试像这样更改存储过程:

ALTER PROC [dbo].[spMSTransaction_Delete] 
@OrgID int,
@SiteID int,
@TransactionID int,
@IsActive bit,
@Username varchar(50)
AS 
SET NOCOUNT ON
SET XACT_ABORT ON  
BEGIN TRAN
UPDATE  [dbo].[MSTransaction]
SET     [IsActive] = 0, 
    [ModifiedDate] = GETUTCDATE(), 
    [ModifiedBy] = @Username
WHERE   [OrgID] = @OrgID
    AND [SiteID] = @SiteID
    AND [TransactionID] = @TransactionID
COMMIT