public variable problem

  • Thread starter Thread starter Steven Blair
  • Start date Start date
S

Steven Blair

Hi,

Here is my problem:

This is using Visual Studio 2005

I have a DataSet declared as a public member:

public DataSet tmp

In my Page_Load I populate the DataSet.

Anytime I try to access this DataSet outside the Page_Load, e.g

Wizard1_FinishButtonClick

The DataSet is null.

Any idea why this happens?

Regards,

Steven
 
Hi Steven,

Do you have by any chance used the 'not is postback' in your Page_Load ?

if (!IsPostback)
{
//fill of your dataset
}

If so, then your dataset will only be filled the first time, and not on any
postback.

HTH
Christiaan
 
Yeah my DataSet is initially populated inside

if (!IsPostBack)
{
//Fill DataSet
}

I only ever want it filled once, thats why its in here.
I am using a Wizard component so Page_Load gets called each time user
changes a page.
Surely only one instance of MyForm class exists, and after every call of
Page_Load, does this mean a new instance exists?

I tried declaring my DataSet as static, and that allows me to access the
data, but to me that makes no sense.
Can you explain in some detail whats happening here?
 
Hi,

Post the code for the Page_Load event, I bet you are declaring another
variable with the same name and it takes precedence.

cheers,
 
Hi,



Steven Blair said:
Yeah my DataSet is initially populated inside

if (!IsPostBack)
{
//Fill DataSet
}

Then how it's referenced again, in the postback? what the "else" clause
looks like?
Are you storing it in session ?

Surely only one instance of MyForm class exists, and after every call of
Page_Load, does this mean a new instance exists?

Each request to the page create a new instance, this instance is disposed
when tha page is sent to the client.
I tried declaring my DataSet as static, and that allows me to access the
data, but to me that makes no sense.

If you do so you will be sharing that dataset at Application level, meaning
that all the sessions in your app will read the same dataset, no idea if
this is what you want, but most probably is not/


Solution: Store the Dataset in Session :

if (!IsPostBack)
{
//Fill DataSet
Session["MyDataset"] = myDataset;
}
else
myDataset = (DataSet) Session["MyDataset"] ;


cheers,
 
Ahh, that is the problem then. ASP.NET pages are created when a page is
requested and are thrown out when the request is complete, rather than have a
single instance stay in memory for all requests.

In order to save the dataset, you will need to cache it in order to maintain
it between requests.

The big question is do you want this dataset accessible by all callers to
the page, or a separate instance for each individual user?

If the first, the quickest way to do it is with the Application state, if
the second, the Session state is your best bet.

Inside of your Form_Load event handler, after you have created your dataset
and set it equal to your public variable, you would want to do the following:

Session["mydataset")=ds;

Where mydataset is a name you are giving to the dataset within the Session
state and ds is a reference to it.

Just for note, if you want to use the Application state, simply replace
Session with Application.

That stores the dataset, now, in order to use it you need to retrieve it
from the store, to do so, you would simply do (inside of your Form_Load, but
when a postback:

ds = (DataSet)Session["mydataset"];

Again where ds is the name of the reference you want to the dataset and
mydataset is the same name you gave above.

Brendan
 
The DataSet is only declared as a public in ym class. But this is quite
intersesting information.
I didn't realise a new instance of the page was created each time.
For my problem, I will simply cache the DataSet in the Session.

Thanks for your help everyone.

Regards,

Steven
 
Steven Blair said:
The DataSet is only declared as a public in ym class. But this is quite
intersesting information.
I didn't realise a new instance of the page was created each time.
For my problem, I will simply cache the DataSet in the Session.

Thanks for your help everyone.

It might be worth noting, that sessions last for around 20 mins by default,
so if your site is doing this for every visitor, you'll use a lot more
memory than if the datasets just lasted for the duration of the request!
 
Well its a Wizard I am using so should I do remove the DataSet when user
leaves the page:

Something like:

Session["MyDataSet"] = null; //Is this valid?

Regards,

Steven
 
Steven Blair said:
Well its a Wizard I am using so should I do remove the DataSet when user
leaves the page:

Something like:

Session["MyDataSet"] = null; //Is this valid?

Yep, that'd work. Though if a user closes tehe wizard, it won't run. Not a
big deal though, since I imagine most users will finish it. You should be
careful if you're whacking a dataset in the session for a normal web page
(like your homepage), where most of your visitors will just navigate away!
 
Back
Top