Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-11-13
2
Medium Priority
?
300 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 1000 total points
ID: 20278388
to retrive

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

Assisted Solution

by:Vinod_nk
Vinod_nk earned 1000 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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

688 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