Error trying to insert ASP.NET data

0

I have a GridView with several Textboxs in Footer , this to try to insert, but at the moment of clicking the button, it fails and tells me that the statement .

I'm using as a BD test one in access

my code is the following since the finished update, does not insert but does not show syntax error or anything like that

protected void dataGridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            Button bt = (Button)sender;
            GridViewRow grdRow = (GridViewRow)bt.Parent.Parent;

            TextBox Txt_who = (TextBox)grdRow.Cells[0].FindControl("Txt_who");
            int Txt_pay_roll = Convert.ToInt32(grdRow.Cells[1].FindControl("Txt_pay_roll"));
            TextBox Txt_name = (TextBox)grdRow.Cells[2].FindControl("Txt_name");
            TextBox Txt_ou = (TextBox)grdRow.Cells[3].FindControl("Txt_ou");
            int Txt_pool = Convert.ToInt32(grdRow.Cells[4].FindControl("Txt_pool"));
            int Txt_team = Convert.ToInt32(grdRow.Cells[5].FindControl("Txt_team"));
            int Txt_rol = Convert.ToInt32(grdRow.Cells[6].FindControl("Txt_rol"));

            using (OleDbConnection connection = new OleDbConnection())
            {
                using (OleDbCommand command = new OleDbCommand("SELECT *  FROM users"))
                {
                    command.Connection = connection;
                    connection.Open();

                    command.CommandText = "INSERT INTO users (who, payroll_number, ou, pool, team, rol_id) VALUES (@WHO, @PAYROLL_NUMBER, @NAME, @OU, @POOL @TEAM, @ROL_ID)";
                    command.Parameters.AddWithValue("@WHO", Txt_who.Text.Trim());
                    command.Parameters.AddWithValue("@PAYROLL_NAME", Txt_who.Text.Trim());
                    command.Parameters.AddWithValue("@OU", Txt_who.Text.Trim());
                    command.Parameters.AddWithValue("@POOL", Txt_who.Text.Trim());
                    command.Parameters.AddWithValue("@TEAM", Txt_who.Text.Trim());
                    command.Parameters.AddWithValue("@ROL_ID", Txt_who.Text.Trim());
                    command.ExecuteNonQuery();
                }
            }
        }
        catch (Exception)
        {
        }
    }

and the error that marks me is the following

  

Invalid postback or callback argument. Event validation is enabled   using in configuration or <% @   Page EnableEventValidation="true"% > in a page. For security   purposes, this feature verifies that arguments to postback or callback   events originate from the server control that originally rendered   them. If the data is valid and expected, use the   ClientScriptManager.RegisterForEventValidation method in order to   register the postback or callback data for validation.

    
asked by Cesar Gutierrez Davalos 08.05.2017 в 15:39
source

2 answers

1

If you can provide more information about the textbox of your footer, since at first glance you are not sending any parameter to your declared variables within the query.

command.Parameters.AddWithValue("@WHO", valuedeltxt);
command.Parameters.AddWithValue("@PAYROLL_NUMBER", valuedeltxt);
command.Parameters.AddWithValue("@OU", valuedeltxt);
command.Parameters.AddWithValue("@POOL", valuedeltxt);
command.Parameters.AddWithValue("@TEAM", valuedeltxt);
command.Parameters.AddWithValue("@ROL_ID", valuedeltxt);
    
answered by 08.05.2017 в 16:52
0

I see two things to correct:

  • You do not need to do SELECT to all users when you use the context using (OleDbCommand command = new OleDbCommand("SELECT * FROM users")) .
  • A validation if(!Page.IsPostBack){ ... } must be added to event dataGridView1_SelectedIndexChanged .

So that the code should be:

protected void dataGridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {   
        Button bt = (Button)sender;
        GridViewRow grdRow = (GridViewRow)bt.Parent.Parent;

        TextBox Txt_who = (TextBox)grdRow.Cells[0].FindControl("Txt_who");
        TextBox Txt_pay_roll = (TextBox)grdRow.Cells[1].FindControl("Txt_pay_roll");
        TextBox Txt_name = (TextBox)grdRow.Cells[2].FindControl("Txt_name");
        TextBox Txt_ou = (TextBox)grdRow.Cells[3].FindControl("Txt_ou");
        TextBox Txt_pool = (TextBox)grdRow.Cells[4].FindControl("Txt_pool");
        TextBox Txt_team = (TextBox)grdRow.Cells[5].FindControl("Txt_team");
        TextBox Txt_rol = (TextBox)grdRow.Cells[6].FindControl("Txt_rol");

        using (OleDbConnection connection = new OleDbConnection())
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                command.Connection = connection;
                connection.Open();

                command.Parameters.AddWithValue("@WHO", Txt_who.Text);
                command.Parameters.AddWithValue("@PAYROLL_NUMBER", Txt_pay_roll.Text);
                command.Parameters.AddWithValue("@NAME", Txt_name.Text);
                command.Parameters.AddWithValue("@OU", Txt_ou.Text);
                command.Parameters.AddWithValue("@POOL", Txt_pool.Text);
                command.Parameters.AddWithValue("@TEAM", Txt_team.Text);
                command.Parameters.AddWithValue("@ROL_ID", Txt_rol.Text);

                command.CommandText = "INSERT INTO users (who, payroll_number, ou, pool, team, rol_id) VALUES (@WHO, @PAYROLL_NUMBER, @NAME, @OU, @POOL @TEAM, @ROL_ID)";
                command.ExecuteNonQuery();
            }
        }

        BindGrid_With_Data();
    }
}

I do not know the data types but you may need a conversion, for example, for the ROL_ID field, since being a ID could mean that it is a INT :

command.Parameters.AddWithValue("@ROL_ID", Convert.ToInt32(Txt_rol.Text));
    
answered by 09.05.2017 в 22:18