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# - Count Number of Visitors in Website using Asp.net in C#, VB.NET

Nov 26, 2013
Introduction

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

Description

In previous articles I explained Asp.net Interview questions, Export Gridview data to PDF, Send values from one page to another page using QueryString, Joins in SQL Server, Highlight Gridview records based on search and many articles relating to Gridview, SQL, jQuery,asp.net, C#,VB.NET. Now I will explain how to count number of visitors to website in asp.net using C# and VB.NET.
To get number of visits 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


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

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
Application["NoOfVisitors"] = (int)Application["NoOfVisitors"] + 1;
Application.UnLock();
}
In above code Application_Start event will raise only once when application starts and Session_Start event will raise for every postback operation if you want to know more about it check this What is Global.asax file and uses of it

Now open your aspx page and write the following code


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<b>No of Visits:</b>
</td>
<td>
<asp:Label ID="lblCount" runat="server" ForeColor="Red" />
</td>
</tr>
</table>
</form>
</body>
</html>
Now in code behind add the following namespaces

C# Code


using System;
After that add below code in code behind


protected void Page_Load(object sender, EventArgs e)
{
lblCount.Text = Application["NoOfVisitors"].ToString();
}
VB.NET Code


Partial Class VbCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
lblCount.Text = Application("NoOfVisitors").ToString()
End Sub
End Class
Demo


Download Sample Code Attached






Note: Here we are using Application level variable in Global.asax file because of that if application restart automatically the values will be reset  

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

19 comments :

Rajesh said...

Hi suresh. I thought it would be an complex way to get number of visitor count visiting our application but you made it very easy and understandable.. Thanks alot.. I would be expecting more from you..


Unknown said...

Object Reference not set of a Instance of Objects
show error
tell me whats problem
kaleem

Anonymous said...

M.santhakumari.
nice article anna....its very easy to understand

Anonymous said...

Very Nice ,Easy to unserstand.... Thanks

Anonymous said...

Hi,suresh,
I got some error in fusion charts. I Bind one bar chart and one Pie chart in my web page.it'working correctly but some perameter exceptions are coming like 'ParameterException: #06091847 chartobject-1 Warning >> A FusionChart oject with the specified id "ChartTargetID" already exists. Renaming it to chartobject-1'.
GALLERY_RENDERER = "javascript";
if (GALLERY_RENDERER && GALLERY_RENDERER.search(/javascript|flash/i) == 0) FusionCharts.setCurrentRenderer(GALLERY_RENDERER);
var Barchart = new FusionCharts("../js/Charts/Data/MSColumn3D.swf", "ChartTargetID", "860", "350", "0", "0");
var chart1 = new FusionCharts("../js/Charts/Data/Pie3D.swf", "ChartTarget", "860", "350", "0", "0");

please give some solution.

JD said...

thanks for your article..

Anonymous said...

thanx

Unknown said...

No of visits are getting reset to initial value 0 after some hours. can you help me by telling me how to resolve this issue

Anonymous said...

hi mr. suresh, really nice code, thanks for that. i used this in my one of website and i started it from a another value e.g. 1044. now is it like that it will not update the value and the counter will always start from that fixed value, please reply asap

Anonymous said...

after refresh count value not increasing the count always being 1.why and how to solve it

Unknown said...

How to get visitor number on particular url of website

Watch TV Channels Without Setup Box Free said...

fter refresh count value not increasing the count always being 1.why and how to solve it

Anonymous said...

Hi suresh,
as per you aritacle you said that "Session_Start event will raise for every postback operation". but i don't think so that every post back raise session start event. kindly correct my if i am wrong

Anonymous said...

Error: Object reference not set to an instance of an object.

lblCount.Text = Application["NoOfVisitors"].ToString();

Unknown said...

what if the is restarted

Unknown said...

what if i want to get the count at the end of the day..is there any specific interval after which the count is reset to 0.Pl mention in detail of everything.This all sounds half knowledge which is dangerous.

dharma said...

Remove Application["NoOfVisitors"] = 0; from Application_Start
And In Session_Start put below code
if(Application["NoOfVisitors"] == null)
{Application["NoOfVisitors"]=0;}
Application.Lock();
Application["NoOfVisitors"] = (int)Application["NoOfVisitors"] + 1;
Application.UnLock();

Unknown said...

Yes its very smooth an clean code but when i stop my site its again counts from 0,
is there any solution ?

Anonymous said...

Thanks

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.