Tenho uma aplicação web no visual studio onde estou recebendo a seguinte mensagem sempre que tento dar um comando insert
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.'
Código do 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();
Estrutura da tabela :
create table Funcionario(cod_Func int identity (1,1),nome varchar (50),salarioBase decimal,inss decimal,irrf decimal,qtdHoraExtra int,dependente int,salarioLiquido decimal);
Se eu tento inserir especificando os campos da tabela, o erro passa a ser outro
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();
Erro:
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.'
Não entendo por que do erro, visto que cod_Func
é identity, ou seja, não podemos setar seu valor.
INSERT INTO Funcionarios (COLUNAS) values (VALORES_A_SEREM_INSERIDOS)
?