Total Pageviews
Wednesday, July 1, 2009
True Random and Unique AlphaNumeric Number Generator
public string GetRandomUniqueAlphaNumericCode(int length, bool lowerCase)
{
int maxSize = 8;
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)]);
}
strCode = (result.ToString()).ToUpper();
bool flag = false;
for (int i = 0; i < strCode.Length; i++)
{
if (char.IsDigit(strCode, i))
{
flag = true;
}
}
if (flag == false)
{
GetRandomUniqueAlphaNumericCode();
}
return strCode;
}
here strCode is static class variable.
this will generate random and unique alphanumeric string.
Subscribe to:
Post Comments (Atom)
Blog Archive
Ideal SQL Query For Handling Error & Transcation in MS SQL
BEGIN TRY BEGIN TRAN --put queries here COMMIT; END TRY BEGIN CATCH IF @@TRANCOUNT>0 BEGIN SELECT @@ERROR,ERRO...
-
All we need to use the Custom paging in grid view without object data source. Here i am going to explain my code which is used in custom pa...
-
One of the common requests in ASP.NET is to submit a form when visitor hits an Enter key. That could be a case if, for example you want to m...
-
using System.Data; using System.Data.SqlClient; using System.Text; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; ...
thnx aillen for your valuable comments
ReplyDelete