Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi Sir/Mam
I'am a fresher and i have an application that requires to generate a unique alphanumeric id automatically(with out duplicates).and i want to customize the alphabets to my own choice.
For example:
if I need to generate a admission number for a student
if the college name is sathya the id should look like -------- sa00001
-------- sa00002
and they should be unique and never generate a duplicate.How?


Please Help me Out
Thanks In advance
Posted
Updated 18-Dec-13 18:25pm
v2

First of all get the last id of the table from database.
Then parse it
e.g. in your situation:
C#
var numAlpha = new Regex("(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)");
            var match = numAlpha.Match("sa00001");
            var alpha = match.Groups["Alpha"].Value;
            var num = match.Groups["Numeric"].Value;
            var number = Convert.ToInt32(num);
            ++number;
            var nextId = alpha;
            var length = num.Length - number.ToString().Length;
            if (length != 0)
            {
                var format = "D" + length;
                nextId += number.ToString(format);
            }
else{
nextId+=number.ToString();
}


Now you have next id do whatever you can do with it
 
Share this answer
 
v3
Comments
raxhemanth 19-Dec-13 2:14am    
Thankyou Hammad thanks a lot.
Hammad 19-Dec-13 4:23am    
You are welcome buddy. It's Great to know that it helps..
 
Share this answer
 
Generate a unique ID using GUID and then append the string (of your combination to the Uniquely generated key) like this a post in soverflow[^]

I think this will resolve your issue.
 
Share this answer
 
So, you should always retrieve the last inserted ID from the Database table.
Increment it and insert it for the next College.
 
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