?
Solved

asp.net 3.5 hit counter

Posted on 2011-03-20
4
Medium Priority
?
580 Views
Last Modified: 2012-06-21
Hi experts

Using asp.net 3.5 with vb.net as the code-behind, has anyone got any examples of a hit counter, I need the counter to increment for each visitor to the site and not every time the page is loaded, hope somebody can help.
0
Comment
Question by:dizzycat
  • 2
4 Comments
 
LVL 6

Expert Comment

by:AkAlan
ID: 35175896
When users hit your site, do they have to log in or do you need a counter for the interface page?
0
 

Author Comment

by:dizzycat
ID: 35175936
I would just like a counter for the interface page, there is no log-in on my site.
0
 
LVL 6

Accepted Solution

by:
AkAlan earned 2000 total points
ID: 35175969
Here is an example I found here:
http://www.daniweb.com/web-development/aspnet/threads/5145


PrivateSub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load

IfNot Page.IsPostBack Then
Dim nCount As Int32 = 0
nCount = GetCounterValue()
lblTest.Text = nCount.ToString()
EndIf
EndSub

PrivateFunction GetCounterValue() As Int32

Dim ctrFile As StreamReader
Dim ctrFileW As FileStream
Dim sw As StreamWriter
Dim strPath AsString = Server.MapPath("indexcount.txt")
Dim strCounterContents AsString
Dim nCounter As Int32
 
If (File.Exists(strPath)) Then
ctrFile = File.OpenText(strPath)
strCounterContents = ctrFile.ReadLine().ToString()
ctrFile.Close()
nCounter = Convert.ToInt32(strCounterContents)
Else
nCounter = 0
EndIf
nCounter += 1
ctrFileW = New FileStream(strPath, FileMode.OpenOrCreate, FileAccess.Write)
sw = New StreamWriter(ctrFileW)
sw.WriteLine(Convert.ToString(nCounter))
sw.Close()
ctrFileW.Close()
Return nCounter
EndFunction

  
PrivateSub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load

IfNot Page.IsPostBack Then
Dim nCount As Int32 = 0
nCount = GetCounterValue()
lblTest.Text = nCount.ToString()
EndIf
EndSub

PrivateFunction GetCounterValue() As Int32

Dim ctrFile As StreamReader
Dim ctrFileW As FileStream
Dim sw As StreamWriter
Dim strPath AsString = Server.MapPath("indexcount.txt")
Dim strCounterContents AsString
Dim nCounter As Int32
 
If (File.Exists(strPath)) Then
ctrFile = File.OpenText(strPath)
strCounterContents = ctrFile.ReadLine().ToString()
ctrFile.Close()
nCounter = Convert.ToInt32(strCounterContents)
Else
nCounter = 0
EndIf
nCounter += 1
ctrFileW = New FileStream(strPath, FileMode.OpenOrCreate, FileAccess.Write)
sw = New StreamWriter(ctrFileW)
sw.WriteLine(Convert.ToString(nCounter))
sw.Close()
ctrFileW.Close()
Return nCounter
EndFunction

Open in new window


0
 
LVL 8

Expert Comment

by:Anurag Agarwal
ID: 35175993
buddy just go for http://www.hit-counts.com/ ( such types of links, get you site link register it, and get visitor no automatically calculated by this site. u wiil get a link  paste the javascript wherever u want to print the visitor no like eg


 <tr>
 <td align ="center">Visitor No.      </td>
 <td align ="left" style="text-align: left">
  <img src='http://www.hit-counts.com/counter.php?t=10&digits=9&ic=72000&cid=74644' border='0' alt=''>
</td>
</tr>



the image source u will get after registration (underlined ) its free and easy to manage, with various designs available.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question