954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

count no of Visitors

Hi all,
I have developed my own website.
I need to add a counter that tells the number of visitors
to my website.
can u plz help how to do.
I know only a little about sessions since i dont have references.But
Dont worry I will understand your codes and explanations
with love
Embeza

Embeza
Light Poster
48 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

I would instantiate an application variable in the application on start pulling the value from whereever it is saved

Increment it in session onstart

Save it back to whereever in application onend

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

Hi campkev,
a little broader please.
your explanation is too short.Can you tell me the code plz.
with regards
Embeza

Embeza
Light Poster
48 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

well where are you going to store this variable? database, file?

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

As you wanting to store this in the Database?

You could us an old CGI standard? Or you could do something like this website shows (writing to a text file).

http://www.c-sharpcorner.com/Code/2002/Aug/HitCounterAspNet.asp

or another example

http://www.asp101.com/samples/counter_aspx.asp

Paladine
Master Poster
Team Colleague
824 posts since Feb 2003
Reputation Points: 211
Solved Threads: 27
 

Hi paladine,
I need to store it in database.
So tell me how to do.
with regards
Embeza

Embeza
Light Poster
48 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

Hi there,
I have given you the tools to do this. Give it a try and if you have specific problems I will help you with it. The code to do this is right there in the links I provided.

Another one method is this (not the most elegant):

1. Create a table to store the hit count in the database
2. Create a session variable for storage of the Hit Count
3. Connect to the database & retrieve the Hit Count Value from the table.
4. Increment the hit count value and then write back
to the database the updated value.
5. Place a Label control on the page to display the hit count value, and use the session variable to display the value.

Hope this helps.

FYI - a little effort goes a long way : http://www.asp101.com/samples/counter_db_aspx.asp

Paladine
Master Poster
Team Colleague
824 posts since Feb 2003
Reputation Points: 211
Solved Threads: 27
 

Hi,

1. Create one database named "Sample", create a new table called "visitorcount" and create one column called "visitorcount" as integer in sql server.

2. Then put one label called "Label1" in .aspx page

3. Put the follwing codings in your .cs page.

The code follows:

using System.Data;
using System.Data.SqlClient;

protected System.Web.UI.WebControls.Label Label1;
public int count;
SqlConnection connection = new SqlConnection("database=sample;user=sa;password=sa");


privatevoid Page_Load(object sender, System.EventArgs e)
{
string strqry = "select visitorcount from visitorcount";

connection.Open();

SqlCommand command = new SqlCommand(strqry,connection);

SqlDataReader reader = command.ExecuteReader();

if (reader.Read())
{

count = Convert.ToInt32(reader.GetValue(0));

Label1.Text = count.ToString();

count = count+1;
}

reader.Close();

string strUpdateqry = "update visitorcount set visitorcount=" +count;

SqlCommand command1 = new SqlCommand(strUpdateqry,connection);

command1.ExecuteNonQuery();


Label1.Text = count.ToString();
}

I hope this will be very clear to u.
Ali Abbas from Dubai.

AliAbbas
Newbie Poster
2 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

thank u Ali, I will try that.

thank you paladine but the sites you recommeded me do not
have enough codes and explanations.
As a beginner i need a site of more of coding and explanation (if any)
But i thank very much 4 ur suggestion
with regards
Embeza

Embeza
Light Poster
48 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

go to Global.asax
write this codein application_start()


Dim i As Integer
i = Application("hitcount")
Application.Lock()
i = i + 1
Application("hitcount") = i + 1
Application.UnLock()



This Application("hitcount") varible is used to count no of users who visited ur site.
if any problem contact me
njoy

nunnakk
Newbie Poster
4 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
 

um this wouldn't work at all

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

Thanks I will try that

Embeza
Light Poster
48 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

Instead of hitting the db twice, you can increase the count with one query
"update TheTable SET visitorcount = visitorcount+1"

Hi, 1. Create one database named "Sample", create a new table called "visitorcount" and create one column called "visitorcount" as integer in sql server. 2. Then put one label called "Label1" in .aspx page 3. Put the follwing codings in your .cs page. The code follows: using System.Data; using System.Data.SqlClient; protected System.Web.UI.WebControls.Label Label1; public int count; SqlConnection connection = new SqlConnection("database=sample;user=sa;password=sa"); privatevoid Page_Load(object sender, System.EventArgs e) { string strqry = "select visitorcount from visitorcount"; connection.Open(); SqlCommand command = new SqlCommand(strqry,connection); SqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { count = Convert.ToInt32(reader.GetValue(0)); Label1.Text = count.ToString(); count = count+1; } reader.Close();

string strUpdateqry = "update visitorcount set visitorcount=" +count; SqlCommand command1 = new SqlCommand(strUpdateqry,connection); command1.ExecuteNonQuery();

Label1.Text = count.ToString(); } I hope this will be very clear to u. Ali Abbas from Dubai.

plazmo
Posting Whiz in Training
207 posts since Aug 2005
Reputation Points: 23
Solved Threads: 16
 

Use Global.asax file and add this

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        Application("OnlineUsers") = 0
    End Sub
    
    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        Application("OnlineUsers") += 1
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        Application("OnlineUsers") -= 1
    End Sub


You need add this in your web.config (in system.web tag):

<sessionState mode="InProc" cookieless="false" timeout="60" />


If you want it in database just place Subroutine in these events like:

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        <strong>AddOnlineUser()</strong>
    End Sub


Where AddOnlineUser() is saome public subroutine for manipulatig data.

ManicCW
Junior Poster in Training
95 posts since Nov 2005
Reputation Points: 13
Solved Threads: 11
 
Thanks I will try that


thanks....
it works................

jeju
Newbie Poster
2 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You