blah blah blah is here! blah blah » Close

up0down
link

I have this code in my Global.asax file

void Application_Start(object sender, EventArgs e) 
{
// Code that runs on application startup
Application.Add("clickCount", 0);
}


I have codes in class View.cs

public class View
{
private int i;

public int I
{
get { return i; }
set { i = value; }
}

public View()
{
//
// TODO: Add constructor logic here
//
i = 0;
}
public View(int I)
{
i = I;
}
}


Now in my Default.aspx page I want to write code in such a manner so that with every time page loads the number of "View" will be increased.
I've written that code this way:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int clicks = (int)Application["clickCount"];
clicks++;
Application["clickCount"] = clicks;

View view = new View(clicks);

Label1.Text = "You are visitor no: " + view.I.ToString() + ", thanks for visiting...";

//String.Format("You are visitor no: " + Application["clickCount"] + ", thanks for visiting...");
}
}


And expectedly it does not work. When browser closed and re-opened, it shows fresh viewCount = 1. Can this be modified so that I can catch every "clicks" in my class View.cs and with its object the increased number can be shown?

last answered 2 years ago

1 answers

up2down
link

you should use a database because you are using a memory instead of database and you are depending on the session which can expire at any time or you can loose any data on a server restart or any IIS problem.

sanjib
226

Many thanks. That's exactly happened. Will XML file instead of database work? Suppose I try to hold each viewer's click in an File?

muster
1556

yes anything will work even System.IO.File.WriteAllText will work, all what you need is to store in a non volatile environment. And i should make a correction here, you didn't store the count in a session but in a global variable because they are different a bit since the session depends on the user and browser/server while the Global variable is only stored in the memory of the server which can be removed in many cases that i stated above.

Feedback