INSERT command error in Visual Studio

0

I have a web application in visual studio where I am getting the following message whenever I try to give an insert command

  

System.Data.SqlClient.SqlException: 'An explicit value for the identity column in table'

C # code:

SqlDataSource1.InsertCommand = "SET IDENTITY_INSERT Funcionario ON " +
            "INSERT INTO Funcionario VALUES ('" + func.nome + "'," + func.salarioBase + "," + func.inss + "," + func.irrf + "," + func.hrsExtras + "," + func.dependentes + "," + func.salarioLiquido + ")" +
            "SET IDENTITY_INSERT Funcionaio OFF";
SqlDataSource1.Insert();

Table structure:

create table Funcionario(cod_Func int identity (1,1),nome varchar (50),salarioBase decimal,inss decimal,irrf decimal,qtdHoraExtra int,dependente int,salarioLiquido decimal);

If I try to insert specifying the fields of the table, the error becomes another

Insert:

SqlDataSource1.InsertCommand = "SET IDENTITY_INSERT Funcionario ON " +
            "INSERT INTO Funcionario (nome,salarioBase,inss,irrf,qtdHoraExtra,dependente,salarioLiquido) VALUES ('" + func.nome + "'," + func.salarioBase + "," + func.inss + "," + func.irrf + "," + func.hrsExtras + "," + func.dependentes + "," + func.salarioLiquido + ")";
SqlDataSource1.Insert();

Error:

  

System.Data.SqlClient.SqlException: 'There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement. '

I do not understand why the error, since cod_Func is identity , that is, we can not set its value.

    
asked by anonymous 17.11.2017 / 17:52

3 answers

2

You set SET IDENTITY_INSERT to ON but do not pass cod_Func to Insert command.

According to the documentation: link

If you have enabled the option to insert the Identity column, you must pass it on.

    
17.11.2017 / 18:40
1

First you must declare the columns that will be inserted in your insert statement. Another very important thing, never concatenate SQL statements like you are doing, if you concatenate your SQL statements, your system will be vulnerable to SQL Injection

With SqlDataSource I do not know how it looks, but I'm putting an example in my answer using SqlCommand .

using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLExpress;Initial Catalog=StackOverflow;Integrated Security=True"))
{
    conn.Open();
    string insert = @"INSERT INTO [dbo].[Funcionario]
                            ([nome]
                            ,[salarioBase]
                            ,[inss]
                            ,[irrf]
                            ,[qtdHoraExtra]
                            ,[dependente]
                            ,[salarioLiquido])
                        VALUES
                            (@nome
                            , @salarioBase
                            , @inss
                            , @irrf
                            , @qtdHoraExtra
                            , @dependente
                            , @salarioLiquido)";
    SqlCommand command = new SqlCommand(insert, conn);
    command.Parameters.Add(new SqlParameter("nome", func.nome));
    command.Parameters.Add(new SqlParameter("salarioBase", func.salarioBase));
    command.Parameters.Add(new SqlParameter("inss", func.inss));
    command.Parameters.Add(new SqlParameter("irrf", func.irrf));
    command.Parameters.Add(new SqlParameter("qtdHoraExtra", func.qtdHoraExtra));
    command.Parameters.Add(new SqlParameter("salarioLiquido", func.dependente));
    command.Parameters.Add(new SqlParameter("dependente", func.dependente));

    command.ExecuteNonQuery();
}
    
17.11.2017 / 18:41
0

In my code there were two errors:

1 - I was using the simpler variation of the INSERT command, where I would just need to specify the table and values ( INSERT INTO TABLE VALUES (X) ), and that was a mistake, simpler does not take into account Identity fields, including them in the columns to be populated;

2 - The SET IDENTITY_INSERT Tabela ON/OFF command tells the program whether Identity fields should be taken into account or not. Consider the following statement:

SqlDataSource1.InsertCommand = "SET IDENTITY_INSERT Funcionario ON " +
        "INSERT INTO Funcionario (nome,salarioBase,inss,irrf,qtdHoraExtra,dependente,salarioLiquido) VALUES ('" + func.nome + "'," + func.salarioBase + "," + func.inss + "," + func.irrf + "," + func.hrsExtras + "," + func.dependentes + "," + func.salarioLiquido + ")";
SqlDataSource1.Insert();

So, if you see that Identity fields MUST be taken into account, however, I do not assign any value to it in the VALUES column, hence the error of having fewer fields than the specified.

Finally, it is necessary to change only the ON to the OFF that will work normally

    
17.11.2017 / 18:41