Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Thursday, 13 November 2014

Display Number of Online Users in ASP.NET

All of us have seen many Websites displaying the number of online users. In this Article, We will learn how to Display Number of Online Users in ASP.NET Web Application / WebSite.

Let's Begin:
1) Go to File -> New -> Website -> Select ASP.NET Empty Web Site
2) Right Click on Website Project (in Solution Explorer) -> Add -> Click on Add New Item
3) Select Global Application Class (Global.asax) -> Click on Add Button.

Global.asax

You will see various events of Global.asax file like Application_Start, Application_End etc. Application_Start event called once when application starts. Session_Start event called every time when a new session is start. Session_End event called when a user session ends. In Application_Start Event, We will initialize our Application state variable.

Global.asax Code:

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        Application["TotalOnlineUsers"] = 0;
    }
   
    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
        Application.Lock();
        Application["TotalOnlineUsers"] = (int)Application["TotalOnlineUsers"] + 1;
        Application.UnLock();
    }

    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.
        Application.Lock();
        Application["TotalOnlineUsers"] = (int)Application["TotalOnlineUsers"] - 1;
        Application.UnLock();
    }
      
</script>
In Web.config file, Add SessionState within system.web element. By Default Session uses Cookies (cookieless="false") and Default timeout period is of 20 minutes.

<system.web>
<sessionState mode="InProc" cookieless="false" timeout="20"></sessionState>
</system.web>
4) Add a Webform and drop a label control from the Toolbox.

Default.aspx Code:

<form id="form1" runat="server">
    <div>
    <p>No. of Online Users:<asp:Label ID="Label1" runat="server" Text="Label" ForeColor="#CC0000"></asp:Label></p>
    </div>
</form>
Default.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
        Label1.Text = Application["TotalOnlineUsers"].ToString();
}
Run the Application (ctrl+ F5). For checking, copy the url and paste it in the new instance of different browser. When a visitor visit your website, a new session is started for him and Application["TotalOnlineUsers"] variable increased by 1. When user session expires than Application["TotalOnlineUsers"] variable decreased by 1.
Preview:
Online user in Asp.net
We can also use Cookieless session by setting cookieless property to true.

<sessionState mode="InProc" cookieless="true" timeout="20"></sessionState>
On running the application, you will see Session ID in URL which is different for all users, sessions, for the new instance of different browser. The session ID is embedded in the URL in the format shown below.
http://yourserver/(session ID here)/default.aspx

Preview:
Online user in Asp.Net
Hope you like it. Thanks. 
[Download Source Code via Google Drive]

Video:

2 comments:

  1. i am posa kumar i tried calc pro plugin in word press it work's only few days. so plz help me ........

    ReplyDelete
  2. i just want a simple fd calc dasy, months, and years mentioned in calc i hope u will help me

    ReplyDelete

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook