473,378 Members | 1,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Storing an object in a session

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.

Feb 14 '06 #1
5 2984
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.

"James Vickers" wrote:
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.

Feb 14 '06 #2
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

Feb 15 '06 #3
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.

Feb 15 '06 #4
..... 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.

Feb 15 '06 #5
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.

"James Vickers" <Ja**********@discussions.microsoft.com> wrote in message
news:2B**********************************@microsof t.com...
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.

Feb 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: yop | last post by:
Hello I have an application, Login page, enter UserName & Password. Function in Users called GetUserDetails and checks the details and if they are valid calls a function to fill the following...
0
by: Sandra | last post by:
I am using a VB6 COM object with an asp.net project. I am storing the COM object in a session variable but am having a problem accessing the COM object from the session variable. I am getting...
2
by: jakk | last post by:
Below is the exception that Iam getting. It says that the DataView that Iam storing in the session is not Serializable. BUt works fine if I store in the inproc session and fails if I switch to...
12
by: Alex D. | last post by:
is it possible? pros and cons?
8
by: rgparkins | last post by:
Hi I am creating a sign-up process on a web site much like that of a wizard form. I have browsed many sites to look for examples of how to store the entry data, so that the user can go back and...
10
by: Mark Rae | last post by:
Hi, This relates to the previous thread "Disappearing Sessions", but is a bit more generic so I thought I'd start a new thread. This one relates to the storing of objects in Session once only to...
9
by: david | last post by:
I have a class with some business-logic and with every roundtrip, I need an instance of this class, so I have to create it, every time again. That doesn't seem very efficient. I thought it would...
1
by: None | last post by:
Hi, I have developed webshop application using asp.net 1.1. I'm using DataGrid in one of the pages of my site. During the page load the DataGrid will be binded by around 7500 products(rows). At...
3
by: RSH | last post by:
Hi, I have a situation where I have created an object that contains fields,properties and functions. After creating the object I attempted to assign it to a session variable so i could retrieve...
4
by: =?Utf-8?B?YmFzdWxhc3o=?= | last post by:
Hi; I want to store a datatable (or an arraylist) as a session variable but when I try; Session = al_RecNo; I get an error that; "Cannot implicitly convert type...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.