无法更新GridView

时间:2019-06-17 07:21:51

标签: c# asp.net gridview

我已经设置了代码,但是当我尝试更新时,它什么都没有改变。我不确定问题出在哪里

我已经尝试将电子邮件和其他邮件的索引从0-6更改,但是当我使用这些索引时,每次尝试更新时,电子邮件都将成为UserID,依此类推。

后端:     受保护的void gvAccount_RowUpdating(对象发送者,GridViewUpdateEventArgs e)     {

    GridViewRow row = (GridViewRow)gvAccount.Rows[e.RowIndex];
    string UserID = gvAccount.DataKeys[e.RowIndex].Values["UserID"].ToString();
    string Email = ((TextBox)row.Cells[1].Controls[0]).Text;
    string FirstName = ((TextBox)row.Cells[2].Controls[0]).Text;
    string LastName = ((TextBox)row.Cells[3].Controls[0]).Text;
    string Password = ((TextBox)row.Cells[4].Controls[0]).Text;
    string Point = ((TextBox)row.Cells[5].Controls[0]).Text;
    string Role = ((TextBox)row.Cells[6].Controls[0]).Text;

    SqlCommand cmd = new SqlCommand("UPDATE UserRegister set Email = '" + Email + "', FirstName = '" + FirstName + "',  LastName = '" + LastName + "', Password = '" + Password + "',Point = '" + Point + "',Role = '" + Role + "' WHERE UserID =" + UserID, con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    gvAccount.EditIndex = -1;
    FillGrid();
    }

没有错误消息,但它没有更新任何内容。我不确定WHERE UserID = UserID问题。 UserID是我的主键

1 个答案:

答案 0 :(得分:0)

我认为您误会了价值。对于Excel,我这样使用:

string email= row[1].ToString();
string firstName = row[2].ToString();
string lastName = row[3].ToString();
string pass= row[4].ToString();
string point = row[5].ToString();
string role = row[6].ToString();

*更新

SQL出了点问题,对于查询,请尝试如下操作:

string con = @"Data Source=myServerAddress;Initial Catalog=myDataBase Integrated Security=SSPI;
User ID=myDomain\myUsername;Password=myPassword;";
string comm = @"Update... SET... WHERE...";
SqlCommand cmd = new SqlCommand(comm, con);
Gridview.Databind();