Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Generate Random String (Password) in Asp.net using C#, VB.NET

Aug 3, 2015
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.

To generate random string in asp.net open your aspx page and write the following code


<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

Generate Random String (Password) in Asp.net using C#, VB.NET

Download Sample Code Attached


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 RSS subscribe by email Subscribe by Email

2 comments :

REENA SHARMA said...

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.????

Unknown said...

Nice blog

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.