locked
Session state reset to zero with this code! What's the problem? RRS feed

  • Question

  • User-1392235324 posted

    There're two web forms on the same project; as I know Session State works with web forms with the same project, but when I go from web form to another the value resets to zero.

    Here the code for these web forms

    Default4.aspx.cs

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["icks"] == null)
                {
                    Session["Clicks"] = 0;
                }
                TextBox1.Text = Session["Clicks"].ToString();
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            int clicksCount = (int)Session["Clicks"] + 1;
            TextBox1.Text = clicksCount.ToString();
            Session["Clicks"] = clicksCount;
        }
    

    Default5.aspx.cs

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["icks"] == null)
                {
                    Session["Clicks"] = 0;
                }
                TextBox1.Text = Session["Clicks"].ToString();
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            int clicksCount = (int)Session["Clicks"] + 1;
            TextBox1.Text = clicksCount.ToString();
            Session["Clicks"] = clicksCount;
        }
    

    Thursday, April 9, 2015 3:10 AM

Answers

  • User-1618234021 posted

    Hi

    Is it a spelling mistake? You are using 2 different key names while checking and setting. It should be Session["Clicks"] instead of Session["icks"]

     if (!IsPostBack)
            {
                if (Session["Cicks"] == null)
                {
                    Session["Clicks"] = 0;
                }
                TextBox1.Text = Session["Clicks"].ToString();
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 9, 2015 3:16 AM

All replies

  • User-1618234021 posted

    Hi

    Is it a spelling mistake? You are using 2 different key names while checking and setting. It should be Session["Clicks"] instead of Session["icks"]

     if (!IsPostBack)
            {
                if (Session["Cicks"] == null)
                {
                    Session["Clicks"] = 0;
                }
                TextBox1.Text = Session["Clicks"].ToString();
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 9, 2015 3:16 AM
  • User-1392235324 posted

    Dear adeelehsan

    I corrected the spling as

    if (!IsPostBack)
            {
                if (Session["Cicks"] == null)
                {
                    Session["Clicks"] = 0;
                }
                TextBox1.Text = Session["Clicks"].ToString();
            }

    but, I have same problem!

    Sunday, April 12, 2015 2:19 AM
  • User-1618234021 posted

    Dear adeelehsan

    I corrected the spling as

    if (!IsPostBack)
            {
                if (Session["Cicks"] == null)
                {
                    Session["Clicks"] = 0;
                }
                TextBox1.Text = Session["Clicks"].ToString();
            }

    but, I have same problem!

    I am afraid its still not correct. It has to be as follows:

    if (!IsPostBack)
            {
                if (Session["Clicks"] == null)
                {
                    Session["Clicks"] = 0;
                }
                TextBox1.Text = Session["Clicks"].ToString();
            }

    Sunday, April 12, 2015 6:53 AM