|
-
February 16th, 2010, 09:27 PM
#1
Primary Key Error
I am making one client server application in which at server side I made tables using this code
Database db = server.Databases["dbo"];
nonqueryCommand.CommandText = "CREATE TABLE UserInfo (ID varchar(50),LoginName varchar(50),Date varchar(50),Time varchar(50) CONSTRAINT PKid PRIMARY KEY (ID))";
nonqueryCommand.ExecuteNonQuery();
nonqueryCommand.CommandText = "CREATE TABLE RAM ( ID varchar(50),PartNo integer,SerialNo integer,SizeMb integer ,SizeGB integer,TotalRam integer CONSTRAINT FKRid FOREIGN KEY(ID) REFERENCES UserInfo) ";
nonqueryCommand.ExecuteNonQuery();
and it works fine create the tables and link to primary and foriegn key too .
Error comes when i want to insert the data in the database named "dbo"
and this error comes when i enter the same data second time .
error is
Violation of PRIMARY KEY constraint 'PKid'. Cannot insert duplicate key in object 'dbo.UserInfo'.
The statement has been terminated.
and Inserting Code is
nonqueryCommand.CommandText = "INSERT INTO UserInfo([ID],[LoginName],[Date],[Time]) VALUES (@stloginname,@sthostname,@stdate,@sttime)";
nonqueryCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stloginname", stloginname));
nonqueryCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sthostname", sthostname));
nonqueryCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stdate", stdate));
nonqueryCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sttime", sttime));
nonqueryCommand.ExecuteNonQuery();
gives error at nonqueryCommand.ExecuteNonQuery();
-
February 17th, 2010, 04:16 AM
#2
Re: Primary Key Error
Sorry...I have to ask an obvious question here. Have you looked at the database to see if there is already a record with the same 'ID'? Are you by any chance doing multiple inserts through that code and passing the same 'ID'? Have you stepped through the application in debug mode to see what value the 'stloginname' variable has?
-
February 17th, 2010, 07:18 AM
#3
Re: Primary Key Error
Hi Nelo ,
Its basically a client Server Program in which it always update the value as the client restart the PC and again INSERT the same CLIENT ID .
After ur post I realise the problem that its due to Identity key , is there any solution of this that it didnt show that error if the same client value come again and replace the previous value with the new one ? I have date column too and willing to replace only if the PC restart on same date
-
February 17th, 2010, 07:51 AM
#4
Re: Primary Key Error
 Originally Posted by uahmed
Hi Nelo ,
Its basically a client Server Program in which it always update the value as the client restart the PC and again INSERT the same CLIENT ID .
After ur post I realise the problem that its due to Identity key , is there any solution of this that it didnt show that error if the same client value come again and replace the previous value with the new one ? I have date column too and willing to replace only if the PC restart on same date
You could do one of two things:
1. You can check to see if the client ID already exists. If it does, simply UPDATE the values that you want to change.
2. You can check to see if the client ID already exists. If it does, you can DELETE the record, then do the INSERT like normal.
===============================
My Blog
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|