Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - How to Count Number of Online Users in Asp.Net Using C#, VB.NET

Nov 26, 2013
Introduction

Here I will explain how to count number of online users of website in asp.net using C# and VB.NET or how to get number of online visitors of website in asp.net using C# and VB.NET.

Description

In previous articles I explained get count of visitors website in asp.net, jQuery cascading dropdown list example in asp.net, jQuery show progressbar in button click in asp.net, Asp.net Interview questions, Joins in SQL Server and many articles relating to Gridview, SQL, jQuery,asp.net, C#,VB.NET. Now I will explain how to count number of online visitors of website in asp.net using C# and VB.NET.

To get number of online visitors for website we need to use methods in Global.asax file for that first add Global.asax file in your application

Open Visual Studio -à Create New Website -à Right click on Solution Explorer -à Select Add New Item -à Select Global Application Class file and click OK

Now open Global.asax file and write the code like as shown below

Global.aspx


<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["OnlineVisitors"] = 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["OnlineVisitors"] = (int)Application["OnlineVisitors"] + 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["OnlineVisitors"] = (int)Application["OnlineVisitors"] - 1;
Application.UnLock();
}
</script>
In above code Application_Start event will raise only once when application starts and Session_Start event will raise for every postback operation and Session_End event is used to reduce count of users when session ends. If you want to know more about it check this What is Global.asax file and uses of it

Here Session_End event will work only when we set sessionstate mode to InProc in web.config file for that reason we need write code in web.config file as shown below


<system.web>
<sessionState mode="InProc" cookieless="false" timeout="20"/>
</system.web>
Now open your aspx page and write the following code


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<b>No of Users Online:</b>
</td>
<td style="color:Red">
<%=Application["OnlineVisitors"].ToString()%>
</td>
</tr>
</table>
</form>
</body>
</html>
Demo


Download Sample Code Attached






Note: We need to remember that session will not expire when we close browser it will expires after 20 minutes of default time or forcefully clear session by using Session.Abandon()

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

14 comments :

jaguar said...

Nice Upload Suresh

Priya said...

very useful informations....superbly maintaining...Thanks..

Anonymous said...

Great article. Thanks.
Please make it "global.asax" in the heading as that may misguide beginners.
Regards

Unknown said...

I have tried this before for ending the session i even tried session.abandon() and session.remove(" "). but it never worked.

The main limitation in using this is that, what happens the user just closes the browser without ending the session ? or the browser crashes ? Then the online visitors will be the same until the session automatically times out right ?

Anyway good try though..!!

Unknown said...

can we do in the MVC also? i tried with this <%=Application["OnlineVisitors"].ToString()%> in view its not working. i m waiting for your favorable response .

Anonymous said...

nice article...very useful

Unknown said...

nice and so simple.... Thanks for information.

Anonymous said...

thank you for your valuable information. and then i am expecting more information from your side... thank you thank you so much

Unknown said...

I Want to provide access a particular page only four people can access at the same time

imvivek said...

Thanks a lot buddy...

Karthik said...

Thanks Suresh...this code Working fine...

Anonymous said...

if you are using MVC try to fetch response in model or view-model then bind that with respective layout or view
like this Online Users:
@Model.countUsersLoggedIn

Unknown said...

If User logout or session Abondon the count is not decreasing.

valmik Jadhav said...

nice article...very useful
But can you please give some idea to handle browser close scenario without log off.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.