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

Generating Random Number and String in C# or Generating the Password Automatically By using asp.net with c#

Apr 6, 2010

Hi friends here I have written the code to generate the auto generated password 

open the visual studio 
select the new website

After that copy and paste the following code in aspx page



<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form2" runat="server">
<div>
<asp:Button ID="btnpassword" runat="server" onclick="btnpassword_Click" Text="Generate Password" />
</div>
<div>
<asp:GridView ID="gvid" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>

After that open the aspx.cs file before copy and paste the code declare the two variables globally


string strPassword = string.Empty;
Random objRandom = new Random(); 

After that write the following code in button click 

protected void btnpassword_Click(object sender, EventArgs e)
{
string password = generatePassword(6);
gvid.DataSource = password;
gvid.DataBind();
}
After that write the following code for generate passowrd

private string generatePassword(int length)
{

int randomNumber;
int i = 1;
string strTemp;
while (i <= length)
{
randomNumber = objRandom.Next(1, 30);
strTemp = (string)Microsoft.VisualBasic.Interaction.Choose(randomNumber, "B", "!", "D", "2", "F", "3", "$", "4", "a", "5", "j", "6", "K", "7", "L", "8", "m", "9", "N", "p", "@", "X", "Y", "#", "G", "H", "%", "R", "w");
strPassword += strTemp;
i++;
}
return strPassword;
}

And you have to give the Microsoft. Visual basic reference to that particular project.

After that press F5 you will see the auto generated password for every button click

I think this code will help you.

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

8 comments :

Anonymous said...

Hello Suresh,

I tried with this code in my project and i got errors on this
1)objrandom.next are you missing assembly refrence
i already add visual basic what you said i did that

venkatesh maddireddy said...

hi suresh i got an error in this code Error 1 The type or namespace name 'Interaction' does not exist in the namespace 'Microsoft.VisualBasic' (are you missing an assembly reference?)

venkatesh maddireddy said...

hi suresh i got an error in this code Error 1 The type or namespace name 'Interaction' does not exist in the namespace 'Microsoft.VisualBasic' (are you missing an assembly reference?)

Anonymous said...

@ All You need to add a reference as suresh already mentioned.right click on ur website>add reference>under .net column select Microsoft. Visual basic.
Thats it.

Anonymous said...

hai sir , this code working well when we open website.. bt if we open project means its displayed the parse error like

"<%@ Application Codebehind="Global.asax.cs" Inherits="Generate_password.Global" Language="C#" %>"


how to clear it?? help me

Anonymous said...

Hi everyone,not to declare globally.
string strPassword = string.Empty;
Random objRandom = new Random();

Modify the generatePassword code like these it works.
private string generatePassword(int length)
{
string strPassword = string.Empty;
Random objRandom = new Random();
int randomNumber;
int i = 1;
string strTemp;
while (i <= length)
{
randomNumber = objRandom.Next(1, 30);
strTemp = (string)Microsoft.VisualBasic.Interaction.Choose(randomNumber, "B", "!", "D", "2", "F", "3", "$", "4", "a", "5", "j", "6", "K", "7", "L", "8", "m", "9", "N", "p", "@", "X", "Y", "#", "G", "H", "%", "R", "w");
strPassword += strTemp;
i++;
}
return strPassword;
}


From Sunny Vishwakarma
sunnyvishwakarma940@gmail.com

expert advice said...

Thanks, you help me solve the password problem.

Manoj Kumar said...

venkatesh maddireddy: For resoving the error you faced, you need to add Microsoft.VisualBasic dll in your references.

Give your Valuable Comments

Other Related Posts

© 2010-2012 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.