0

所以在这里我试图为我的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
4

1 回答 1

1

在 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
于 2013-06-18T07:13:25.953 回答