Saturday, 31 August 2013

How to generate Text number in ASP.NET using VB.NET/C#, How to Generate Random Numbers in ASP.NET

Hi friends, in this article I will explain about How to generate Text number in ASP.NET using VB.NET/C#, How to Generate Random Numbers in ASP.NET.
It is very important because in so many sites generate random text or number and asking enter that random number while registering or upload something or download something.
In some sites when we sign up for some site then will generate password and send to mail id or password. I will explain how it will be generated with one small example.
Take one webpage and take one label and button like below.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="randomnum.aspx.vb" Inherits="randomnum" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
    <title>Aspdotnet-roja:Generate Random Text</title>
    <form id="form1" runat="server">
    <h3 style="color:red">Aspdotnet-roja:Generate Random Text</h3>
        <asp:Label style="color :Green" ID="Label1" runat="server" Text="Label"></asp:Label><br />
        <asp:Button ID="generate" runat="server" Text="Generate" onclick="generate_Click" Height="40px" Width="120px" />

VB.NET.Net Code to create/generate unique random text in ASP.NET:
Partial Class randomnum
    Inherits System.Web.UI.Page
    Dim num As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
    Protected Sub generate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles generate.Click
        num = GenerateRNumber(5)
        Label1.Text = "Your Password is: " & num
    End Sub
    Public Shared Function GenerateRNumber(ByVal Length As Integer) As String
        Dim _allowedChars As String = "ABCDEFGHJKLMNOPQRSTUVWXYZ0123456789abcdefghijkmnopqrstuvwxyz"
        Dim rndNum As New Random()
        Dim num As Char() = New Char(Length - 1) {}
        Dim allowedCharCount As Integer = _allowedChars.Length
        For i As Integer = 0 To Length - 1
            num(i) = _allowedChars(Convert.ToInt32((_allowedChars.Length) * rndNum.NextDouble()))
        Return New String(num)
    End Function
End Class

C#.NET.Net Code to create/generate unique random text in ASP.NET:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
partial class randomnum : System.Web.UI.Page
      string num;

      protected void Page_Load(object sender, System.EventArgs e)
      protected void generate_Click(object sender, EventArgs e)
            num = GenerateRNumber(5);
            Label1.Text = "Your Password is: " + num;
      public static string GenerateRNumber(int Length)
            string _allowedChars = "ABCDEFGHJKLMNOPQRSTUVWXYZ0123456789abcdefghijkmnopqrstuvwxyz";
            Random rndNum = new Random();
            char[] num = new char[Length];
            int allowedCharCount = _allowedChars.Length;
            for (int i = 0; i <= Length - 1; i++) {
                  num[i] = _allowedChars[Convert.ToInt32((_allowedChars.Length) * rndNum.NextDouble())];
            return new string(num);
    Public randomnum()
            Load += Page_Load;

Save and run the web page it will as shown in the below figure.

 Happy Reading ........If you like my blog Aspdotnet-Roja then why are you waiting like my blog through facebook page Aspdotnet-Roja

No comments:

Post a Comment

© 2012-2018 All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.