How to get the online Users Count in ASP.NET

No.of Views1845
Bookmarked15 times
Downloads 
Votes1
By  RRaveen   On  18 Aug 2010 08:08:54
Tag : ASP.NET , Miscellaneous
Today I wish to get the how many users in online codegain.com, base on that idea, I come cross to a simple and easy solution to implement this.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Today I wish to get the how many users in online codegain.com, base on that idea, I come cross to a simple and easy solution to implement this.

Implementation

Basically we have to count no of users or visitors online in the whole application, based on that we have to use the Application object in ASP.NET to keep the count or read the count as well.
But next question when we have to increase count by one visitor enter into www.codegain.com? It’s simple, because when every visitor enters into site, IIS will create a session object for users. So we can increase count by one when session is start.Same we can reduce the count by one when session is end.

Now we have to compose the code to implement what we are discusses above.

Step 1

Create a web project using visual studio 2010 or 2008.

Step 2

Then right click on your web project and then click add new Item -> then select the Global Application Class from template list window.

It will look like following,

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

<script runat="server">void Application_Start(object sender, EventArgs e) 
    {// Code that runs on application startup}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}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>

 Step 3

Now we have to add code for first portion of the implementation. Its mean when application start, we will set no of online users is 0.

Code

void Application_Start(object sender, EventArgs e)
    {// Code that runs on application startupApplication["CurrentUsers"] = 0;
    }

Then we have to increase count by one when users come into site.

Code

void Session_Start(object sender, EventArgs e)
    {// Code that runs when a new session is startedApplication.Lock();
        Application["CurrentUsers"] = (int)Application["CurrentUsers"] + 1;
        Application.UnLock();
    }

In above code there is small tricky pint when we are use the Application object to update point. The Web application is multi thread application so, when we are trying to common object, we have to lock first that object then we have to update and then finally we have to release as well.

lock the application object,

Application.Lock();

Release application object

Application.UnLock();

Third part of this implementation, just we have to decrease count by one when session is end.

Code

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.// Code that runs when a new session is startedApplication.Lock();if ((int)Application["CurrentUsers"] > 0)
        {
            Application["CurrentUsers"] = (int)Application["CurrentUsers"] - 1;
        }
        Application.UnLock();
    }

Finally, you have to display this count. If you have master page, then you can add simple in top left corner or right corner in within the div or label object as like follows,

Online Users: <%=Application ["CurrentUsers"].ToString()%>

 That’s all. If want to refresh this count based on the interval, then you can use the timer to update label. Thank you for reading.

Sample Project Source

Download source files -3 kb

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
By:DavidDate Of Posted:4/20/2011 11:22:51 PM
Same what author have written
Hi Mass, This author also using Global.asax. so i think your link does not help much more.and this is great and easy article to count online users. i give 4 out of 5.
By:Massimiliano BianchiDate Of Posted:4/20/2011 2:41:52 AM
Online users using global.asax
You can use global.asax to know how many people are online on your site. Check this article: http://www.massimilianobianchi.info/max/21art_Show-online-users-in-ASP-NET-sites.aspx
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top