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

9 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..


HR FireJob 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

Vishvjeet Patel said...

Awesome

Anonymous said...

Very Nice ,Easy to unserstand.... Thanks

siva krishna 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.

Joseph Devy Chrisma Widyanata said...

thanks for your article..

Anonymous said...

thanx

Ishan Parmar 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

Give your Valuable Comments

Other Related Posts

© 2010-2012 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.