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

Recommended Answers

All 14 Replies

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

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

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

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

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

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");


private void 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.

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

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

um this wouldn't work at all

Thanks I will try that

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");


private void 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.

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)
        [B]AddOnlineUser()[/B]
    End Sub

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

Thanks I will try that

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

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.