Putting a class in a Session

  • Thread starter Thread starter A.M
  • Start date Start date
A

A.M

Hi,

How can I store a class (including all it's members) in session object?

I already tried something like:

Session["Status"] = myClass;

but it stores the value of myClass.ToString in it!!

Thanks,
Alan
 
The Session object is a collection of -objects-. Any objects, but to
properly use the class after retrieving from the Session, you must cast it
back from an object to its native type:
myClass = (MyType)Session["Status"]; I'm not 100% sure that this is the
problem that you are having, but that is how I use Session to store an
object.

I use Session all the time to store complete objects. It works great.

Jeffrey Palermo
 
Than you for help.

The Trace is on for all pages.
How can I see the members of that object in Trace output? Because Trace just
shows the class name (like myClass)!!.

Thanks again,
Alan



Jeffrey Palermo said:
The Session object is a collection of -objects-. Any objects, but to
properly use the class after retrieving from the Session, you must cast it
back from an object to its native type:
myClass = (MyType)Session["Status"]; I'm not 100% sure that this is the
problem that you are having, but that is how I use Session to store an
object.

I use Session all the time to store complete objects. It works great.

Jeffrey Palermo

A.M said:
Hi,

How can I store a class (including all it's members) in session object?

I already tried something like:

Session["Status"] = myClass;

but it stores the value of myClass.ToString in it!!

Thanks,
Alan
 
You will need to override the .ToString() method for your class. I believe
the trace calls .ToString() on each object in Session, etc.

bill

A.M said:
Than you for help.

The Trace is on for all pages.
How can I see the members of that object in Trace output? Because Trace just
shows the class name (like myClass)!!.

Thanks again,
Alan



Jeffrey Palermo said:
The Session object is a collection of -objects-. Any objects, but to
properly use the class after retrieving from the Session, you must cast it
back from an object to its native type:
myClass = (MyType)Session["Status"]; I'm not 100% sure that this is the
problem that you are having, but that is how I use Session to store an
object.

I use Session all the time to store complete objects. It works great.

Jeffrey Palermo

A.M said:
Hi,

How can I store a class (including all it's members) in session object?

I already tried something like:

Session["Status"] = myClass;

but it stores the value of myClass.ToString in it!!

Thanks,
Alan
 
Yes, that's right. Session stores objects, not just strings, and if you
don't explicitly define a ToString() method, then the one inherited from
Ojbect will be called, and that's not what you want, so override ToString()
and have it return some key information in your object.

Jeffrey Palermo

William F. Robertson said:
You will need to override the .ToString() method for your class. I believe
the trace calls .ToString() on each object in Session, etc.

bill

A.M said:
Than you for help.

The Trace is on for all pages.
How can I see the members of that object in Trace output? Because Trace just
shows the class name (like myClass)!!.

Thanks again,
Alan
cast
it
back from an object to its native type:
myClass = (MyType)Session["Status"]; I'm not 100% sure that this is the
problem that you are having, but that is how I use Session to store an
object.

I use Session all the time to store complete objects. It works great.

Jeffrey Palermo

Hi,

How can I store a class (including all it's members) in session object?

I already tried something like:

Session["Status"] = myClass;

but it stores the value of myClass.ToString in it!!

Thanks,
Alan
 
Hi Alan,

I've found that there is anothe thread named
"Re: Putting a class in a Session" in the same newsgroup on this issue and
some community member also provide some informative suggestions there. I'd
appreciate if you have a look there. Also, if you feel convenient that we
continue to discuss in that thread, please feel free to followup there.
Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Back
Top