1
votes

I have the codes up but when I try to update, it did not change anything. I am not sure where the problem went wrong

I have tried to change the index of email and others from 0-6 but when I use these index, everytime i tries to update, the email became UserID and so on.

Back End: protected void gvAccount_RowUpdating(object sender, 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();
    }

There is no error message, but it just did not update anything. I am not sure if is the WHERE UserID = UserID problem. UserID is my primary key

1

1 Answers

0
votes

I think you've mistaken to take the value. For Excel I use like this:

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

*Update

Something's wrong with your SQL, for the query try somethings like this:

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