I have a Gridview
where I am trying to edit my data by clicking on an edit button in the Gridview
and raising the RowUpdating
event handler.
When I click update in Gridview
row, I receive no errors but the data in the Gridview
remains the same as before I clicked edit/update as well as the
data in the database
- I have checked to see if my Stored Procedure actually works.
- I have used breakpoints and all the data is correct.
- I have done the same methods/procedures for deleting the row from the gridview and database and they have worked.
- I have checked
if (!IsPostback)
Here's my Stored Procedure:
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
Here's my Method for executing stored procedure:
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();
}
I am using my own Connection class: GitHub
Here's my gridview:
<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>
Here's my actual code:
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();
}