Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6974063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:11:20+00:00 2026-05-27T17:11:20+00:00

I need to persist in Session some data. I wrote many properties like that:

  • 0

I need to persist in Session some data.
I wrote many properties like that:

public List<string> FillOrder
{
    get { return Session[SessionKeys.QueryFillOrder] as List<string> ?? new List<string>(); }
    set { Session[SessionKeys.QueryFillOrder] = value; }
}

When I have to consume this data I have to write code like that:

List<string> fillOrder = FillOrder;
fillOrder.Add(accordion.ID);
FillOrder = fillOrder;

that seems to me so ugly, because I would prefer to do that:

FillOrder.Add(accordion.ID);

but this way my value would not be saved back in Session.

Can you think of any better way to achieve the same result?

Thank you very much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:11:20+00:00Added an answer on May 27, 2026 at 5:11 pm

    I always use a wrapper class around the ASP.NET session to simplify access to session variables:

    public class MySession
    {
        // private constructor
        private MySession()
        {
           FillOrder = new List<string>();
        }
    
        // Gets the current session.
        public static MySession Current
        {
          get
          {
            var session = (MySession)HttpContext.Current.Session["__MySession__"];
            if (session == null)
            {
              session = new MySession();
              HttpContext.Current.Session["__MySession__"] = session;
            }
            return session;
          }
        }
    
        // **** add your session properties here, e.g like this:
        public List<string> FillOrder {get; set; }
        public string Property1 { get; set; }
        public DateTime MyDate { get; set; }
        public int LoginId { get; set; }
    }
    

    This class stores one instance of itself in the ASP.NET session and allows you to access your session properties in a type-safe way from any class, e.g like this:

    MySession.Current.FillOrder.Add(accordion.ID);
    
    int loginId = MySession.Current.LoginId;
    
    string property1 = MySession.Current.Property1;
    MySession.Current.Property1 = newValue;
    
    DateTime myDate = MySession.Current.MyDate;
    MySession.Current.MyDate = DateTime.Now;
    

    This approach has several advantages:

    • you can initialize your session variables in the constructor (i.e. new List<string>)
    • it saves you from a lot of type-casting
    • you don’t have to use hard-coded session keys throughout your application (e.g. Session[“loginId”]
    • you can document your session items by adding XML doc comments on the properties of MySession
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When a user logs into my site, I need to persist that login session.
I need to persist an object that is not marked with the serializable attribute.
I think I need some help understanding how static objects persist in an ASP.Net
I have a launchd daemon that every so often uploads some data via a
I need to persist just one object in session scope of my JSF application.
How Can I persist a User-Specific data for an ASP.Net application. I tried Session
I have a set of configuration items I need to persist to a human
I have an array of bytes in my C# class, and need to persist
I have an app where I need a BOOL value to persist across launches.
I have the following setup, and I need to know how to persist state.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.