Solved

How to generate a random unique 10 digit alphanumeric?

Posted on 2009-04-08
10
1,583 Views
Last Modified: 2013-12-17
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!
0
Comment
Question by:ipjyo
  • 6
  • 3
10 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 24102351
0
 
LVL 6

Accepted Solution

by:
SaedSalman earned 500 total points
ID: 24102354
Hello
change the MaxSize, MinSize, string 'a' (allowed symboles) to fit your need,
also follow-up link here:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_24001435.html

regrads
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();

  }

Open in new window

0
 

Author Comment

by:ipjyo
ID: 24102403
Thanks for the codes..I will try them and let you know soon.

Thanks!
0
 

Author Comment

by:ipjyo
ID: 24102489
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)>);  
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();

  }

Open in new window

0
 

Author Comment

by:ipjyo
ID: 24102531
I have added System.Security.Cryptography..now it is showing.
But I still dont get if there is any typo error in this
result.Append(chars1)>);  

Thank you!
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:ipjyo
ID: 24102613
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!


abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890

Open in new window

0
 
LVL 6

Assisted Solution

by:SaedSalman
SaedSalman earned 500 total points
ID: 24102760
Sorry, But I logged out.
Pre-Tested
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(chars[b % (chars.Length - 1)]); }

   return result.ToString();

  }

Open in new window

0
 

Author Comment

by:ipjyo
ID: 24102761
The code "http://snipplr.com/view/13357/random-string-generator" is working fine.
Thanks very much guru-sami
0
 

Author Comment

by:ipjyo
ID: 24102775
Hi SaedSalman,

Thank you for working for me..I would also like to save and use your code.
0
 
LVL 6

Expert Comment

by:SaedSalman
ID: 24102825
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
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now