Storing an object in a session

G

Guest

All,

I am a bit of noob to ASP.NET, and I am writing some OO based pages to try
and emerge myself into it, however, I have come across a problem that has me
completely stumped!

What I want to do is to take an object that I am using on one page, and pass
it to another page - this, I believe is easily achievable with a session
(yes, I know about Server.Transfer for form elements, but I want an Object
based upon a class that I have written).

So, for example, lets say that I have a SqlDataReader, which I have
populated and used successfully bound to combo (dropdown) box. What I want to
do is:

'code here declares and uses sqlReader of type SqlDataReader
Session("sqlReader") = sqlReader

And then on the next page when I navigated to it:

Dim sqlReader as SqlDataReader = Session("sqlReader")

However, no matter what I try, I always get "Object reference not set to an
instance of an object. " errors.

Can anyone explain or point me in the direction of where I can find out how
to store and use objects in a session, or an alternative way of doing this? I
ultimately want to store my user-defined object in a session later on.

Many thanks for your time.

James.
 
G

Guest

All,

Sorry for adding a new note so soon, but i've written my own class, and
using my example of what I wanted to do, I can achieve this! I only get a
problem when I try to store a data reader....

Is there something I am missing? Can you only store certain types of objects
in a session?

Thanks,

James.
 
B

blackstaronline.net

Its hard to determine the answer without seeing the code that creates
the first SqlReader. Can you put some more code up?

Try writing that Session string to the page with
Response.Write(Session("SqlReader")) and respond back with what it
displays.

Hope you find a solution,
Jeremy Reid
http://blackstaronline.net/hgtit
 
A

anon2005

A datareader holds an active SQL connection -- do NOT cache or
otherwise store in session state.

Instead, you can populate your own data object from the datareader, and
then cache that... or store a dataset or datatable... but not a
datareader directly. That may be part of the problem.
 
A

anon2005

..... and to answer the question :)

If you're storing, for example, a datatable that can be shared amongst
multiple users, you'd want to use the ASP.NET Cache, but if it's user
specific, use Session.

When you put objects in Session, they get boxed to type Object, so you
need to cast them to get them out. In C#, you'd do something like:

MyDataObject obj = Session["keyname"] as MyDataObject;

if (obj == null) {
// do some handling if the object isn't there
}

Alertnatively you can just cast it directly like
MyDataOjbect obj = (MyDataObject)Session["keyname"];

Either of these is acceptable, though the second throws an exception if
the object cannot be cast.

Another approach is: if you're using server-side form, which generally
posts back to itself, you can just maintain state in ViewState.
 
S

sloan

Check my blog:
http://spaces.msn.com/sholliday/ 10/24/2005 entry

NEVER cache a sqlreader. It doens't make sense on many levels.

You can cache a dataset, but it has high overhead (esp 1.1 dataset).

You best bet is to use on the "out of the box" collections, from dot.net, or
make your own CustomCollection.
Like you could do an ArrayList of ListItems


When I have items I want cache, and seldom change, I usually write a wrapper
for it.


public ArrayList GetStateListItems (bool forceRefresh)
{
if ( null==Session["mykey"] || forceRefresh)
{
//Get a fresh IDataReader, loop over, and add items to the ArrayList
ArrayList al = new ArrayList();
while IDataReader.Read()

{
al.Add(new ListItem( IDataReader.GetInt32(0),
IDataReader.GetString(1));
}
return al;
}
else
{
return (ArrayList)Session["mykey"];
}

return null;

}

Something like that. Naturally, I'd have a instantiated IDataReader, the
code is just to show how.
I never use the syntax Session["mykey"] .. I use my wrapper object, but the
idea is the same.

1. Check the cache to see it exists.
2. If it doesn't, then create a new ArrayList, and populate it from the
IDataReader
3. If it does exist, then return the cached version.
4. Throw in a forceRefresh flag, just for kicks, in case I want to force a
refresh.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top