Click here to Skip to main content
15,904,339 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I need to generate automatic userid with sting AA at first, but i dont know how?
for example
it will be automaticaly filled in textbox when user input their personal data, can anybody help me?

i used code like this

MsUser g = new MsUser();
            g.UserID = textBox1.Text;
            g.UserName = textBox2.Text;
            g.UserEmail = textBox4.Text;
            g.UserAddress = richTextBox1.Text;
            g.UserPhone = textBox5.Text;
            g.UserRole = comboBox1.Text;
jgakenhe 22-May-15 13:35pm    
When you save the data, return the UserID from the database and then fill the textbox.

This way you'll make one trip to the database and you'll know if the record already exists or not.

The UserID will be the Integer Primary Key, then use a static string function to add the "AA00."

Never "fill in" a textbox: the value should only be assigned when the data is committed to the DB. If you try to decide before that point, as soon as you start working in a multiuser environment you will get intermittent problems as two stations end up trying to use the same userID. If you don't spot it, this can seriously compromise your database integrity, and you will have real problems.
And think about it: why the heck would a user want to know what his userID is going to be before he actually signs up? It's only relevant to anyone after he is a "full member".

So as jgakenhe said: use a IDENTITY field at the DB to allocate unique id numbers, and format them to display in C# or SQL:
string ID = string.Format("AA{0:D3}", idValueFromDB);

SELECT 'AA' + RIGHT('000' + CAST(IdValue AS NVARCHAR(3)),3) AS ID FROM MyTable
Share this answer
you can use primary key identity
Share this answer

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