Display Number of Online Users in ASP.Net

We have all seen many websites dislaying the number of online users. In this article, we will learn how to display the number of online users in an ASP.NET Web Application / Website.
 
Procedure
 
Let's begin.
  1. Go to File -> New -> Website then select ASP.NET Empty Web Site
  2. Right-click on the Website Project (in Solution Explorer) then select Add then click on Add New Item
  3. Select Global Application Class (Global.asax) then click on the Add Button
 
You will see various events in the Global.asax file, for example Application_Start, Application_End and so onThe . Application_Start event is called once when the application starts. The Session_Start event is called every time a new session is started. The Session_End event is called when a user session ends. In the Application_Start event, we will initialize our Application state variable.
  1. <%@ Application Language="C#" %>  
  2.   
  3. <script runat="server">  
  4.   
  5.     void Application_Start(object sender, EventArgs e)   
  6.     {  
  7.         // Code that runs on application startup  
  8.         Application["TotalOnlineUsers"] = 0;  
  9.     }  
  10.       
  11.     void Application_End(object sender, EventArgs e)   
  12.     {  
  13.         //  Code that runs on application shutdown  
  14.   
  15.     }  
  16.           
  17.     void Application_Error(object sender, EventArgs e)   
  18.     {   
  19.         // Code that runs when an unhandled error occurs  
  20.   
  21.     }  
  22.   
  23.     void Session_Start(object sender, EventArgs e)   
  24.     {  
  25.         // Code that runs when a new session is started  
  26.         Application.Lock();  
  27.         Application["TotalOnlineUsers"] = (int)Application["TotalOnlineUsers"] + 1;  
  28.         Application.UnLock();  
  29.     }  
  30.   
  31.     void Session_End(object sender, EventArgs e)   
  32.     {  
  33.         // Code that runs when a session ends.   
  34.         // Note: The Session_End event is raised only when the sessionstate mode  
  35.         // is set to InProc in the Web.config file. If session mode is set to StateServer   
  36.         // or SQLServer, the event is not raised.  
  37.         Application.Lock();  
  38.         Application["TotalOnlineUsers"] = (int)Application["TotalOnlineUsers"] - 1;  
  39.         Application.UnLock();  
  40.     }  
  41.          
  42. </script>  
In the Web.config file, add SessionState within the system.web element. By default the session uses Cookies (cookieless="false") and the default timeout period is 20 minutes.
  1. <system.web>  
  2. <sessionState mode="InProc" cookieless="false" timeout="20"></sessionState>  
  3. </system.web>  
Now add a Webform and drop a label control from the Toolbox.
Default.aspx Code: 
  1. <form id="form1" runat="server">  
  2.     <div>  
  3.     <p>No. of Online Users:<asp:Label ID="Label1" runat="server" Text="Label" ForeColor="#CC0000"></asp:Label></p>  
  4.     </div>  
  5. </form>  
Default.aspx.cs Code: 
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.         Label1.Text = Application["TotalOnlineUsers"].ToString();  
  4. }  
Run the application (Ctrl+ F5). For checking it, copy the URL and paste it into a new instance of a different browser. When a visitor visits your website, a new session is started for him and the Application["TotalOnlineUsers"] variable is increased by 1. When the user session expires then the Application["TotalOnlineUsers"] variable is decreased by 1. 
 
Preview:
 
 
We can also use a cookieless session by setting the cookieless property to true. 
  1. <sessionState mode="InProc" cookieless="true" timeout="20"></sessionState>  
On running the application, you will see a Session ID in URL that is different for all users, for all sessions, for the new instance of the ohter browser.

The session ID is embedded in the URL in this format:
 
http://yourserver/(session ID here)/default.aspx 
 
Preview:
 
I hope you like it. Thanks. 

Up Next
    Ebook Download
    View all
    Learn
    View all