Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net Report Printing Issue 3 31
imap mails 1 22
C# Desktop Application 3 29
Winforms -- Capture CTRL+SHIFT+D at the form not working? 3 17
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 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

12 Experts available now in Live!

Get 1:1 Help Now