locked
Ideal way of creating session variable and closing. RRS feed

  • Question

  • User246057125 posted

    I am creating a session variable on Page_Load as below:

            try
            {
                if (!IsPostBack)
                {
                    Session.Add("CategoryTaskFlag", "New");
                    LoadPipes();
                    LoadCategoryTypes();
                    RefreshPipeLengthGridWithTempData();
                }
                RebindGrid();
            }
            catch (Exception ex)
            {
                reuse.CustomClientMessage(ex.Message.ToString(),this.Page);
            }

    Below is the code sample of a method to pass parameters to the stored procedure. I am using this session value in this procedure.

        protected void SaveRecord()
        {
            try
            {
                int TwSerialNumber = 0;
                if (Session["CategoryTaskFlag"].ToString() == "New")
                    TwSerialNumber = 0;
                else if (Session["CategoryTaskFlag"].ToString() == "Update")
                    TwSerialNumber = Convert.ToInt32(Session["CategorySerialNumber"].ToString());
    
                SqlCommand cmdCategoryEntry = new SqlCommand("pheSch_CreateOrUpdateTubewellTypeDetails", dbConnection.cn);
    
                cmdCategoryEntry.Parameters.Add(new SqlParameter("@TaskFlag", SqlDbType.NVarChar));
                cmdCategoryEntry.Parameters["@TaskFlag"].Value = Session["CategoryTaskFlag"].ToString();
    
                cmdCategoryEntry.Parameters.Add(new SqlParameter("@SerialNumber", SqlDbType.BigInt));
                cmdCategoryEntry.Parameters["@SerialNumber"].Value = Convert.ToInt32(txtMainSerial.Text);
               cmdCategoryEntry.Parameters.Add(new SqlParameter("@pipe_type", SqlDbType.NVarChar));
                cmdCategoryEntry.Parameters["@pipe_type"].Value = txtPipe.Text;
    
                cmdCategoryEntry.CommandType = CommandType.StoredProcedure;
    
                if (dbConnection.cn.State == ConnectionState.Open)
                    dbConnection.CloseConnection();
    
                dbConnection.OpenConnection("Scheme");
                cmdCategoryEntry.ExecuteNonQuery();
                dbConnection.CloseConnection();
                RebindGrid();
                Session["CategoryTaskFlag"] = "New";
                Session["CategorySerialNumber"] = "";
                ClearFields();
            }
            catch (Exception ex)
            {
                CustomClientMessage(ex.Message.ToString());
            }
            finally
            {
                categorySerialNo = 0;
                mainSerialNo = 0;
            }
    }

    After a few entries, I notice that records stop inserting and sometimes Sys.WebForms.PageRequestParserException is displayed frequently until I refresh it several times to make it work.

    I want to know how to handle this Session variable. Is there any other way to handle global variables in ASP.NET 2.0?

    Monday, November 21, 2011 1:56 PM

Answers

  • User-578657687 posted

    Hi,

    After a few entries, I notice that records stop inserting and sometimes Sys.WebForms.PageRequestParserException is displayed frequently until I refresh it several times to make it work.

    I suggest you to check the session timeout. The Timeout specifies the number of minutes a session can be idle before it is abanbonded:

    http://msdn.microsoft.com/en-us/library/h6bb9cz9.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 22, 2011 11:29 PM