2

how to create unique id for each user in asp.net C#

I was used random number for each profile but its not unique.

4
  • 1
    Why not leave that to an Identity field in the database? Commented Oct 10, 2013 at 4:49
  • u mean to say autonumber? Commented Oct 10, 2013 at 4:53
  • Yes, depending on the RDBMS you could say autonumber X-) Commented Oct 10, 2013 at 4:53
  • okk but i want to create id like suposse personname is shaikh and city is pune,state is maharashtra, country is india then combination of all this field like INMHPU123333......? Commented Oct 10, 2013 at 4:58

2 Answers 2

3

Guid.NewGuid() is almost unique.

Usage:

Guid id = Guid.NewGuid();

// Output: e62e2706-a8db-4fae-891a-65985fd80351
Sign up to request clarification or add additional context in comments.

5 Comments

How to use this method..could u please tel me
I updated the answer. If you want string value, you can use id.ToString();. C# Guid is equivalent to SQL uniqueidentifier.
Well, at some point, you might get duplicate, but chances are zero to none. See this question.
okk but i want to create id like suposse personname is shaikh and city is pune,state is maharashtra, country is india then combination of all this field like INMHPU123333.
Sounds more like you want a hashing function. Check out stackoverflow.com/questions/11454004/…
1

If you’re using SQL Server (I’m pretty sure something similar exists in Oracle and MySQL but I can’t confirm) you can use identity column with integer value that will automatically generate unique ID for new user.

If you need something different like you shown in one of the comments “INMHPU123333” then you’ll need to create a separate function for this.

Note that this is not really ideal from the performance perspective because it will cause big index fragmentation. Not a huge deal though if you don’t have large number of records.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.