Saturday, March 15, 2014

How to Generate Random Number in ASP.NET and use it into various application.

Posted by Unknown on 1:07 AM with No comments
In this post I am going to Generate a Random Number using RANDOM class in the .NET Library.

Here we go.

For Generating a Random Number Follow the step by step Procedure.

Step 1: First of all create a new Project in Microsoft Visual Studio. In this I am using version 2010.
             Microsoft Visual Studio>New>Project.
             Name the Project as you want.

Step 2: Now add a new Webform into the solution and name it GenerateRandomNumber.aspx

Step 3: Use the following code for designing the webpage.



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GenerateRandomNumber.aspx.cs"
    Inherits="EmailForm.GenerateRandomNumber" %>
 
<!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 runat="server">
    <title></title>
    <style type="text/css">
        .button
        {
            displayblock;
            floatright;
            bordernone;
            colorwhite;
            width75px;
            height40px;
            background-color#323233;
        }
        .searchTextBox
        {
            backgroundwhite;
            border1px solid #DDD;
            box-shadow0 0 5px #DDD inset;
            color#666;
            padding5px 10px;
            width200px;
            outlinenone;
            margin-left15px;
            margin-top5px;
        }
        .auto-style3
        {
            color#666;
            text-aligncenter;
        }
        .table-style
        {
            width38%;
            height163px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 class="auto-style3">
            <strong><em>Generating Ranodm Number</em></strong></h2>
         </div>
    <div>
        <table align="center" class="table-style">
            <tr>
                <td>
                    Generated Random Number:
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" class="searchTextBox"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td align="center">
                    <asp:Button ID="Button1" runat="server"  OnClick="Button1_Click" 
                        Text="Generate" class="button"  />
                </td>
            </tr>
        </table>
    </div>
    <div>
     <asp:TextBox ID="TextBox2" runat="server" class="searchTextBox" ></asp:TextBox>
        <asp:Button ID="Button2" runat="server" Text="Speak" onclick="Button2_Click" />
        <asp:Button ID="Button3" runat="server" Text="Pause" onclick="Button3_Click" />
        <asp:Button ID="Button4" runat="server" Text="Resume" onclick="Button4_Click" />
    </div>
    </form>
 
    <div>
   
    </div>
</body>
</html>


Step 4:  Now after completing this add following code into the the codebehind file GenerateRandomNumber.aspx.cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

 
namespace EmailForm
{
    public partial class GenerateRandomNumber : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            GeneratePassword();
 
        }
 
        public string GeneratePassword()
        {
            // Take this one as empty, It will hold the Randomly Generated Number.
 
            string RandomNumber = "";
 
            // Define what characters you want to allow into Randomly Generated Number.
 
            string allowedChars = "";
            allowedChars = "0,1,2,3,4,5,6,7,8,9";
            allowedChars += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,";
            allowedChars += "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,";
            allowedChars += "~,!,@,#,$,%,^,&,*,+,?";
 
            // Define the character ',' to eliminate comma symbol from allowed characters.
 
            char[] seperator = { ',' };
            string[] array = allowedChars.Split(seperator);
 
            string str = "";
            string temp = "";
 
            // By using the Ranodm Class from .NET Library.
 
            Random rand = new Random();
 
            // Create for loop to Generate Random Number.
            // In the for loop the value i < 15 shows the lenght of Randomly Generated Number.  
 
            for (int i = 0; i < 15; i++)
            {
                temp = array[rand.Next(0, array.Length)];
 
                str += temp;
                RandomNumber = str;
 
                TextBox1.Text = str;         // This will show Randomly Generated Number iinto TextBox.
 
            }
 
            return RandomNumber;
        }
 
        
 
 
 
 
    }
}
Step 5: This is complete now. Run the project and click on the Generate Button.
              It will Generate the Random Number. And use this in your application wherever it requires.

              Below are the Screen shot of this Code:


Before Clicking on the Generate Button.

After Clicking on the Generate Button.




Step 6: Like and Share if this post helps you.


Thank you. I hope this will help you.