Solved

How to generate a random unique 10 digit alphanumeric?

Posted on 2009-04-08
10
1,618 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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
 

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add a Condition in ASP Repeater 6 48
Please explain purpose of GZIP 4 60
Need to sort columns in DataGridView 4 38
PowerShell: Adding ToGB to a script 4 21
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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