简体   繁体   English

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

[英]using row deleting in asp.net using c#

so here i"m trying to use some delete function for my datagridview, here's the code behind: 所以在这里我试图为我的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;
        }
    }

here's the class function: 这是类函数:

        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());
    }

the problem is, the code's not working (the row is not deleted, it just stay the same) is there any mistake or wrong-practice in using the code? 问题是,代码无法正常工作(行未删除,只是保持不变),使用代码时是否有任何错误或错误做法? thank you for your help 谢谢您的帮助

EDIT the stored procedure: 编辑存储过程:

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

In DeleteTransaction function 在DeleteTransaction函数中

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

should this be something like 应该像这样吗

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

Perhaps you shouldn't even pass this from the code. 也许您甚至都不应该通过代码传递它。 Just set IsActive to false directly in your stored procedure since you'll always want to set it to false when you call this stored procedure. 只需在存储过程中直接将IsActive设置为false,因为在调用此存储过程时,您始终希望将其设置为false。

Try changing your stored procedure like this: 尝试像这样更改存储过程:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM