473,395 Members | 1,504 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,395 software developers and data experts.

public variable problem

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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
9 1915
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
"Steven Blair" <st**********@btinternet.com> schreef in bericht
news:uB**************@TK2MSFTNGP15.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #2
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?

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Steven Blair" <st**********@btinternet.com> wrote in message
news:uB**************@TK2MSFTNGP15.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #4
Hi,

"Steven Blair" <st**********@btinternet.com> wrote in message
news:ej****************@TK2MSFTNGP14.phx.gbl...
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nov 17 '05 #5
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
"Steven Blair" wrote:
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?

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #6
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
*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #7
"Steven Blair" <st**********@btinternet.com> wrote in message
news:un**************@TK2MSFTNGP10.phx.gbl...
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!
Nov 17 '05 #8
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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #9
"Steven Blair" <st**********@btinternet.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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!
Nov 17 '05 #10

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

Similar topics

9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
10
by: Zap | last post by:
Widespread opinion is that public data members are evil, because if you have to change the way the data is stored in your class you have to break the code accessing it, etc. After reading this...
5
by: MMSJED | last post by:
I am beginner in using C#, actually I am trying to move from VB6 to C# I need very small help in programming problem my be you will laugh when you get it That simply I have to form let’s say...
2
by: THY | last post by:
Hi, I am having some problem, I declare few variable in a public module and use them in the web application. But after that I found that the variable declared in public variable =...
2
by: Ekul | last post by:
I have an application that allows users to login and logout. I track how many users are logged in and when each individual is logged in. The application will not allow concurrent logins(let a...
3
by: Joe Fallon | last post by:
I have a Shared varibale in a base class and all the Shared methods in the sub-classes use it (and CHANGE it). I thought I was saving myself the "trouble" of Dimming this variable inside each...
8
by: Floris van Haaster | last post by:
Hi All! I have a question, i have a web application and I store some member information in a variable i declared in a module like: Public some_info_variable as string in module1.vb But...
4
by: ST | last post by:
From Form1 I call a public sub in a module. The sub writes on Form1. Problem: the call creates a new Form1 and writes on it, instead of writing on the one already shown Form1. I have tried to...
8
by: rk2008 | last post by:
I have an ASP.NET application, which needs to initialize a memory location and update it multiple times in different projects in the same solution. I have used a public static variable and I could...
6
by: felicia | last post by:
Hi, I am having problem to retrieve the data value of a public variable. Below is my code: Form1 Option Explicit Public selected_entry_id As String Private Sub btnEdit_Click() ...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.