Create recent unique visitor number in ASP.NET using C#

To get the recent user number in ASP.Net using c# follow the below steps.

  1. Add a Global.asax page, Global.asax is a page containg session level and application level events, this is also called an application file.

    Write the below code in to your Global.asax page

    <%@ Application Language="C#" %> 
    <script runat="server">
        public static int u_count = 0;
       
    void Application_Start(object sender, EventArgs e)
        {
           
    // Code that runs on application startup
            Application["user_Count"] = u_count; 
        }   
       
    void Application_End(object sender, EventArgs e)
        {
           
    //  Code that runs on application shutdown
        }       
       
    void Application_Error(object sender, EventArgs e)
        {
           
    // Code that runs when an unhandled error occurs 
        } 
       
    void Session_Start(object sender, EventArgs e)
        {
           
    // Code that runs when a new session is started
            u_count = Convert.ToInt32(Application["user_Count"]); Application["user_Count"] = u_count + 1; 
        } 
       
    void Session_End(object sender, EventArgs e)
        {
           
    // Code that runs when a session ends.
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer
            // or SQLServer, the event is not raised.         
        }
    </script>

    In above code “u_count “ ll start from zero but as it goes on the “user_count” and “u_count” ll increase one and ll hold the value “1”.
     
  2. Write the below code in to code behind page or .cs file
     

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Globalization; 

    public partial class Default2 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            int a;

            a = Convert.ToInt32((Application["user_Count"]));

            Label1.Text = Convert.ToString(a);

            if (a < 10)

                Label1.Text = "000" + Label1.Text;

            else if (a < 100)

                Label1.Text = "00" + Label1.Text;

            else if (a < 1000)

                Label1.Text = "0" + Label1.Text;      
        }
    }

Output

Clipboard02.jpg