Abie Giordano
Abie Giordano

Reputation: 371

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:

 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

Upvotes: 0

Views: 222

Answers (1)

Ben Narube
Ben Narube

Reputation: 448

In DeleteTransaction function

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.

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

Upvotes: 1

Related Questions