In this article I will explain with an example, how to auto generate Unique ID (Random ID) with Prefix in ASP.Net using C# and VB.Net.
This article will illustrate how to generate Unique ID (Random ID) of different sizes with Numeric and AlphaNumeric types with a Fixed Prefix in ASP.Net using C# and VB.Net.
HTML Markup
The following HTML Markup consists of an ASP.Net DropDownList, a RadioButtonList, a Button and a Label.
The DropDownList contains the Length options and the RadioButtonList contains the Alphanumeric and Numeric options for ID.
<table border="0" cellpadding="0" cellspacing="0">
            <asp:DropDownList ID="ddlLength" runat="server">
                <asp:ListItem Text="5" Value="5" />
                <asp:ListItem Text="8" Value="8" />
                <asp:ListItem Text="10" Value="10" />
            <asp:RadioButtonList ID="rbType" runat="server" RepeatDirection="Horizontal">
                <asp:ListItem Text="Alphanumeric" Value="1" Selected="True" />
                <asp:ListItem Text="Numeric" Value="2" />
        <td><asp:Button ID="btnGenerate" Text="Generate ID" runat="server" OnClick="GenerateID" /></td>
        <td colspan="3"><asp:Label ID="lblID" runat="server" /></td>
Generating Unique ID (Random ID) with Prefix using C# and VB.Net
When the Generate Button is clicked, the length and the type values of the ID are fetched from the DropDownList and RadioButtonList.
There are three strings, first containing Upper Case Alphabets, second containing Lower Case Alphabets and the third one containing the 10 digits.
Then a loop is executed and inside the loop a random number is used to fetch the character from the combination of Alphabets and Numeric strings or only the Numeric string based on the type selected in the RadioButtonList.
Inside the For Loop, a While loop is used to avoid repetition of the characters.
Finally, a string Prefix value is concatenated with the generated Unique ID (Random ID) and the final output is displayed in the Label control.
protected void GenerateID(object sender, EventArgs e)
    string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string small_alphabets = "abcdefghijklmnopqrstuvwxyz";
    string numbers = "1234567890";
    string characters = numbers;
    if (rbType.SelectedItem.Value == "1")
        characters += alphabets + small_alphabets + numbers;
    int length = int.Parse(ddlLength.SelectedItem.Value);
    string id = string.Empty;
    for (int i = 0; i < length; i++)
        string character = string.Empty;
            int index = new Random().Next(0, characters.Length);
            character = characters.ToCharArray()[index].ToString();
        } while (id.IndexOf(character) != -1);
        id += character;
    lblID.Text = "MK-" + id;
Protected Sub GenerateID(sender As Object, e As EventArgs)
    Dim alphabets As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Dim small_alphabets As String = "abcdefghijklmnopqrstuvwxyz"
    Dim numbers As String = "1234567890"
    Dim characters As String = numbers
    If rbType.SelectedItem.Value = "1" Then
        characters += Convert.ToString(alphabets & small_alphabets) & numbers
    End If
    Dim length As Integer = Integer.Parse(ddlLength.SelectedItem.Value)
    Dim id As String = String.Empty
    For i As Integer = 0 To length - 1
        Dim character As String = String.Empty
            Dim index As Integer = New Random().Next(0, characters.Length)
            character = characters.ToCharArray()(index).ToString()
        Loop While id.IndexOf(character) <> -1
        id += character
    lblID.Text = "MK-" & id
End Sub
Auto generate Unique ID (Random ID) with Prefix using C# and VB.Net