I'd like to count how many visitors came to my site. Can you give me a quick and easy way to do this?

Well that's what an application variable is all about.

Try google with this. You'll get a solution.

commented: helpful till the end +1

This is what I did. But this gets lost when I upload my project again?
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
Application("Hits") = Application("Hits") + 1

End Sub

Where are you actually incrementing the variable.
It is supposed to be done in the global.asa file

Yes that's where I did it. But when I make changes on my project and upload it again it starts at 1 again

I believe thats how it works.
The variable persists for a single application build.
When you are uploading a different build, it would start off again.

Why would you require it to persist across builds anyway?

When going to my website from a different computer, I saw the visitor count was back to 1. I guess it's not working. What could be the problem?

Could you send the content of your global.asa file?
I think you are doing something wrong here.

First of all thank you bala24 for your time

<%@ Application Language="VB" %>

<script runat="server">

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
              Application("mydate") = DateTime.Today.Date.ToShortDateString
        
    End Sub
    
    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
          End Sub
        
    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
          End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        Application("Hits") = Application("Hits") + 1

          End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
           End Sub
       
</script>

Oops My mistake.
I should have mentioned that you have to persist your current count by storing it in some file or db table on Application End event.

The problem is that every time you start your application, the Application variable is getting created afresh.

So do the following:
1). In application_End event store it somewhere.
2).In Application_Start event retrieve this value.

I think this should solve your problem.

P.S its good practice to use Application.Lock and Application.UnLock prior to and after incrementing the Application variable.This would ensure handling concurrent access.

If you have any doubts refer this:

http://www.daniweb.com/forums/thread48851.html

where would it be better to save the number
database table or just a textfile

Well thats up to you to decide.
Usually db table can be used in cases if you require to check later on as to how many people are hitting your application on a per week or any other duration to keep a tab.
So you can have a date field along with the count for this purpose.

If not, I would suggest go for a text field if you simply want to show the content on the
page and don't want to keep track of anything.

Thank you bala24.
If you don't mind I need some more help. Till now I have only worked with database from a webpage through grids. I don't know how to make a connection etc from the global.asax code.

Well That's no difference then what you did with the webpage with grids.

You just need to write the code to connect and retrieve/insert record from db in the particular event.

Last question
Where do I read and where do I write to my databas

Application Start, Session Start, Session End, Application End

Well I did mention this previously.

Application_Start : Retrieve the last written value from database and update application variable.

Session_Start: Increment the variable.

Session_End: This is not useful in your case. This helps in cases if you want to display how many users are currently online. eg.Forums in DW.

Application_End : Write back the application variable in database.

If you want to maintain history of visits on a daily basis, you can as well add a new row every time along with date to your table and while retrieving get the last inserted row based on a unique value (max identity column or last updated date).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.