Solved

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

Posted on 2007-11-13
2
298 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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