473,382 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Problem with System.Guid and duplicate key

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
Apr 11 '07 #1
1 5680
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
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

Apr 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Louis Frolio | last post by:
Greetings All, I have read many upon many articles here regarding GUID data types and uniqueness. There have been many opinions regarding the effectiveness of GUID's and when they should/should...
3
by: Robert Chapman | last post by:
I know that this should be impossible, yet it is happening... I have some c# code that uses a method scoped variable of type Guid. The code assigns Guid.NewGuid() to the variable. This value is...
2
by: Jiho Han | last post by:
I generated a class from a schema. One of the fields are typed as System.Guid. Perfect. The only problem is when this class serializes, the guid field serializes as...
10
by: www.dir | last post by:
Hi, The following is from MSDN. "A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a...
3
by: Ravi | last post by:
Hi, I have a simple .aspx page where user enters data (e.g Name address, etc). At the end, he/she clicks Save button which Posts to the same page and in the Save button's click event I am saving...
16
by: Jay Douglas | last post by:
Does anybody know the odds of generating a duplicate guid? -- Jay Douglas http://jaydouglas.com
0
by: Slawomir Nasiadka | last post by:
Hi, I'am new to this group so I would like to say "Hello" everyone and here is my problem: I'm writing a simple application (code is at the end of this message) witch would list all mails...
2
by: =?Utf-8?B?SmFjb2I=?= | last post by:
I am trying to use an XSD generated function which needs a parameter of type 'ref System.Guid?' I am passing it a 'ref System.Guid', but am getting the following error: "cannot convert from 'ref...
7
by: tatata9999 | last post by:
Hi, SQL env: sql server 2000 Target column of insertion: varchar(15) Case, a var is made up of a character of the following characters (random selection):...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.