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
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)
{
return;
}
finally
{
}
}
Here is my code for my stored procedure
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