简体   繁体   中英

Page Counter not working ASP.NET C#

everybody. I've been trying to follow exactly what it says in my textbook, but to no avail. Now, my problem is that I'm currently attempting to make a page counter to keep track of how many times each page has been accessed, then display each value on a new page.

Here is the C# Counter code which is the same for all pages:

int sessionCount = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["CountMain"] == null)
            sessionCount = 0;
        else
            sessionCount = Convert.ToInt32(Session["CountMain"]);
        sessionCount++;
    }
    protected void Page_PreRender(object sender, EventArgs e)
    {
        Session["CountMain"] = sessionCount;
    }

The prerender is something I added in myself after research on the internet, and in the textbook. No luck.

Here is the Counter Page C# code:

public partial class Counter : System.Web.UI.Page
{
    int sessionCount = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        string sessionMain = Session["CountMain"].ToString();
        string sessionComment = Session["CountComment"].ToString();
        string sessionCompleted = Session["CountCompleted"].ToString();
        string sessionCurrent = Session["CountCurrent"].ToString();
        string sessionAbout = Session["CountAbout"].ToString();
        string sessionContact = Session["CountContact"].ToString();
        string sessionCounter = Session["CountCounter"].ToString();

        if (Session["CountCounter"] == null)
            sessionCount = 0;
        else
            sessionCount = Convert.ToInt32(Session["CountCounter"]);
        sessionCount++;

        lblAboutCount.Text = sessionAbout;
        lblCommentCount.Text = sessionComment;
        lblCompletedCount.Text = sessionCompleted;
        lblContactCount.Text = sessionContact;
        lblCounterCount.Text = sessionCounter;
        lblCurrentCount.Text = sessionCurrent;
        lblMainCount.Text = sessionMain;
    }

When I try to run it, I get a "NullReferenceException was unhandled by user code, Object reference not set to an instance of an object." Error.

Thanks in advance.

EDIT #1

Okay, Thanks to Hexxangonal, the counter is now working. However, my Counter Page is now counting it self by 2 times. (incrementing by 2 every time it loads)

public partial class Counter : System.Web.UI.Page
{
    int sessionCount = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["CountCounter"] == null)
        {
            sessionCount = 0;
            Session["CountCounter"] = sessionCount;
        }
        else
        {
            sessionCount = Convert.ToInt32(Session["CountCounter"]);
            sessionCount++;
            Session["CountCounter"] = sessionCount;
        }

        Session["CountCounter"] = sessionCount;

        lblAboutCount.Text = Convert.ToString(Session["CountAbout"]);
        lblCommentCount.Text = Convert.ToString(Session["CountComment"]);
        lblCompletedCount.Text = Convert.ToString(Session["CountCompleted"]);
        lblContactCount.Text = Convert.ToString(Session["CountContact"]);
        lblCounterCount.Text = Convert.ToString(Session["CountCounter"]);
        lblCurrentCount.Text = Convert.ToString(Session["CountCurrent"]);
        lblMainCount.Text = Convert.ToString(Session["CountMain"]);
    }

The NullReference exception is probably coming from one of your Session["CountXXXXX"].ToString() lines (CountXXXXX is one of your count objects like CountMain ) because Session["CountXXXXX"] does not exist (it is null).

You can actually simplify that page to the following logic and you will bypass the issue as the null will just be assigned to the string variable.

public partial class Counter : System.Web.UI.Page
{
    int sessionCount = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["CountCounter"] == null)
            sessionCount = 0;
        else
            sessionCount = Convert.ToInt32(Session["CountCounter"]);
        sessionCount++;

        // **NEW** Save the new count value
        Session["CountCounter"] = sessionCount;

        lblAboutCount.Text = Session["CountAbout"];
        lblCommentCount.Text = Session["CountComment"];
        lblCompletedCount.Text = Session["CountCompleted"];
        lblContactCount.Text = Session["CountContact"];
        lblCounterCount.Text = sessionCounter;
        lblCurrentCount.Text = Session["CountCurrent"];
        lblMainCount.Text = Session["CountMain"];
    }

There was also an issue where you were mixing integers and strings with the sessionCount variable with a member variable and a local variable (respectively). I have cleaned this up.

The problem is with these two lines

   if (Session["CountCounter"] == null)
        sessionCount = 0;
    else
        sessionCount = Convert.ToInt32(Session["CountCounter"]);
    sessionCount++;

You are not putting back these values into the session variable.
You should be doing as follows:-

    if (Session["CountCounter"] == null)
    {
        sessionCount = 0;
        Session["CountCounter"]=sessionCount;
    }
    else
    {
        sessionCount = Convert.ToInt32(Session["CountCounter"]);
        sessionCount++;
        Session["CountCounter"]=sessionCount;
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM