Reputation: 21
I have gridview that changes a cell value to dateTime whenever I click the review button. Now, my problem is whenever I want to update that particular row, the whole column updates to dateTime. Is there something wrong with the update sql statement which might be causing this problem? Please help. Thanks!
protected void GridViewReview_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Review"))
{
GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;
Label label = row.FindControl("lbl_reviewDate") as Label;
label.Text = DateTime.Now.ToString();
string connString = ConfigurationManager.ConnectionStrings["sqlconnectionstringPIM"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
string queryString = @"
UPDATE [dbo].[Group]
SET [lastreview] = @lastreview
FROM [PIM].[dbo].[Group] AS g
INNER JOIN [Department] AS d
ON g.departmentID = d.id
INNER JOIN [HOD] AS h
ON d.hodid = h.id
WHERE h.uid = h.uid";
SqlCommand mySqlCommand = new SqlCommand(queryString, conn);
mySqlCommand.Parameters.Add("@lastreview", SqlDbType.DateTime).Value = label.Text;
mySqlCommand.ExecuteNonQuery();
conn.Close();
}
}
}
Upvotes: 0
Views: 92
Reputation: 11841
An UPDATE
statement updates all rows in the table, unless a WHERE
clause us used, in which case only the matching rows are updated. In your case, WHERE h.uid = h.uid
is always going to be true (might as well read 1 = 1) hence it updates all rows, because all rows match the clause. Looks like a typo to me. I assume you actually want to compare h.uid to another identifier, not to itself.
Upvotes: 3