Error when wanting to edit row in GridView with ASP.Net

0

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

.

    
asked by Cesar Gutierrez Davalos 18.05.2017 в 15:42
source

2 answers

0

Notice that in your query the query says the following:

UPDATE users SET who = [@WHO], payroll_number = [@PAYROLL_NUMBER], name = [@NAME], ou = [@OU], pool = [@POOL], team = [@TEAM], rol_id = [@ROL_ID] WHERE id = id

and surely WHERE id = id is wrong, pq id must be numeric, and probably you wanted to pass an Id to know which record to modify. You are missing a parameter.

    
answered by 18.05.2017 в 16:22
0

According to what I can see in your code, all the parameters are sent by text type when you put the single quotes --- > 'valor' checks if the update procedure actually receives all the parameters in text type .... if the parameter is a date type you must send it as a date type, same if you receive a double, or an integer, or a byte, etc. ... must be of the same type of data

    
answered by 18.05.2017 в 23:31