Introduction: 
Here I will explain how to generate random string or password in asp.net using c#, vb.net with example or generate random number and string with example in asp.net using c#, vb.net or generate random alphanumeric string in asp.net using c#, vb.net with example.
Description: 
   
In previous articles I explained insert, update, delete in gridview with single stored procedure, jQuery gridview crud operations without postback in asp.net, gridview examples in asp.net, display images from database using handler in asp.net, generate random password in asp.net using c# and many articles relating to gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to generate random string or password or alphanumeric string in asp.net using c#, vb.net with example.
In previous articles I explained insert, update, delete in gridview with single stored procedure, jQuery gridview crud operations without postback in asp.net, gridview examples in asp.net, display images from database using handler in asp.net, generate random password in asp.net using c# and many articles relating to gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to generate random string or password or alphanumeric string in asp.net using c#, vb.net with example.
| 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title>Generate Random Number
  or String or Password in Asp.net</title> 
</head> 
<body> 
<form id="form1"
  runat="server"> 
<div> 
Enter Number of Characters: <asp:TextBox ID="txtCharacters" runat="server"/> 
<asp:Button ID="btnGenerate"
  Text="Generate"
  runat="server" 
onclick="btnGenerate_Click" /><br
  /> 
<asp:Label ID="lblResult"
  runat="server"
  ForeColor="Red"
  /> 
</div> 
</form> 
</body> 
</html> | 
Now add following namespaces in code behind
C#
Code
| 
using System; 
using System.Web; | 
After completion of adding namespaces you need to write the
code like as shown below
| 
protected void Page_Load(object
  sender, EventArgs e) 
{ 
} 
protected void btnGenerate_Click(object
  sender, EventArgs e) 
{ 
// declare array string to generate random string with
  combination of small,capital letters and numbers 
char[] charArr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); 
string strrandom = string.Empty; 
Random objran = new Random(); 
int noofcharacters = Convert.ToInt32(txtCharacters.Text); 
for (int i = 0; i < noofcharacters; i++) 
{ 
//It will not allow Repetation of Characters 
int pos = objran.Next(1,
  charArr.Length); 
if
  (!strrandom.Contains(charArr.GetValue(pos).ToString())) 
strrandom += charArr.GetValue(pos); 
else 
i--; 
} 
lblResult.Text = strrandom; 
} | 
VB.NET
Code
| 
Imports System.Web 
Partial Class VBCode 
Inherits System.Web.UI.Page 
Protected Sub Page_Load(sender As
  Object, e As EventArgs) 
End Sub 
Protected Sub btnGenerate_Click(ByVal
  sender As Object,
  ByVal e As EventArgs) 
' declare array string to generate random string with
  combination of small,capital letters and numbers 
Dim charArr As Char() = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray() 
Dim strrandom As String = String.Empty 
Dim objran As New Random() 
Dim noofcharacters As Integer = Convert.ToInt32(txtCharacters.Text) 
For i As Integer = 0 To noofcharacters - 1 
'It will not allow Repetation of Characters 
Dim pos As Integer = objran.[Next](1,
  charArr.Length) 
If Not
  strrandom.Contains(charArr.GetValue(pos).ToString()) Then 
strrandom += charArr.GetValue(pos) 
Else 
i -= 1 
End If 
Next 
lblResult.Text = strrandom 
End Sub 
End Class | 
Demo
| If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. | |||
|  Subscribe by RSS  Subscribe by Email | |||


 
2 comments :
Its an awsm thng but i want that enter only your name or username and then we create a password... can u plz help me.????
Nice blog
Note: Only a member of this blog may post a comment.