buddy
buddy

Reputation: 428

"Violation of PRIMARY KEY constraint” when Inserting in database

I'm trying to insert a record in database from a winform it was working fine and insert first 5 records with no error but when i try to insert next record it give me the error of Violation of PRIMARY KEY....!

The table is

enter image description here But in Database table there is not record of no 6. There is no trigger associate to this table. there is no F-Key relationship of that table. I tried it from sql server to insert it but again error. What is the reason behind it?

Upvotes: 1

Views: 1451

Answers (4)

Iti Tyagi
Iti Tyagi

Reputation: 3671

Primary key musty be inserted in table by itself, as this also reduces the code error chances that sometimes we try to insert same key again and again, (which is not the property of PK).

Also in database:

 table> design> Primary Key> Properties>Identity specification> Yes

This will enable self insertion of key, also you can specify start index.

Upvotes: 0

user2968347
user2968347

Reputation: 21

Primary key should be auto generated.if you are getting this error then check your data first.

Upvotes: 0

Monika
Monika

Reputation: 2210

You can fix this error by not trying to insert rows with a duplicate primary key.

Upvotes: 0

Colt
Colt

Reputation: 144

There is definitely something wrong with your data. Please make sure you don't have a "duplicated PK" inserted.

And in my opinion, the PK should be generated automatically unless you have some special requirements.

Upvotes: 2

Related Questions