Click here to Skip to main content
16,015,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a stored procedure which I use to update data through GridView RowUpdating event. What a stored procedure does is that it update a certain record first and then it create another record after successfully update.
What happens now is that the first record is updated successfully and then it creates two new records instead of one record. I'm using SqlDataSource on RowUpdating event.
The code run okay when I test by using SSMS.

Here is my codes for RowUpdating event
C#
protected void gridNewForgotPassword_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    string requestHistoryID = this.gridNewForgotPassword.DataKeys[e.RowIndex].Values["RequestHistoryID"].ToString();
    string requestID = this.gridNewForgotPassword.DataKeys[e.RowIndex].Values["RequestID"].ToString();
    string requestTypeWorkflowID = this.gridNewForgotPassword.DataKeys[e.RowIndex].Values["RequestTypeWorkflowID"].ToString();
    string requestTypeID = this.gridNewForgotPassword.DataKeys[e.RowIndex].Values["RequestTypeID"].ToString();
    DropDownList cmbRequestStatusID = (DropDownList)this.gridNewForgotPassword.Rows[e.RowIndex].FindControl("cmbRequestStatusID");
    TextBox txtRemarks = (TextBox)this.gridNewForgotPassword.Rows[e.RowIndex].FindControl("txtRemarks");

    try
    {
        this.spGetNewForgotPasswordRequestsDS.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;
        this.spGetNewForgotPasswordRequestsDS.UpdateCommand = "spUpdateRequest";
        this.spGetNewForgotPasswordRequestsDS.UpdateParameters.Add("RequestHistoryID", requestHistoryID);
        this.spGetNewForgotPasswordRequestsDS.UpdateParameters.Add("RequestID", requestID);
        this.spGetNewForgotPasswordRequestsDS.UpdateParameters.Add("RequestTypeWorkflowID", requestTypeWorkflowID);
        this.spGetNewForgotPasswordRequestsDS.UpdateParameters.Add("RequestTypeID", requestTypeID);
        this.spGetNewForgotPasswordRequestsDS.UpdateParameters.Add("RequestStatusID", cmbRequestStatusID.SelectedValue);
        this.spGetNewForgotPasswordRequestsDS.UpdateParameters.Add("ApprovalID", Session["PFNumber"].ToString());
        this.spGetNewForgotPasswordRequestsDS.UpdateParameters.Add("ApprovalName", Session["cn"].ToString());
        this.spGetNewForgotPasswordRequestsDS.UpdateParameters.Add("ApprovalDate", DateTime.Now.ToLongDateString());
        this.spGetNewForgotPasswordRequestsDS.UpdateParameters.Add("Remarks", txtRemarks.Text);
        this.spGetNewForgotPasswordRequestsDS.Update();
        this.gridNewForgotPassword.DataBind();
    }
    catch (Exception ex)
    {
        //this.divMsg.Visible = true;
        //this.lblMsg.Text = ex.Message;
        return;
    }
    finally
    {
    }
}

Here is my code for my stored procedure
SQL
BEGIN TRAN
UPDATE RequestHistories
SET
RequestStatusID=@RequestStatusID,
ApprovalID=@ApprovalID,
ApprovalName=@ApprovalName,
ApprovalDate=@ApprovalDate,
Remarks=@Remarks
WHERE
RequestHistoryID=@RequestHistoryID

SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM

SELECT @nextRequestTypeWorkflowID = fnReturnNextRequestTypeWorkflowID_1.RequestTypeWorkflowID FROM fnReturnNextRequestTypeWorkflowID(@RequestTypeID,@RequestTypeWorkflowID) AS fnReturnNextRequestTypeWorkflowID_1
SELECT @ApprovalPositionCode = ApprovalPositionCode FROM RequestTypeWorkflows WHERE RequestTypeWorkflowID=@nextRequestTypeWorkflowID
SELECT @ApprovalPositionName = ApprovalPositionName FROM RequestTypeWorkflows WHERE RequestTypeWorkflowID=@nextRequestTypeWorkflowID

INSERT INTO RequestHistories
(
RequestID,
RequestTypeWorkflowID,
ApprovalPositionCode,
ApprovalPositionName
)
VALUES
(
@RequestID,
@nextRequestTypeWorkflowID,
@ApprovalPositionCode,
@ApprovalPositionName
)
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM

COMMIT TRAN

PROBLEM:
IF (@intErrorCode <> 0) BEGIN
RAISERROR('Unexpected error occurred!',16,1);
ROLLBACK TRAN
END
Posted
Updated 27-Mar-15 3:22am
v3
Comments
ZurdoDev 27-Mar-15 7:33am    
Why is it doing that?
Andrew Augustino 27-Mar-15 8:43am    
I don't know exactly why but I tracked it on VS by using breakpoint, when SqlDataSource calls update method it insert one record and then it goes to HTML part of the aspx page. So when it leaves the finally block on the code behind it insert another record on the table
ZurdoDev 27-Mar-15 8:54am    
No one can help unless you click Improve Question and add relevant code. We can't see what's happening.

1 solution

At a guess, you forgot to set e.Cancel = true in the RowUpdating event handler. As a result, the GridView is calling the UpdateCommand on the associated DataSource control after your code has already called the same command.

EDIT: You'll also need to set the EditIndex to -1 before re-binding the grid; otherwise, the row will remain in edit-mode.
C#
this.spGetNewForgotPasswordRequestsDS.Update();
this.gridNewForgotPassword.EditIndex = -1;
this.gridNewForgotPassword.DataBind();
 
Share this answer
 
v2
Comments
Andrew Augustino 27-Mar-15 9:57am    
Thanks it works as I expected
Andrew Augustino 7-Apr-15 3:02am    
Soory, I have to reject solution after I found that its not 100% correct. If I put e.Cancel=true the said GridView row doesn't change from the edit mode to item mode. It remains in edit mode until I click cancel. Anyway but the problem of duplicate insert is over by this method except for that edit mode to item mode thing.
Richard Deeming 7-Apr-15 7:19am    
You probably just need to set the EditIndex to -1 before re-binding the grid. I've updated the answer.
Andrew Augustino 9-Apr-15 9:08am    
Yes Richard, with that addition line it works okay now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900