Hello ,
I would like to count the no of hits to my website.
How do i do it?
whether I need to code it in Global.asax file in application on start ??????
Reply

Recommended Answers

All 6 Replies

Hello ,
I would like to count the no of hits to my website.
How do i do it?
whether I need to code it in Global.asax file in application on start ??????
Reply

Hi you can place the code in Global.asax file and u need to call that variable in the aspx page. Example:

void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
        Application["Visitors"] = 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["Visitors"] = Convert.ToInt32(Application["Visitors"]) +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["Visitors"] = Convert.ToInt32(Application["Visitors"])-1;

    }

In the aspx file u need to call that variable as follow:

protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text =  Application["Visitors"].ToString();
       
    }

*** do reply whether it solves ur problem or not.....

use application variable....works... 100%

commented: bad advice -1

use application variable....works... 100%

Application variables are not persisted and you will lose the count data.

You can also take a look at a free application called smarter stats for weblog analysis. There is a lot more information to be had from IIS log files then the number of times your page was accessed. You have to take in to account spiders that crawl your site.

SmartStats: http://www.smartertools.com/

Thank you all ur support.
It works fine.

gr8 work...

Hi you can place the code in Global.asax file and u need to call that variable in the aspx page. Example:

void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
        Application["Visitors"] = 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["Visitors"] = Convert.ToInt32(Application["Visitors"]) +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["Visitors"] = Convert.ToInt32(Application["Visitors"])-1;

    }

In the aspx file u need to call that variable as follow:

protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text =  Application["Visitors"].ToString();
       
    }

*** do reply whether it solves ur problem or not.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.