CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2009
    Posts
    16

    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();

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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?

  3. #3
    Join Date
    Oct 2009
    Posts
    16

    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

  4. #4
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Primary Key Error

    Quote Originally Posted by uahmed View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured