Problem with System.Guid and duplicate key

J

Jeff

ASP.NET 2.0

This problem occur using the basic version of SQL Server 2005 which get
installed when installing Visual Studio 2005!

I have inserted a record in the Test table. But when I try to insert a
second record I get this error:
{"Violation of PRIMARY KEY constraint 'PK_Test'. Cannot insert duplicate key
in object 'dbo.Test'.

The primary key value in the only record in the table Test has this value
00000000-0000-0000-0000-000000000000

Here you see how the Id value is calculated in my problem code. I thought
"new System.Guid()" should use a combination of datetime and the MAC address
on network card to generate a truly unique value. But it looks I'm wrong
about that.
TestInfo test = new TestInfo(new System.Guid(), "test value");

This is the definition of this Test table
create table Test (
Id uniqueidentifier not null,
Value nvarchar(2000),
CONSTRAINT PK_Test PRIMARY KEY (Id)
)

"new System.Guid()" don't generate a unique value, what should I do to make
it generate a unique value???

Best Regards!

Jeff
 
K

Konstantinos Pantos

You should use the NewGuid static method of System.Guid class. For example
using System;

class Sample
{
public static void Main()
{
Guid g;
// Create and display the value of two GUIDs.
g = Guid.NewGuid();
Console.WriteLine(g);
Console.WriteLine(Guid.NewGuid());
}
}

/*
This code example produces the following results:

0f8fad5b-d9cb-469f-a165-70867728950e
7c9e6679-7425-40de-944b-e07fc1f90ae7

*/


HTH,

--
Konstantinos Pantos,
Software Engineer
Microsoft MVP [ASP.NET]
http://kostas.pantos.name
http://blog.pantos.name
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top