Cannot insert a NULL value in an SQL table column that is defined as auto-increment

I have a table employee where id is defined as auto increment

circuit:

employee(id, name, company, salary, age)

insert into employee(name, company, salary, age) 
values('John', 'ABC', 90000, 30);

It works. However, doing the same through asp.net

SqlConnection xconn = new SqlConnection();
xconn.ConnectionString = @"";  //connection details go here
xconn.Open()

String query = "insert into employee(name, company, salary, age) values(@name, @company, @salary, @age)";
SqlCommand ycmd = new SqlCommand(query, xconn);
ycmd.Parameters.Add("@name", name);
ycmd.Parameters.Add("@company", company);
ycmd.Parameters.Add("@salary", salary);
ycmd.Parameters.Add("@age", age);
ycmd.ExecuteNonQuery();

It name, company, salary, agecontains the corresponding values.

I get an exception

Cannot insert null value in column id, null values ​​not allowed in column

+4
source share
2 answers

. , - , , . , , .

+1

, SqlCommand SQL , . SQL, , .

AddWithValue : .Add Parameters.AddWithValue. , , Add().

+1

All Articles