Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a project that accept users input and submit it to the database,an e-mail is sent to the user confirming there registration, as unique user_id is assign to each user, which is required to successfully log in to the webpage.

I want to automatically generate an a unique value that will be sent to each user that successfully registered.

1.Any help like a demo connectionString and the command it welcome.

Thanks
Posted
Updated 4-Jan-11 11:50am
v2

We can't supply a connection string, it is pretty much unique to your database (in terms of type, server, default table and security.
This[^] is the website I use for all things connection string related.

As for creating the unique identifier, you should do this in the database table rather than code if possible. When you define the primary key you should define it as an identity column[^].

Once you've inserted the user, the generated key is available by calling the ScopeIdentityMethod(): This[^] has some good examples.
 
Share this answer
 
Comments
wizardzz 4-Jan-11 16:46pm    
As you mentioned, primary key is not a preferred user id.
You want to create a random string to use as a unique user name?

This has been answered here:
How to Generate Random String in C#?[^]

I think a great way to secure a unique id string without extra hit on the db is to retrieve the primary key as an integer, add a salt, and hash it to create a user name.
 
Share this answer
 
v2
Comments
Keith Barrow 4-Jan-11 16:38pm    
This is a poor method, each check would required a database hit which is costly, but there *are* good reasons to use a random ID rather than a generated int. See http://www.codinghorror.com/blog/2007/03/primary-keys-ids-versus-guids.html
Keith Barrow 4-Jan-11 16:39pm    
You can generate a new GUID on insert in SQL Server using
select newid()
Uniqueness isn't guaranteed, but the likelihood of two identical GUIDs is vanishingly small.
wizardzz 4-Jan-11 16:51pm    
I would stay away from the coding horror of primary keys as id at all costs. The GUID does not guarantee uniqueness so the perf hit may be necessary. Check the updated answer.
Keith Barrow 4-Jan-11 17:00pm    
A) I disagree that Identity fields are a coding horror, they've worked fine for most people in the past, but I prefer natural keys anyway.
Keith Barrow 4-Jan-11 17:07pm    
B) The chances of two GUIDs being identical is astronomically low, there are 2 ^ 128 guids available, so you need the order of a quadrillion *existing* guids to make it remotely likely two would collide.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900