Sunil Patel
Sunil Patel

Reputation: 265

How to insert data in Asp.net page when the table has an identity column

I'm getting error when I'm inserting data in database into a table with an identity specification.

SQL

string query = "insert into student values('"+txtname.Text+"','"+name_image+"','"+txtage.Text+"','"+txtaddress.Text+‌​"')";

Error is

Column name or number of supplied values does not match table definition.

Upvotes: 1

Views: 225

Answers (2)

Sourabh Sharma
Sourabh Sharma

Reputation: 8322

Dont Try to Fill the auto-Increment column. Provides the column value other than auto-increment column.

suppose Column-1 is Auto-incremented then the query will be

Insert into tableName(Column-2,column-3,..) Values(val-2,val-3,..);

Leave the auto increment column

Upvotes: 1

marc_s
marc_s

Reputation: 754488

You should get in the habit of always specifying the explicit list of columns you want to insert values into - that way, you can omit the identity column and prevent this error:

INSERT INTO dbo.Student(Col1, Col2, ...., ColN)
VALUES(Val1, Val2, ...., ValN)

Just saying INSERT INTO dbo.Students VALUES(...) means you have to provide a value for every column - including the identity column - which you cannot provide a value for!

Upvotes: 3

Related Questions