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 'Funcionario' can only be specified when a column list is used and IDENTITY_INSERT is ON.'
Code of the C#:
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 enter the table fields, 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 don’t understand why I made a mistake, since cod_Func
is identity, that is, we cannot set its value.
your command would not have to be
INSERT INTO Funcionarios (COLUNAS) values (VALORES_A_SEREM_INSERIDOS)
?– R.Santos
The error is self-explanatory: An explicit value for the identity column in the table 'Functio' can only be specified when a column list is used and IDENTITY_INSERT is ON
– Rovann Linhalis
@Rovannlinhalis That’s right, even though I leave IDENTITY_INSERT as ON, it keeps giving the same error
– Daniel Santos
@R.Santos I tried it that way you told me, only it displays the following message : "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."
– Daniel Santos
The number of columns and values you are passing in the Insert command are different this is the error now, show how your INSERT now with the columns
– R.Santos
@R.Santos I entered some more information in the question, but it still gives the same error ...
– Daniel Santos
Do a test try to change the structure of your table by changing the
cod_Func
to integer and then try to enter a record with an employee code together, to see if there is an error– R.Santos
I already did that, it managed to enter the data normally, it only gives error when changing the cod_Func to Identity
– Daniel Santos
Your idea is that cod_func is something unique and auto_incremet that?
– R.Santos