joomla visitors
Mithila Software Blog | Creating a Random Alphanumeric String in asp.net

Creating a Random Alphanumeric String in asp.net

November 27, 2012 Posted by Mithila Software

There are sometimes when we need to create an alphanumeric string in asp.net.The small routine below can help you achieve this.

Copy and paste this asp random generator method in your application

  public string GetRandomString(int length)
    {
        Random rnd = new Random();
        string charPool
        = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        System.Text.StringBuilder rs = new System.Text.StringBuilder();
        while (length-- > 0)
            rs.Append(charPool[(int)(rnd.NextDouble() * charPool.Length)]);
        return rs.ToString();
    }

Calling this method from your application.

Simple ! Place it anywhere in your codebehind events...

Response.Write(GetRandomString(20));

SAMPLE OUTPUT :: IJMG8VQNZ4U7EMBNA0IL

Tags:

Asp.Net

blog comments powered by Disqus
Address ::
Mithila Software Solutions,
Green House , 3 GM Road ,
Near Radio Station , Darbhanga
PIN:: 846004
Phone :: # 099-397-49350