User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 425,991 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,681 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 16372 | Replies: 9
Reply
Join Date: Mar 2004
Location: Brisbane
Posts: 632
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Rep Power: 7
Solved Threads: 6
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

ASP .NET hit counter?

  #1  
Apr 7th, 2004
Anyone got any idea's for a hit counter for a webpage in asp .NET, using the VB code behind? Any help would be really appreciated.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2004
Location: Brisbane
Posts: 632
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Rep Power: 7
Solved Threads: 6
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Re: ASP .NET hit counter?

  #2  
Apr 7th, 2004
ok I used the global.asax page for this one, got this information off www.4guysfromrolla.com, good site.


Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

'This variable Hits will store the # of visitors

Application("Hits") = 0

End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)

'Lock the Application for concurrency issues

Application.Lock()

'Increment the counter

Application("Hits") = Application("Hits") + 1

'Unlock the Application

Application.UnLock()

End Sub

Now to declare it in my webform...

This page has been viewed
<%=application("Hits")%>
times.

Ok guys, this works but I would not by any means recommend it because it resests itself every time you rebuild the project, plus it resets whenever the server your site is on reboots or restarts, if anyone has a better alternative to this, please let me know. The next question I have is how do I replace the number on the hit counter, with a graphic, say it said 2 people have visited the page in plain text, I want that "2" to be an image of a 2, I've found a little on this but it's not working. Will post back if I find anything out.
Reply With Quote  
Join Date: Feb 2002
Location: New York
Posts: 862
Reputation: Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light 
Rep Power: 12
Solved Threads: 15
Moderator
Tekmaven's Avatar
Tekmaven Tekmaven is offline Offline
The C# Man, Myth, Legend

Re: ASP .NET hit counter?

  #3  
Apr 8th, 2004
Are you fermiliar with GDI+ in .NET? What you need to do is make a dynamic image. Make a new webform, give it an image content type, and in the page's code, create the graphic (using the hitcount you want), do a Response.Flush(), and then then write the Graphic using Response.Write (Maybe WriteStream). Then, in your webpage, just do <img> tags to your dynamic image webform ;-).
-Ryan Hoffman

ASP.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
Reply With Quote  
Join Date: Feb 2002
Location: New York
Posts: 862
Reputation: Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light 
Rep Power: 12
Solved Threads: 15
Moderator
Tekmaven's Avatar
Tekmaven Tekmaven is offline Offline
The C# Man, Myth, Legend

Re: ASP .NET hit counter?

  #4  
Apr 8th, 2004
-Ryan Hoffman

ASP.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
Reply With Quote  
Join Date: Feb 2002
Location: New York
Posts: 862
Reputation: Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light 
Rep Power: 12
Solved Threads: 15
Moderator
Tekmaven's Avatar
Tekmaven Tekmaven is offline Offline
The C# Man, Myth, Legend

Re: ASP .NET hit counter?

  #5  
Apr 8th, 2004
Also, to make the hitcount perminate, you'll want to use a file, a database, or registry key to keep track of the hitcount ;-).
-Ryan Hoffman

ASP.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
Reply With Quote  
Join Date: Mar 2004
Location: Brisbane
Posts: 632
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Rep Power: 7
Solved Threads: 6
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Help Help?

  #6  
Apr 13th, 2004
Ok, I have been trying to find where I can make a vb.net perminant hit counter, most are by using text files (and I would definitely prefer them over a database). My problem now is, I can't find any code to make one! The little code I can find is poorly explained and I was wondering if one of you guys could maybe help me out here. God Bless!:cry:
Reply With Quote  
Join Date: Mar 2004
Location: Brisbane
Posts: 632
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Rep Power: 7
Solved Threads: 6
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Solution Done... and done

  #7  
Apr 15th, 2004
Fixed this one also guys. Here is the code for my hit counter. There was also some code to create .gif images for the number images on the fly but... it proved a little difficult, I'll probably put them in version two of the website. The company website is nearly up the boss loves it, I'll keep you guys updated so I can get some feedback on my work, thanks! Here is the code

  
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
Dim nCount As Int32 = 0
nCount = GetCounterValue()
lblTest.Text = nCount.ToString()
End If
End Sub

Private Function GetCounterValue() As Int32

Dim ctrFile As StreamReader
Dim ctrFileW As FileStream
Dim sw As StreamWriter
Dim strPath As String = Server.MapPath("indexcount.txt")
Dim strCounterContents As String
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
End If
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
End Function


and of course, don't forget the "Imports System.IO" (no quotations) at the top of the code. Happy coding guys! Thanks again Tekmaven!

Slade :cheesy:
Reply With Quote  
Join Date: Sep 2006
Location: Kochi, Kerala, India
Posts: 1
Reputation: Shalvin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Shalvin Shalvin is offline Offline
Newbie Poster

Re: ASP .NET hit counter?

  #8  
Sep 29th, 2006
But how to do the same in C#. For instance :

int hits = Int32.Parse(Application["Hits"]);

Will create a error
Error 2 Argument '1': cannot convert from 'object' to 'string' E:\Sh\ASP\cS HitCounter\Global.asax 26 32 E:\...\cS HitCounter\



Originally Posted by Slade View Post
ok I used the global.asax page for this one, got this information off www.4guysfromrolla.com, good site.


Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

'This variable Hits will store the # of visitors

Application("Hits") = 0

End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)

'Lock the Application for concurrency issues

Application.Lock()

'Increment the counter

Application("Hits") = Application("Hits") + 1

'Unlock the Application

Application.UnLock()

End Sub

Now to declare it in my webform...

This page has been viewed
<%=application("Hits")%>
times.

Ok guys, this works but I would not by any means recommend it because it resests itself every time you rebuild the project, plus it resets whenever the server your site is on reboots or restarts, if anyone has a better alternative to this, please let me know. The next question I have is how do I replace the number on the hit counter, with a graphic, say it said 2 people have visited the page in plain text, I want that "2" to be an image of a 2, I've found a little on this but it's not working. Will post back if I find anything out.
Reply With Quote  
Join Date: Aug 2005
Location: Ohio
Posts: 204
Reputation: plazmo is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: ASP .NET hit counter?

  #9  
Sep 30th, 2006
Originally Posted by Shalvin View Post
But how to do the same in C#. For instance :

int hits = Int32.Parse(Application["Hits"]);

Will create a error
Error 2 Argument '1': cannot convert from 'object' to 'string' E:\Sh\ASP\cS HitCounter\Global.asax 26 32 E:\...\cS HitCounter\





int hits = Int32.Parse(Application["Hits"].ToString());

but
int hits = (int)Application["Hits"];

would be better because the object is already an int
Reply With Quote  
Join Date: Jun 2007
Location: India
Posts: 1
Reputation: dileep_mpm1000 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dileep_mpm1000 dileep_mpm1000 is offline Offline
Newbie Poster

Re: ASP .NET hit counter?

  #10  
Jun 27th, 2007
Write this code to your global.asax
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Application.Lock()
Application("counter") = Application("counter") + 1
Application.UnLock()
End Sub

and write these lines in your default homepage

<%
Application.Lock()
Application("counter") = CType(Application("counter") + 1, Int32)
Application.UnLock()
%>

Everything will be fine then.

Enjoy the weekend.

Dileep
dileep@active-technologies.net
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb ASP.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 12:00 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC