Click here to Skip to main content
15,884,472 members
Home / Discussions / ASP.NET
   

ASP.NET

 
QuestionGenerating unique user ID Pin
Otekpo Emmanuel7-Jul-20 7:29
Otekpo Emmanuel7-Jul-20 7:29 
AnswerRe: Generating unique user ID Pin
Richard Deeming7-Jul-20 7:47
mveRichard Deeming7-Jul-20 7:47 
GeneralRe: Generating unique user ID Pin
Otekpo Emmanuel8-Jul-20 3:06
Otekpo Emmanuel8-Jul-20 3:06 
GeneralRe: Generating unique user ID Pin
Richard Deeming8-Jul-20 3:42
mveRichard Deeming8-Jul-20 3:42 
GeneralRe: Generating unique user ID Pin
Mycroft Holmes8-Jul-20 12:46
professionalMycroft Holmes8-Jul-20 12:46 
AnswerRe: Generating unique user ID Pin
David Mujica10-Jul-20 2:47
David Mujica10-Jul-20 2:47 
GeneralRe: Generating unique user ID Pin
jkirkerx13-Jul-20 11:25
professionaljkirkerx13-Jul-20 11:25 
GeneralRe: Generating unique user ID Pin
DerekT-P17-Jul-20 10:44
professionalDerekT-P17-Jul-20 10:44 
Sorry, but I downvoted this - for a number of reasons:
* It requires an additional two physical i/o calls - to get the latest value, and to update it afterwards
* It requires that you have a READ lock on the sequence table; to avoid duplicates, you must read the value, insert the new record, and update it all within a transaction and without allowing any other process to read the value. At best this requires an additional lock, but at worst - if poorly coded - can leave that lock in place for a prolonged period and cause a major performance bottleneck
* It creates sequential user ids; presumably you have secure password / two-factor authentication, but by using sequential numeric ids you're making it very easy for hackers as they can just generate sequential hacks on the id

If sequential IDs isn't an issue (and it may not be in all cases) the simplest thing is to use an auto-increment field and return the new ID from the insert statement. Any decent DBMS will keep track without issue. In the event of a transaction rollback there may be a "missing" ID but that shouldn't (be allowed) to cause your application a problem.

A method I use when generating IDs is to use a GUID value (or sometimes a truncated portion of a GUID) and simply INSERT into the table. With a unique key on the ID, then in the vanishingly small likelihood of a duplicate, the INSERT will fail. Catch the "duplicate record" exception, replace the ID with a new GUID and insert again. The performance hit from that is miniscule as it will probably never ever happen.
QuestionMessage Removed Pin
6-Jul-20 23:46
umeshamin6-Jul-20 23:46 
AnswerMessage Removed Pin
13-Jul-20 11:39
professionaljkirkerx13-Jul-20 11:39 
Questionpls help Pin
Member 148797912-Jul-20 21:44
Member 148797912-Jul-20 21:44 
SuggestionRe: pls help Pin
Richard MacCutchan2-Jul-20 21:48
mveRichard MacCutchan2-Jul-20 21:48 
GeneralRe: pls help Pin
ZurdoDev6-Jul-20 8:10
professionalZurdoDev6-Jul-20 8:10 
AnswerRe: pls help Pin
David Mujica6-Jul-20 7:27
David Mujica6-Jul-20 7:27 
JokeRe: pls help Pin
DerekT-P7-Jul-20 2:18
professionalDerekT-P7-Jul-20 2:18 
QuestionAngular service error http 400 using .Net core api to accept json, escaped quotes crashing api Pin
jkirkerx29-Jun-20 12:48
professionaljkirkerx29-Jun-20 12:48 
AnswerRe: Angular service error http 400 using .Net core api to accept json, escaped quotes crashing api Pin
Richard Deeming29-Jun-20 23:42
mveRichard Deeming29-Jun-20 23:42 
GeneralRe: Angular service error http 400 using .Net core api to accept json, escaped quotes crashing api Pin
jkirkerx30-Jun-20 6:06
professionaljkirkerx30-Jun-20 6:06 
Questionerror server Pin
Member 1265427722-Jun-20 2:37
Member 1265427722-Jun-20 2:37 
AnswerRe: error server Pin
ZurdoDev22-Jun-20 2:44
professionalZurdoDev22-Jun-20 2:44 
AnswerRe: error server Pin
Richard Deeming22-Jun-20 5:19
mveRichard Deeming22-Jun-20 5:19 
GeneralRe: error server Pin
Member 1265427722-Jun-20 23:37
Member 1265427722-Jun-20 23:37 
AnswerRe: error server Pin
Member 1265427722-Jun-20 23:41
Member 1265427722-Jun-20 23:41 
AnswerRe: error server Pin
OriginalGriff22-Jun-20 23:51
mveOriginalGriff22-Jun-20 23:51 
GeneralRe: error server Pin
Member 1265427723-Jun-20 1:34
Member 1265427723-Jun-20 1:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.