首页 > 解决方案 > Gridview RowUpdating 事件使用存储过程不更新实际数据库?

问题描述

我有一个Gridview我试图通过单击中的编辑按钮Gridview并引发RowUpdating事件处理程序来编辑我的数据的地方。

当我单击Gridview行中的更新时,我没有收到任何错误,但其中的数据Gridview与单击编辑/更新之前的数据以及数据库中的数据相同

这是我的存储过程:

CREATE PROCEDURE [dbo].UpdateReview
    @theID int,
    @theDate datetime,
    @theReview varchar(max),
    @theFood int, 
    @theService int, 
    @theAtmos int, 
    @thePrice int
AS
    SET @theDate = GETDATE();

BEGIN
    UPDATE Reviews 
    SET 
        ReviewDate = @theDate,
        ReviewText = @theReview, 
        FoodQuality = @theFood, 
        ServiceRating = @theService, 
        AtmosphereRating = @theAtmos, 
        PriceRating = @thePrice
    WHERE ReviewID = @theID
END

这是我执行存储过程的方法:

public void UpdateReview(int id, string review, int food, int service, int atmos, int price)
        {
            DBConnect objDB = new DBConnect();
            objCmd.Parameters.Clear();
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.CommandText = "UpdateReview";

            objCmd.Parameters.AddWithValue("@theID", id);
            objCmd.Parameters.AddWithValue("@theReview", review);
            objCmd.Parameters.AddWithValue("@theFood", food);
            objCmd.Parameters.AddWithValue("@theService", service);
            objCmd.Parameters.AddWithValue("@theAtmos", atmos);
            objCmd.Parameters.AddWithValue("@thePrice", price);

            objDB.GetConnection();
            objDB.DoUpdateUsingCmdObj(objCmd);
            objDB.CloseConnection();
        }

我正在使用我自己的 Connection 类:GitHub

这是我的网格视图:

<asp:GridView ID="gvMyReviews" runat="server" AutoGenerateColumns="false" OnRowEditing="gvMyReviews_RowEditing" OnRowUpdating="gvMyReviews_RowUpdating" OnRowCancelingEdit="gvMyReviews_RowCancelingEdit" OnRowDeleting="gvMyReviews_RowDeleting" >
            <Columns>
                <asp:BoundField DataField="ReviewID" HeaderText="ID" ReadOnly="true" />
                <asp:BoundField DataField="UserID" HeaderText="User ID" ReadOnly="true" />
                <asp:BoundField DataField="RestName" HeaderText="Restaurant" ReadOnly="true" />
                <asp:BoundField DataField="ReviewDate" HeaderText="Date of Review" DataFormatString="{0:d}" ReadOnly="true" />
                <asp:BoundField DataField="FoodQuality" HeaderText="Food Quality" />
                <asp:BoundField DataField="ServiceRating" HeaderText="Service" />
                <asp:BoundField DataField="AtmosphereRating" HeaderText="Atmosphere" />
                <asp:BoundField DataField="PriceRating" HeaderText="Price" />
                <asp:BoundField DataField="ReviewText" HeaderText="Review" />
                <asp:CommandField HeaderText="Modify" ShowEditButton="true" ControlStyle-CssClass="button2" />
                <asp:CommandField HeaderText="Remove" ShowDeleteButton="true" ControlStyle-CssClass="button2" />
            </Columns>
        </asp:GridView>

这是我的实际代码:

protected void gvMyReviews_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvMyReviews.EditIndex = e.NewEditIndex;
            DataSet ds = p.GetReviewsByUserID(200);
            gvMyReviews.DataSource = ds;
            gvMyReviews.DataBind();
        }

        protected void gvMyReviews_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int index = e.RowIndex;
            int reviewID = int.Parse(gvMyReviews.Rows[index].Cells[0].Text);

            TextBox txtFoodRating = (TextBox)gvMyReviews.Rows[index].Cells[4].Controls[0];
            int foodRating = int.Parse(txtFoodRating.Text);

            TextBox txtServiceRating = (TextBox)gvMyReviews.Rows[index].Cells[5].Controls[0];
            int serviceRating = int.Parse(txtServiceRating.Text);

            TextBox txtAtmosphereRating = (TextBox)gvMyReviews.Rows[index].Cells[6].Controls[0];
            int atmosphereRating = int.Parse(txtAtmosphereRating.Text);

            TextBox txtPriceRating = (TextBox)gvMyReviews.Rows[index].Cells[7].Controls[0];
            int priceRating = int.Parse(txtPriceRating.Text);

            TextBox txtReview = (TextBox)gvMyReviews.Rows[index].Cells[8].Controls[0];
            string strReview = txtReview.Text;

            p.UpdateReview(reviewID, strReview, foodRating, serviceRating, atmosphereRating, priceRating);

            gvMyReviews.EditIndex = -1;

            DataSet ds = p.GetReviewsByUserID(200);
            gvMyReviews.DataSource = ds;
            gvMyReviews.DataBind();
        }

        protected void gvMyReviews_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvMyReviews.EditIndex = -1;
            DataSet ds = p.GetReviewsByUserID(200);
            gvMyReviews.DataSource = ds;
            gvMyReviews.DataBind();
        }

标签: c#asp.netstored-proceduresgridviewrowcommand

解决方案


即使您在 sp 中设置@theDate 值,您也需要通过命令添加@theDate 参数。否则,您应该将 sp 更改为如下所示..

CREATE PROCEDURE [dbo].UpdateReview
    @theID int,
    @theReview varchar(max),
    @theFood int, 
    @theService int, 
    @theAtmos int, 
    @thePrice int
AS
BEGIN
    UPDATE Reviews 
    SET 
        ReviewDate = GETDATE(),
        ReviewText = @theReview, 
        FoodQuality = @theFood, 
        ServiceRating = @theService, 
        AtmosphereRating = @theAtmos, 
        PriceRating = @thePrice
    WHERE ReviewID = @theID
END

推荐阅读