Question

I am collecting first name and last name from a user in a registration process. I want to generate a unique registration code using first name and last name without checking in the database for all the existing codes. John Cano can generate something like JC76AS1 where JC if from John Cano .

What could be the best way to generate the code part 76AS1 without check the database for existing code and trying to don't create a duplicated a registration code?

Was it helpful?

Solution

I am not sure if it would be possible to do that without checking it in the database or maintaining a reference of already generated registration codes in memory.

You can use GUID and append that to FirstName's first character and last name's first character. But it will be long, but unique.

string firstName = "John";
string lastName = "Cano";
string uniqueRegistrationCode = string.Format("{0}{1}-{2}", firstName[0], lastName[0], Guid.NewGuid());

and you would get back something like:

JC-3f507a3c-cd80-482d-b3be-c8002856926e

(this assuming that there is at least one character in first and last name)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top