0

I have a table in my database that has an auto-increment Id column. I want to write a query to insert values from textboxes and comboboxes. I know that it should be easy, but my problem is that the table contains an id column that has auto-increment feature, so how can I add values to my table?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    Don't insert a value for the id column (it won't let you). See this: http://stackoverflow.com/questions/3493612/how-to-insert-new-row-to-database-with-auto-increment-column-without-specifying – bgura May 10 '17 at 13:33
  • how do you insert the values into the database? ADO ? LinqToSQL ? Entity? ... – Mong Zhu May 10 '17 at 13:36
  • 2
    Show your code, sample data, and table structures. Complete Questions will help us understand your problem better. – SS_DBA May 10 '17 at 13:36

2 Answers2

1

If you want the ID to be maintained by the DB just don't mention it in the insert statement.

or if you want to add your own ID (not recommended) you need to enable identity insert then do the insert then disable it again

SET IDENTITY_INSERT mytable ON

-- insert statement here

SET IDENTITY_INSERT mytable  OFF
RegBes
  • 554
  • 3
  • 11
0

you can try to update your database column Id and set it to Identity autoincrement on SQL. Then on your insert don't worry about the Id column, SQL will add the value for you.

for example :
Table Person
ColumnId
ColumnName
ColumnLastName

you only have to add the value for ColumnName and ColumnLastName. for EF etc. you may need to do more to make it work. Please post your code snippet so we can help you better.

Drew Aguirre
  • 375
  • 2
  • 15