Solved

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

Posted on 2007-11-13
2
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

630 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