Hi gmrneeraj,
Check this example. Now please take its reference and correct your code.
HTML
<asp:TextBox runat="server" ID="TextBox1" /><br />
<asp:TextBox runat="server" ID="TextBox2" /><br />
<asp:TextBox runat="server" ID="TextBox3" /><br />
<asp:TextBox runat="server" ID="TextBox4" /><br />
<asp:TextBox runat="server" ID="TextBox5" /><br />
<asp:TextBox runat="server" ID="TextBox6" /><br />
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
List<string> numbers = new List<string>();
for (int i = 0; i < 6; i++)
{
numbers.Add(GenerateID());
}
TextBox1.Text = numbers[0];
TextBox2.Text = numbers[1];
TextBox3.Text = numbers[2];
TextBox4.Text = numbers[3];
TextBox5.Text = numbers[4];
TextBox6.Text = numbers[5];
}
protected string GenerateID()
{
string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string small_alphabets = "abcdefghijklmnopqrstuvwxyz";
string numbers = "1234567890";
string characters = alphabets + small_alphabets + numbers;
int length = 6;
string id = string.Empty;
for (int i = 0; i < length; i++)
{
string character = string.Empty;
do
{
int index = new Random().Next(0, characters.Length);
character = characters.ToCharArray()[index].ToString();
} while (id.IndexOf(character) != -1);
id += character;
}
return id;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim numbers As List(Of String) = New List(Of String)()
For i As Integer = 0 To 5 Step 1
numbers.Add(GenerateID())
Next
TextBox1.Text = numbers(0)
TextBox2.Text = numbers(1)
TextBox3.Text = numbers(2)
TextBox4.Text = numbers(3)
TextBox5.Text = numbers(4)
TextBox6.Text = numbers(5)
End Sub
Protected Function GenerateID() As String
Dim alphabets As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim small_alphabets As String = "abcdefghijklmnopqrstuvwxyz"
Dim numbers As String = "1234567890"
Dim characters As String = alphabets & small_alphabets & numbers
Dim length As Integer = 6
Dim id As String = String.Empty
For i As Integer = 0 To length - 1
Dim character As String = String.Empty
Do
Dim index As Integer = New Random().Next(0, characters.Length)
character = characters.ToCharArray()(index).ToString()
Loop While id.IndexOf(character) <> -1
id += character
Next
Return id
End Function