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.

farhat
  • 84
  • 3
  • 8

2 Answers2

3

Guid.NewGuid() is almost unique.

Usage:

Guid id = Guid.NewGuid();

// Output: e62e2706-a8db-4fae-891a-65985fd80351
Win
  • 61,100
  • 13
  • 102
  • 181
  • How to use this method..could u please tel me – farhat Oct 10 '13 at 04:52
  • I updated the answer. If you want string value, you can use `id.ToString();`. C# Guid is equivalent to SQL uniqueidentifier. – Win Oct 10 '13 at 04:56
  • Well, at some point, you might get duplicate, but chances are zero to none. See this [question](http://stackoverflow.com/a/1705517/296861). – Win Oct 10 '13 at 05:01
  • 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. – farhat Oct 10 '13 at 05:08
  • 1
    Sounds more like you want a hashing function. Check out http://stackoverflow.com/questions/11454004/calculate-a-md5-hash-from-a-string – geedubb Oct 10 '13 at 05:22
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.

Kenneth Hampton
  • 685
  • 6
  • 7