Click here to Skip to main content
15,895,794 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need Student Admission number in auto generation i used Guid but it's very large random numbers

i need only first 4 values letters next 8 numbers
pls help ?

Guid id = Guid.NewGuid();
Posted
Updated 19-Oct-14 23:37pm
v2
Comments
Abhijit Ghosh (Subho) 20-Oct-14 5:41am    
Why not create your own using Random class? If your problem is that the generated guid is large, just extract a substring from it. If you need a autogenerated number with custom specifications, you have to build your own.
CHill60 20-Oct-14 6:29am    
Why not just use an Identity column on your database? Uniqueness comes for free. Not sure why everyone is focused on random numbers
Prasanth Radhakrishanan 20-Oct-14 6:34am    
I'm used identity column but this for another Admission Id auto generation i need first format is "oxfod1001"
CHill60 21-Oct-14 8:22am    
See my solution to your other post.

You can do something like this.

var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var nums = "0123456789";
var stringChars = new char[4];
var stringNums = new char[8];
var random = new Random();

for (int i = 0; i < stringChars.Length; i++)
    stringChars[i] = chars[random.Next(chars.Length)];
for (int i = 0; i < stringNums.Length; i++)
    stringNums[i] = nums[random.Next(nums.Length)];
var finalString = new char[stringChars.Length + stringNums.Length];
stringChars.CopyTo(finalString, 0);
stringNums.CopyTo(finalString, stringChars.Length);


The code block presented above will give you a randomized string with 4 alphabets followed by 8 numbers. finalString contains the generated string. Hope this helps.


-Subho
 
Share this answer
 
Comments
Prasanth Radhakrishanan 20-Oct-14 6:29am    
finalString return null not working
Abhijit Ghosh (Subho) 20-Oct-14 6:33am    
Must be something at your end. I just tested this. Works fine.
Prasanth Radhakrishanan 20-Oct-14 6:36am    
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
..
..
..
var finalString = new char[stringChars.Length + stringNums.Length];
..
..

TextBox1.Text = finalString.ToString();
i tested
Abhijit Ghosh (Subho) 20-Oct-14 6:44am    
If you didn't notice, FinalString is a char array.
Remove: TextBox1.Text = finalString.ToString();
Add: TextBox1.Text = new string(finalString);
Prasanth Radhakrishanan 20-Oct-14 6:51am    
its working
C#
static class StringExt {

public static IEnumerable<String> SplitInParts(this String s, Int32 partLength) {

    for (var i = 0; i < s.Length; i += partLength)
      yield return s.Substring(i, Math.Min(partLength, s.Length - i));
  }
}
var parts = System.Guid.NewGuid().ToString().Replace("-","").SplitInParts(4);

But remember for retaining the uniqueness you need to have the 16 digits. You can also use guid.ToString("N") for getting number without '-', please try and use the whole GUID it is made for a purpose. :)
Please try this and post back your comments if this works out.
Thanks.
 
Share this answer
 
v3
If you really need a GUID, that is a Global Unique Identifier [^], then you have to retain all its length.
On the other hand, if your requirements aren't so strict (e.g. you can drop the 'global' requirement) then you may generate a shorter random id.
 
Share this answer
 
This would be helpful : http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server[^]

In this case, you will be generating numbers in SQL, not in code.
 
Share this answer
 
v2

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