更新GridView行

时间:2015-09-22 07:48:20

标签: c# asp.net gridview

我有gridview,每当我点击查看按钮时,它会将单元格值更改为dateTime。现在,我的问题是每当我想要更新该特定行时,整个列都更新为dateTime。更新sql语句是否有问题可能导致此问题?请帮忙。谢谢!

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();
        }
    }
}

1 个答案:

答案 0 :(得分:3)

UPDATE语句更新表中的所有行,除非我们使用了WHERE子句,在这种情况下只更新匹配的行。在您的情况下,WHERE h.uid = h.uid始终为真(也可能读取1 = 1)因此它会更新所有行,因为所有行都匹配该子句。看起来像是一个错字。我假设您实际上想要将h.uid与另一个标识符进行比较,而不是将其与自身进行比较。