我有一个Gridview
,我要在其中尝试通过单击Gridview
中的编辑按钮并引发RowUpdating
事件处理程序来编辑数据。
当我单击Gridview
行中的更新时,我没有收到任何错误,但是Gridview
中的数据与单击编辑/更新之前相同,
数据库中的数据
if (!IsPostback)
这是我的存储过程:
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();
}
答案 0 :(得分:0)
即使您在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