.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

A simple Hit counter

Posted By:Gowthammanju       Posted Date: August 29, 2011    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

This article explains about counting no of hits in our website in a easy approach
 

Try these steps as

step 1: Open A project and add a text file in it
Step 2:Then create the page in which you need to place the hit counter
step 3:In page load event call the coding as
using System.IO;
protected void Page_Load(object sender, EventArgs e)
{
    Application.Lock();
    Response.Write(gethitcounts()); //you can change here for label control
    Application.UnLock();
}
public string gethitcounts()
{
    string lastcount = "";
    try
    {
        StreamReader SR = File.OpenText(Server.MapPath("hitcounter.txt"));
        string getcount = null;
        while ((getcount = SR.ReadLine()) != null)
        {
            lastcount = lastcount + getcount;
        }
        SR.Close();
        long newcount = Convert.ToInt64(lastcount);
        newcount++;
        TextWriter TxtWtr = new StreamWriter(Server.MapPath("hitcounter.txt"));
        TxtWtr.WriteLine(Convert.ToString(newcount));
        TxtWtr.Close();
        SR = File.OpenText(Server.MapPath("hitcounter.txt"));
        getcount = null;
        lastcount = "";
        while ((getcount = SR.ReadLine()) != null)
        {
            lastcount = lastcount + getcount;
        }
        SR.Close();
    }
    catch (Exception ex)
    {
        TextWriter TxtWtr = new StreamWriter(Server.MapPath("hitcounter.txt"));
        TxtWtr.WriteLine(Convert.ToString("1"));
        TxtWtr.Close();
        lastcount = "1";
    }
    return lastcount;
}

Thats it
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend