本文介绍了为什么ASP.NET网格视图RowUpdating事件插入重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程,用于通过GridView RowUpdating事件更新数据。存储过程的作用是首先更新某个记录,然后在成功更新后创建另一个记录。

现在发生的是第一条记录成功更新,然后创建两条新记录而不是一条记录。我在RowUpdating事件上使用SqlDataSource。

当我使用SSMS测试时代码运行正常。



这是我的RowUpdating代码事件

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)
    {
        //this.divMsg.Visible = true;
        //this.lblMsg.Text = ex.Message;
        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

推荐答案

this.spGetNewForgotPasswordRequestsDS.Update();
this.gridNewForgotPassword.EditIndex = -1;
this.gridNewForgotPassword.DataBind();


这篇关于为什么ASP.NET网格视图RowUpdating事件插入重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 11:49