Click here to Skip to main content
14,735,933 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I have develop a visitor counter using 2.0, but i think it is unnecessary incremented day by day. for example if the visitor count is 20 it shows around 60-70. please solve the problem, the code is written in global.asax file, given below:
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
Dim result As Integer
Dim conn As OleDbConnection
Dim comm As OleDbCommand
Dim strConnection As String
strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("database/counter.mdb")
conn = New OleDbConnection(strConnection)
comm = New OleDbCommand("SELECT visitor FROM [counter]where visitorname='naresh'", conn)
result = CType(comm.ExecuteScalar(), String)
Application("counter") = result
'Dim total As String = Application("counter")
End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a new session is started
Application("counter") = Application("counter") + 1
Dim conn1 As OleDbConnection
Dim comm1 As OleDbCommand
Dim strConnection1 As String
strConnection1 = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("database/counter.mdb")
conn1 = New OleDbConnection(strConnection1)
comm1 = New OleDbCommand("UPDATE [counter] SET visitor = ' " & Application("counter") & " ' where visitorname='naresh' ", conn1)
End Sub
Updated 14-Apr-10 20:21pm

1 solution

1) Setup Web.Config file: Add your "breakpoint" (the amount of visits before the Application object dumps it's count to the Database
2) Global.asax:
Sub Application_Start(sender As Object, e As EventArgs)
  Application("RunningCount") = Visit.GetTotalCount()
End Sub

Sub Session_Start(sender As Object, e As EventArgs)
  Application("TotalUsers") = CInt(Application("TotalUsers")) + 1
  Application("RunningCount") = CInt(Application("RunningCount")) + 1

  If CInt(Application("TotalUsers")) >= CInt(ConfigurationSettings.AppSettings("MAX")) Then
    Application("TotalUsers") = 0
  End If
End Sub

3) Create a new Class file (Visits.vb):
Public Class Visits
  Public Shared Sub Count(visits As Integer)
     Dim connectionString As String = "workstation id=GEEK;packet size=4096;integrated security=SSPI;initial catalog=MyDatabase;persist security info=False"
     Dim myConnection As New SqlConnection(connectionString)
     Dim myCommand As New SqlCommand("UPDATE Visitor SET TotalVisits = @visits + TotalVisits WHERE VisitID = 1", myConnection)

     myCommand.Parameters.Add(New SqlParameter("@visits", visits))

        if myConnection.State = ConnectionState.Open Then myConnection.Close
     End Try

  End Sub
End Class

Its done!

Have a look at this article:
Implementing Hit Counter in[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900