JLM
JLM

Reputation: 57

Trying to reset session counter

I have the code below which counts sessions, bascially you can add a name and state which is saved in sessions and that information goes into a listbox. You can add as many as you like which also makes the session counter increase by 1 each time. The problem I'm having is with the reset button. It resets the page, but not the session counter, or counter back to 0.

namespace sessions
{
    public partial class Default : System.Web.UI.Page
{
    int sessionCount;

    protected void Page_Load(object sender, EventArgs e)
    {
        LabelCount.Visible = false;
       if (!Page.IsPostBack)
       {
        if (Session["Count"] == null)
        {
            sessionCount = 0;
        }
        else
        {
            sessionCount = Convert.ToInt32(Session["Count"]);
        }
       }
    }

    protected void ButtonSave_Click(object sender, EventArgs e)
    {

        LabelCount.Visible = true;


        Session["TextBoxFirst"] = TextBoxFirst.Text;
        Session["TextBoxLast"] = TextBoxLast.Text;
        Session["TextBoxState"] = TextBoxState.Text;





        if ((RequiredFieldValidator1.IsValid) && (RequiredFieldValidator2.IsValid) && (RequiredFieldValidator3.IsValid) && (RequiredFieldValidator4.IsValid) && (RegularExpressionValidator1.IsValid))
        {

            ListBox1.Items.Add(Session["TextBoxFirst"] + " " + Session["TextBoxLast"] + " " + "from" + " " + Session["TextBoxState"]);

        }
        {
            Application.Lock();
            int sessionCount = Convert.ToInt32(Session["SessionCount"]);
            sessionCount++;
            Session["SessionCount"] = sessionCount;
            Application.UnLock();
            LabelCount.Text = "Session Count Is " + sessionCount;
        }


    }

    protected void ButtonReset_Click(object sender, EventArgs e)
    {
        Session["SessionCount"] = 0;
        Server.Transfer("Default.aspx");


    }
}

}

Upvotes: 0

Views: 810

Answers (2)

Ryan
Ryan

Reputation: 3982

Can't you simply reset the counter in the ButtonReset_Click event?

protected void ButtonReset_Click(object sender, EventArgs e)
{
    Session["Count"] = 0;
    Server.Transfer("Default.aspx");
}

I'd also consider wrapping the session access into a simple property

public int SessionCount
{
    get
    {
        return Session["Count"] ?? 0;
    }

    set
    {
        Session["Count"] = value;
    } 
}

And remove the class level variable. This makes the code simpler.

Upvotes: 1

dyson
dyson

Reputation: 896

The global sessionCount is only being set to 0 if the Session variable, Count, is null.

As the Page_Load event sets the variable to either 0 or the actual count, then when the button is pressed, it can never be null - upon the postback, the Page_Load event will fire before the control events do.

You could remedy this with the following wrapper around the logic in Page_Load:

if (!Page.IsPostBack)
{
    if (Session["Count"] == null)
    {
        sessionCount = 0;
    }
    else
    {
        sessionCount = Convert.ToInt32(Session["Count"]);
    }
}

Now, this logic will not be hit when the reset button is clicked, although you'll probably find that the counter still isn't null at this point, and so you perhaps need to remove that check in the reset button handler.

Upvotes: 1

Related Questions