Hello Experts,

I have a web application build with asp.net(c#) and SQL Server 2005. Where user can enter customer record. But my problem is that, whenever end user try to enter record from two different places at the same time, it shows an error for violation of primary key constrain. How can I solve this? Both the end user are from different location and should be able to enter record at the same time. Any solution? Please help me out.

Im not to sure if your Pk is a Integer/String,but try creating a function that returns a number for PK(append int to string if ur PK includes both),this will at least ensure that only one integer is returned...Insert this Pk to the DB

that will never happen...
even if 2 users try to insert a record at the same time ( which u said )
there will be gap of milli seconds.

This error is possiblem while updating a record.
i mean you are editing a record but not saved yet.

on other side the other user will edit the same record.
and user will update the record.

but when the first user try to update the record.
user will recv an error u have mentioned above.

also show me your code..

How are your primary keys generated? Are you using some SQL code to get a new primary key value? Something like select max(id) from table ?

Take a look at identity columns in SqlServer. It will generate a unique number for you and the database guarantees that even if two records are inserted at seemingly the same time, the identity values generated for two records will be different.

And I don't think that this error could happen in the situation described by dnanetwork. It is perfectly fine to update the same record twice in a very short period of time. Both queries would update a record with the same primary key and this should not throw any errors. It is true that the data from the first update would be overwritten by the second update, but that is not an error as far as the database is concerned.

Well Thanks a lot for replying in this thread. I had the problem while inserting and that is solved by using row level lock while inserting.

cool :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.