Click to See Complete Forum and Search --> : Primary Key Error


uahmed
February 16th, 2010, 08:27 PM
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();

nelo
February 17th, 2010, 03:16 AM
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?

uahmed
February 17th, 2010, 06:18 AM
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

eclipsed4utoo
February 17th, 2010, 06:51 AM
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.