Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unique Ident

Status
Not open for further replies.

WelshyWizard

IS-IT--Management
Apr 23, 2006
89
GB
Hey all,

I'm using the following code to create a uniquie ident on a windows app:
Code:
TextBox2.Text = DateTime.Now.ToString().GetHashCode().ToString("x")
This inserts my unique ident (based upon) current date time in textbox2 upon the loading of the form. There are only 4 users on this app and the chances of them using at exactly the same time are pretty remote. With this in mind, would people agree that this is an acceptable method of creating a unique identifier for both primary key purposes and also for the user to jot down the simple id for future reference.

The reason I don't use the VB.NET GUID is that it is soooooo long!

Cheers

Today is the tomorrow you worried about yesterday - and all is well.....
 
If you need to be positive that they are unique, you're going to have to check for their existence. You could create a table that held just these IDs, with that column being set to uinique/PK. When you generate a new ID, attempt to insert it into that table, if the insert fails with a duplicate value exception, generate a new ID and try again.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Why go through all of the trouble to create a unique primary key? Why not use an identity field?
 
I'm not sure about his situation, but I have an app that needs to generate random number strings for users to use as PINs. Using a random number instead of an Identity field (or the whole mess with Oracle's dual.next value crap) reduces the likelihood of someone spamming numbers at the system to gain access, or users accessing the system on another person's PIN.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Rick, the poster is trying to create a primary key as stated in his post. I hope you're not using PIN numbers as primary keys?

The reason a GUID is so long is that it takes that many bytes to gurantee (hopefully) uniqueness. Being that this is a primary key, its needs to be random and unique.

If you really need a random number, I would do it in the backend database and wrap it in a transaction so that if the same value has just been inserted, your .Net app is not making so many separate calls to the database.
 
Ahh, I didn't read it as he was generating his PKs off of this. And no, I don't use PINs as PKs (although some people do: )

Good idea on the server side generating. I'm working with an Oracle back end on this one though and my knowledge is somewhat limited, so I just threw it together on the ASP.Net side of the app. At this point, the performance isn't much of an issue, but as the user base grows and the likelihood of collisions increase, I can see it becoming a problem.

-Rick


VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top