Solved

Can I put custom class instances into Sessions in ASP.NET 2.0 / C#?

Posted on 2007-11-13
2
294 Views
Last Modified: 2013-11-07
I have a custom class that I would like to put an instance of into a user Session. Is this possible? For example, if I have class:

 public class ClassTest
    {
        private string _test1;
        public string Test1
        {
            get { return _test1; }
            set { _test1 = value; }
        }
    }

What I want to do is put the instance into the Session like this:
ClassTest abc = new ClassTest();
Session["special"] = abc;

I want to be able to do this from anywhere on my site but doesn't work:
Response.Write(Session["special"].Test1.ToString());

How can I accomplish this with session variables?
0
Comment
Question by:bemara57
2 Comments
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 250 total points
ID: 20278388
to retrive

ClassTest abc  = (ClassTest)Session["special"];
0
 
LVL 8

Assisted Solution

by:Vinod_nk
Vinod_nk earned 250 total points
ID: 20278828
Hi,
You will have to make the class serializable, to add/retrieve to/from session.
So your class definition will look like :

[Serializable()]
public class ClassTest
    {
        private string _test1;
        public string Test1
        {
            get { return _test1; }
            set { _test1 = value; }
        }
    }

Rgds
V
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now