Tags: char, constraint, database, dbo, duplicate, empty, fileds, guid, insert, key, microsoft, mysql, object, oracle, pk_tbltype, point, primary, primerykeyraf, recieve, row, server, sql, table, violation

Violation of PRIMARY KEY constraint PK_tblType. Cannot insert duplicate key in object dbo.

On Database » Microsoft SQL Server

3,607 words with 7 Comments; publish: Sun, 06 Jan 2008 08:52:00 GMT; (25078.13, « »)

the point here that i have a small table with two fileds,

ID (guid) as primerykey


and the table is empty when i add a new row i recieve this exception,

Violation of PRIMARY KEY constraint 'PK_tblType'. Cannot insert duplicate key in object 'dbo.tblType'.

i found no way to solve the problem.

thanks in advans

All Comments

Leave a comment...

    • Apparently you are a value that already exists. How are you generating the GUID?

      #1; Sun, 06 Jan 2008 08:53:00 GMT
    • Yes, i think the problem is faulty guid.

      Try this

      Dim _id As Guid = Guid.NewGuid()

      i cannot remember how but if you do something wrong in the guid, the database gives same values for rows.

      good luck

      #2; Sun, 06 Jan 2008 08:54:00 GMT
    • i forget to mention that the table is empty. and here is the code side

      tblRafTableAdapter mm =newtblRafTableAdapter();

      Guid guid =newGuid();

      guid =Guid.NewGuid();


      #3; Sun, 06 Jan 2008 08:55:00 GMT
    • hi

      check the InsertCommand in your tableAdapter by right click and choose properties

      also make sure the autoIncrement property for the ID column (inside the dataset) is set to false ,and readonly is set to false


      #4; Sun, 06 Jan 2008 08:56:00 GMT
    • i have nothing there, all clear. beside that the auto generated insert command is cleared and work fine. when i test the sql server by direct insert over the server, it works fine and insert the wonted values. beside i can create the guid by NEWID() over sql. but i can not add any value from the vs application. i tried that both way, by web application and by windows forms, and i have the same result

      #5; Sun, 06 Jan 2008 08:57:00 GMT
    • hey, i found out some strange thing, i tried to configure a custom adapter and a command to insert data. i configured the database string directly like this

      Data Source=.\\SQLEXPRESS;AttachDbFilename=" +"C:\\Documents and Settings\\developer\\My Documents\\Visual Studio 2005\\Projects\\PolKitap\\PolKitap\\mylibrary.mdf" +";Integrated Security=True;User Instance=True";

      and after that bingo the data is inserted, when i use the auto generated database string which is like this,

      Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\mylibrary.mdf;Database=mylibrary; Integrated Security=True;User Instance=True"

      the system throw the known exception, guys any idea about this situation!

      thanks again

      #6; Sun, 06 Jan 2008 08:58:00 GMT
    • hi

      to make the last connection string works

      you must put your datafile (.mdf) inside a App_Data inside your project

      note that in the runtime ,the DataDirectory in ( AttachDbFilename=|DataDirectory| )will be replaced by the App_Data special folder ,

      so add the App_Data folder then add your db file to it ,

      to add App_Data folder , right click on the website and select add asp.net Folder ->select App_Data


      #7; Sun, 06 Jan 2008 08:59:00 GMT