Saturday, 11 November 2017

Create random unique id in asp.net C#

UniqueID.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UniqueID.aspx.cs" Inherits="ShowDataOnePage2010.UniqueID" %>
<!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">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="style1">
            <tr>
                <td>
                    <asp:Label ID="lblMsg" runat="server" Font-Bold="True" ForeColor="Maroon"></asp:Label>
                    </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

UniqueID.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ShowDataOnePage2010
{
    public partial class UniqueID : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMsg.Text = "RL-" + GetUniqueKey();
        }
        #region UniqueId

        public string GetUniqueKey()
        {
            string guidResult = string.Empty;
            while (guidResult.Length < 7)
            {
                //Get the GUID
                guidResult += Guid.NewGuid().ToString().GetHashCode().ToString("X");
            }
            //Return the first length Bytes.
            return guidResult.Substring(0, 7);
        }
        #endregion
    }
}

0 comments

Post a Comment