943,812 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 43752
  • ASP.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 7th, 2004
0

ASP .NET hit counter?

Expand Post »
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.
Reputation Points: 115
Solved Threads: 7
Practically a Master Poster
Slade is offline Offline
633 posts
since Mar 2004
Apr 7th, 2004
0

Re: ASP .NET hit counter?

ok I used the global.asax page for this one, got this information off www.4guysfromrolla.com, good site.


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

'This variable Hits will store the # of visitors

Application("Hits") = 0

EndSub

Sub Session_Start(ByVal sender AsObject, 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()

EndSub

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.
Reputation Points: 115
Solved Threads: 7
Practically a Master Poster
Slade is offline Offline
633 posts
since Mar 2004
Apr 8th, 2004
0

Re: ASP .NET hit counter?

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 ;-).
Moderator
Reputation Points: 322
Solved Threads: 28
The C# Man, Myth, Legend
Tekmaven is offline Offline
914 posts
since Feb 2002
Apr 8th, 2004
0

Re: ASP .NET hit counter?

Moderator
Reputation Points: 322
Solved Threads: 28
The C# Man, Myth, Legend
Tekmaven is offline Offline
914 posts
since Feb 2002
Apr 8th, 2004
0

Re: ASP .NET hit counter?

Also, to make the hitcount perminate, you'll want to use a file, a database, or registry key to keep track of the hitcount ;-).
Moderator
Reputation Points: 322
Solved Threads: 28
The C# Man, Myth, Legend
Tekmaven is offline Offline
914 posts
since Feb 2002
Apr 13th, 2004
0

Help?

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:
Reputation Points: 115
Solved Threads: 7
Practically a Master Poster
Slade is offline Offline
633 posts
since Mar 2004
Apr 15th, 2004
0

Done... and done

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

  
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

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:
Reputation Points: 115
Solved Threads: 7
Practically a Master Poster
Slade is offline Offline
633 posts
since Mar 2004
Sep 29th, 2006
0

Re: ASP .NET hit counter?

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\



Click to Expand / Collapse  Quote originally posted by Slade ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Shalvin is offline Offline
11 posts
since Sep 2006
Sep 30th, 2006
0

Re: ASP .NET hit counter?

Click to Expand / Collapse  Quote originally posted by Shalvin ...
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
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
Jun 27th, 2007
0

Re: ASP .NET hit counter?

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dileep_mpm1000 is offline Offline
1 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: validate databound dropdowlnlist
Next Thread in ASP.NET Forum Timeline: How to Insert Data into Database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC