Enable the fields IndexChanging, CancelilngEditing and RowUpdating, At the moment of clicking on "Update" if it shows the labels and the text in them but when and in "Update" it throws me an exception
Index was out of range. Must be non-negative and less than the size of the collection
This is my code
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
bool IsUpdated = false;
int ID = (int)GridView2.DataKeys[e.RowIndex].Value;
int PAYROLL = (int)GridView2.DataKeys[e.RowIndex].Value;
int POOL = (int)GridView2.DataKeys[e.RowIndex].Value;
int TEAM = (int)GridView2.DataKeys[e.RowIndex].Value;
int ROL = (int)GridView2.DataKeys[e.RowIndex].Value;
TextBox WHO = (TextBox)GridView2.Rows[e.RowIndex].FindControl("Tgv_who");
TextBox NAME = (TextBox)GridView2.Rows[e.RowIndex].FindControl("Tgv_name");
TextBox OU = (TextBox)GridView2.Rows[e.RowIndex].FindControl("Tgv_ou");
string myConnectionString = @"C:\Users\gutiece\Desktop\database\" + "Database1.accdb";
using (OleDbConnection connection = new OleDbConnection())
{
using (OleDbCommand command = new OleDbCommand())
{
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data source= " + myConnectionString;
command.Connection = connection;
command.CommandText = "UPDATE users SET who = @who, payroll_number = @payroll, name = @name, ou = @ou, pool = @pool, team = @team, rol_id = @rol WHERE id = @id";
command.Parameters.AddWithValue("@id", ID);
command.Parameters.AddWithValue("@payroll", PAYROLL);
command.Parameters.AddWithValue("@pool", POOL);
command.Parameters.AddWithValue("@team", TEAM);
command.Parameters.AddWithValue("@rol", ROL);
command.Parameters.AddWithValue("@who", WHO);
command.Parameters.AddWithValue("@name", NAME);
command.Parameters.AddWithValue("@ou", OU);
connection.Open();
IsUpdated = command.ExecuteNonQuery() > 0;
GridView2.DataBind();
Response.Redirect("WebForm2.aspx");
connection.Close();
}
}
}