1

I am creating a counter for my webpage. What wan't to achieve is that every time user visits my asp.net application, it stores his data into database. I am using Global.asax and event Application_Start. Here is my code .

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);

        WebpageCounter.SaveVisitor(new WebpageVisitor()
        {
            VisitorIP = HttpContext.Current.Request.UserHostAddress,
            VisitedOn = DateTime.Now
        });
    }

But it never stores anything into database. The function SaveVisitor has been tested and it is functional.

Any suggestions ?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Branislav B.
  • 509
  • 7
  • 21

4 Answers4

4

Application_Start() is only called once for the lifetime of the application domain - not for every request to your site. Also see "ASP.NET Application Life Cycle Overview for IIS 5.0 and 6.0"

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
3

Code for the code behind:

C#

    protected void Page_Load(object sender, EventArgs e)
    {
        this.countMe();

        DataSet tmpDs = new DataSet();
        tmpDs.ReadXml(Server.MapPath("~/counter.xml"));

        lblCounter.Text = tmpDs.Tables[0].Rows[0]["hits"].ToString();
    }

    private void countMe()
    {
        DataSet tmpDs = new DataSet();
        tmpDs.ReadXml(Server.MapPath("~/counter.xml"));

        int hits = Int32.Parse(tmpDs.Tables[0].Rows[0]["hits"].ToString());

        hits += 1;

        tmpDs.Tables[0].Rows[0]["hits"] = hits.ToString();

        tmpDs.WriteXml(Server.MapPath("~/counter.xml"));

    }

VB.NET

    Protected Sub Page_Load(sender As Object, e As EventArgs)
        Me.countMe()

        Dim tmpDs As New DataSet()
        tmpDs.ReadXml(Server.MapPath("~/counter.xml"))

        lblCounter.Text = tmpDs.Tables(0).Rows(0)("hits").ToString()
    End Sub

    Private Sub countMe()
        Dim tmpDs As New DataSet()
        tmpDs.ReadXml(Server.MapPath("~/counter.xml"))

        Dim hits As Integer = Int32.Parse(tmpDs.Tables(0).Rows(0)("hits").ToString())

        hits += 1

        tmpDs.Tables(0).Rows(0)("hits") = hits.ToString()

        tmpDs.WriteXml(Server.MapPath("~/counter.xml"))
    End Sub

XML file will look like this:

<?xml version="1.0" encoding="utf-8" ?>
<counter>
  <count>
     <hits>0</hits>
  </count>

Dinesh Rabara
  • 1,119
  • 12
  • 13
2

Application_Start is runs only when process created - not every visit.

You can use Application_BeginRequest instead.

x2.
  • 9,554
  • 6
  • 41
  • 62
1

This information can be logged by IIS, and then queried/transformed using the excellent logparser. You could also put Google Analytics on your site - its free version is sufficient for all but the busiest sites. If you still feel the need to do this yourself, then Application_BeginRequest is a better place to record this.

EDIT: You could implement it as a module, like the MSDN Custom Module Walkthrough and then your app could be a little more modular

kͩeͣmͮpͥ ͩ
  • 7,783
  • 26
  • 40