ipjyo
asked on
How to generate a random unique 10 digit alphanumeric?
Hello Experts,
Could anybody please post me a code snippet to generate a random and unique 10 digit alphanumeric in C#? I need to insert this 10 digit alphanumeric into a primary key column of a table. Any help is greatly appreciated! Thanks!
Could anybody please post me a code snippet to generate a random and unique 10 digit alphanumeric in C#? I need to insert this 10 digit alphanumeric into a primary key column of a table. Any help is greatly appreciated! Thanks!
check this: http://snipplr.com/view/13357/random-string-generator/
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the codes..I will try them and let you know soon.
Thanks!
Thanks!
ASKER
Hi,
First I am trying the below code. Could you tell me if I need to import any namespace to support RNGCryptoServiceProvider and its methods? because my visual studio is not suppoerting this class.
Can you please clarify?
And also I am not able to understand the following line. Thanks!
result.Append(chars1)>);
First I am trying the below code. Could you tell me if I need to import any namespace to support RNGCryptoServiceProvider and its methods? because my visual studio is not suppoerting this class.
Can you please clarify?
And also I am not able to understand the following line. Thanks!
result.Append(chars1)>);
private string GetUniqueKey()
{
int maxSize = 10 ;
int minSize = 10 ;
char[] chars = new char[62];
string a;
a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
chars = a.ToCharArray();
int size = maxSize ;
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data) ;
size = maxSize ;
data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size) ;
foreach(byte b in data )
{ result.Append(chars1)>); }
return result.ToString();
}
ASKER
I have added System.Security.Cryptograp hy..now it is showing.
But I still dont get if there is any typo error in this
result.Append(chars1)>);
Thank you!
But I still dont get if there is any typo error in this
result.Append(chars1)>);
Thank you!
ASKER
I tried to give
result.Append(chars1);
I have got an error like "there is no chars1 variable defined". When I tried to give it as
result.Append(chars);
it generated a long string as shown below.
Could you please help me what I have missed here.
Thanks!
result.Append(chars1);
I have got an error like "there is no chars1 variable defined". When I tried to give it as
result.Append(chars);
it generated a long string as shown below.
Could you please help me what I have missed here.
Thanks!
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The code "http://snipplr.com/view/13357/random-string-generator" is working fine.
Thanks very much guru-sami
Thanks very much guru-sami
ASKER
Hi SaedSalman,
Thank you for working for me..I would also like to save and use your code.
Thank you for working for me..I would also like to save and use your code.
You are welcom,
> I would also like to save and use your code.
actually not mine ;-)
http://www.codeproject.com/KB/aspnet/UniqueKeys.aspx?display=PrintAll&fid=315463&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2493319
Best Regards,
Saed Salman
> I would also like to save and use your code.
actually not mine ;-)
http://www.codeproject.com/KB/aspnet/UniqueKeys.aspx?display=PrintAll&fid=315463&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2493319
Best Regards,
Saed Salman