C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET WebForms »

Track the visitors in the site


Posted Date: 06 Aug 2007    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: Vijay Kumar NaiduMember Level: Gold    
Rating: 1 out of 5Points: 10



This code snippet helps you track the number of users visited your Website. This code sample stores a counter in an Application variable and every time a new visitor comes to the page, the counter is incremented.

In Global.asax file, write the following code as specified below:


int userCount;
void Application_Start(object sender, EventArgs e)
{
userCount = 0;
Application["mycount"] = count;
}

void Session_Start(object sender, EventArgs e)
{
count = Convert.ToInt32(Application["mycount"]);
Application["mycount"] = count + 1;
}



Now, in the Page_Load event, use the Application object to display the value whereever required.


protected void Page_Load(object sender, EventArgs e)
{
if ( !IsPostBack )
{
Response.Write("No. of Users viewed the Website: " + Convert.ToString(Application["mycount"]));
}
}


Please let me know if you have any questions.

Enjoy!!!

-Vijay Kumar Naidu.



Responses

Author: roopesh    14 Sep 2007Member Level: Silver   Points : 0
the code for tracking vistors is good.& simple too..


Author: Ravindra Singh    01 Feb 2008Member Level: Gold   Points : 0
code is good


Author: R O B I N    06 Jun 2008Member Level: Gold   Points : 0
Good code for the developer,easy and simple


Author: komaladevi    06 Jun 2008Member Level: Gold   Points : 0
same code is not working for me


Author: sushil kumar    06 Jun 2008Member Level: Bronze   Points : 1
yar code nahi run kar raha hai pl give me ur personal id ..
i hv need of this code urgently or mail me at lohsushil@gmail.com
sushil


Author: jeeth    08 Apr 2009Member Level: Gold   Points : 2
Due to several reasons application ends in .net(Application_End event also fired), like any change in the root directories or site idle for long time etc. In that case application starts when next request comes and the count variable that you have used will be re-set to zero again. So the code will not work in that case.
You need to save the access count to file system or database during Application_End and retrieve it when while Application starts



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to write "Text to Speech Applications" in C#?
Previous Resource: Getting other than AppSetting values from Web.Config
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use