when is better to use session to maintain class or ptoperties of class?

Mohammad Ali Echreshavi 101 Reputation points
2023-06-05T10:41:14.89+00:00

hi,

i will be glad if any one can explain for me what difference between bellow code or when is better i used session,

and if i want use session , which is better? keep whole class or all the feilds of class

and that in temsof efficiency, this work is correct?

   public String Name
        {
            get
            {
                return HttpContext.Current.Session["Report_Name"] as String;
            }
            set
            {
                HttpContext.Current.Session["Report_Name"] = value;
            }
        }

and

   public String Name {get ; set;}
        

Developer technologies | ASP.NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2023-06-05T11:37:24.7033333+00:00

    As far as I can tell you're asking how Session works.

    Session persists user data in the web server's memory by a key. The key is stored in an HTTP cookie. On each request the browser sends the HTTP cookie to the web server. The Session API uses the key to find and load the user's Session.

    Please read the official documentation.

    ASP.NET Session State Overview

    The difference between the first code block that uses Session and the second is the second code block does not persist the "Name" property between requests. The first code block saves the "Name" property between requests.

    A couple of points, the first code block is not well designed. Session is volatile as it exists in server memory. Always check is Session exists before trying to use a Session value. Again, see the docs.

    In terms of architecture, session exists in server memory. In a load balanced environment (multiple servers) only one server could have the Session state.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.