How to generate alphanumeric unique random number in asp.net?

Introduction: In this article i am going to explain how to create or generate unique random alphanumeric number or password or string in asp.net using both C# and VB language.

In previous articles i explained How to Encrypt and decrypt username and password and store in sql server database using asp.net and Jquery to show hide password characters in asp.net textbox on checkbox check uncheck and Create change password form in asp.net and Create crystal reports in visual studio 2010 using asp.net and How to bind ListBox with Sql Server Database in asp.net 

Description: Sometimes it is required to generate/create unique random number while working on applications. E.g. to create random password or to concatenate random number with the name of the file to upload through FileUpload control to make that file unique in the uploaded folder etc.

Implementation: Place a Button Control on design page(.aspx)
<asp:Button ID="btnRandomNumber" runat="server" Text="Generate"
            onclick="btnRandomNumber_Click" />


Asp.Net C# Code to create/generate unique random number in asp.net

    protected void btnRandomNumber_Click(object sender, EventArgs e)
    {
        string num = GenerateRandomNumber(5);
    } 
public static string GenerateRandomString(int Length)
        {
            string _allowedChars = "#@$&*abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
            Random randNum = new Random();
            char[] chars = new char[Length];

            for (int i = 0; i < Length; i++)
            {
                chars[i] = _allowedChars[Convert.ToInt32((_allowedChars.Length - 1) * randNum.NextDouble())];
            }
            return new string(chars);
        }

Asp.Net VB Code to create/generate unique random number in asp.net

Protected Sub btnRandomNumber_Click(sender As Object, e As EventArgs) Handles btnRandomNumber.Click
        Dim num As String = GenerateRandomNumber(5)
    End Sub

    Public Shared Function GenerateRandomString(Length As Integer) As String
        Dim _allowedChars As String = "#@$&*abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789"
        Dim randNum As New Random()
        Dim chars As Char() = New Char(Length - 1) {}

        For i As Integer = 0 To Length - 1
            chars(i) = _allowedChars(Convert.ToInt32((_allowedChars.Length - 1) * randNum.NextDouble()))
        Next
        Return New String(chars)
    End Function

 Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates.        
Previous
Next Post »

4 comments

Click here for comments
Anonymous
admin
September 13, 2013 ×

I am getting error near NextDouble() ?

Reply
avatar
September 13, 2013 ×

What error you are getting? and what parameter you are passing to GenerateRandomNumber method?

Reply
avatar
Unknown
admin
January 25, 2014 ×

Thanks Sir,
Nice Article.........

Reply
avatar
January 26, 2014 ×

your welcome Kadir Ansari..stay connected and keep reading for more useful updates like this.:).

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..