I am developing an Windows pplication(using Visual Studio 2008,Sql server 2005). I have to maintain userlog information. I am having an identity column. It is working fine. But now the value of identity column is 172. Again when I try to log on I am getting the error as:
Violation of Primarykey pk_userLogId cannot insert duplicate values in an object’dbo.UserLog’.
The statement has been terminated.
When I close the error dialog box I am able to still use the application but the userlog is not being maintained.
What should I do to avoid getting this error message?
Is there any way I could set the maximum value for an identity column?
please help me out!
Thanks in advance!
My initial response was wrong because it was based on a misunderstanding about the log table schema. In view of Sheetal’s later posting of this schema (and my realizing that the name of the column was UserLogId, not UserLoginID…) this hypothesis is invalidated.
The log table is therefore,
making the OP question the more puzzling…
How can one get PK violation conditions with an identity column?
(unless the INDENTITY_INSERT setting for that table, and that manual edits/insert that include a value for the PK column, maybe Sheetal can shed some light on that… ? )
Sheetal should therefore look into the application’s logic, in the area of the code that completes the login, after the user is authenticated, and look for instance of queries where a value is specified for the UserLogId column (other than in WHERE clauses, of course).
— the following is wrong (based on wrong hypothesis) —-
The issue seems to be that the UserLoginId is declared as the Primary Key for the log table. This is a odd choice for such a table because we’d expect to have many log records for a given user.
In a SQL table, the primary key constraint essentially instructs SQL to refuse any INSERT request for a records with a Primary Key value that readily exist in the table. (And to return an error condition similar to the one mentioned in the question)
It’s hard to advise not knowing the log table schema and use case, but you can maybe remove the pk constraint altogether or use another column for it. An auto-incremented column called "EventId" could be a good choice for it. You’d get something like that in the log table:
etc…