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

Help with transferring Session object between pages.

I have multi-page form where I pass an object between pages as follows:

On each page:

// What page to go to next
private void btnContinue_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
{
MyXferObj obj = new MyXferObj();
obj.dataitemX = somevalue;
obj.dataitemY = somevalue;

Session["MYXFEROBJECT"] = obj;
Server.Transfer("nextwebpage.aspx");
}
}

// Re-populate the page controls, handles "back" button and postbacks
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["MYXFEROBJECT"] != null)
RehydrateObj();
}
}

// Re-constitute the "pass-along" object and use the data fields required
for this page
private void RehydrateObj()
{
// cast the stored obj to MyXferObj type
MyXferObj obj = Session["MYXFEROBJECT"] ;

... Repopulate the page controls with the values from "obj"
}
My questions are:
1) I'm concerned about memory leaks - do I need to "release", set to null
the MyXferObj somewhere?
At each page, my Session["MYXFEROBJECT"] would be set to the "latest"
incarnation of a new MyXferObj - do I
need to free the "old" one somehow (if so, how?) ?

2) Is there a more preferred way to pass information between pages? I
prefer to have a single "data-struct" object passed if
possible, rather then creating an entry in the Session object for each
data item.

Nov 19 '05 #1
3 1648
If you are using InProc Session state then you could clear it in the
Session_End event.
Yes, You could create something like a custom data Structure and store this
in the session state instead of storing each individual value.

If you are not using Dynamic compilation for pages, and using
Server.Transfer, you could use Context.handler (cast this to page1) and
access the DataStructure stored in Page1.
--
Aarthi R S

"ESmith" wrote:
I have multi-page form where I pass an object between pages as follows:

On each page:

// What page to go to next
private void btnContinue_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
{
MyXferObj obj = new MyXferObj();
obj.dataitemX = somevalue;
obj.dataitemY = somevalue;

Session["MYXFEROBJECT"] = obj;
Server.Transfer("nextwebpage.aspx");
}
}

// Re-populate the page controls, handles "back" button and postbacks
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["MYXFEROBJECT"] != null)
RehydrateObj();
}
}

// Re-constitute the "pass-along" object and use the data fields required
for this page
private void RehydrateObj()
{
// cast the stored obj to MyXferObj type
MyXferObj obj = Session["MYXFEROBJECT"] ;

... Repopulate the page controls with the values from "obj"
}
My questions are:
1) I'm concerned about memory leaks - do I need to "release", set to null
the MyXferObj somewhere?
At each page, my Session["MYXFEROBJECT"] would be set to the "latest"
incarnation of a new MyXferObj - do I
need to free the "old" one somehow (if so, how?) ?

2) Is there a more preferred way to pass information between pages? I
prefer to have a single "data-struct" object passed if
possible, rather then creating an entry in the Session object for each
data item.


Nov 19 '05 #2
ESmith wrote:
I have multi-page form where I pass an object between pages as
follows:

On each page:

// What page to go to next
private void btnContinue_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
{
MyXferObj obj = new MyXferObj();
obj.dataitemX = somevalue;
obj.dataitemY = somevalue;

Session["MYXFEROBJECT"] = obj;
Server.Transfer("nextwebpage.aspx");
}
}

// Re-populate the page controls, handles "back" button and postbacks
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["MYXFEROBJECT"] != null)
RehydrateObj();
}
}

// Re-constitute the "pass-along" object and use the data fields
required for this page private void RehydrateObj()
{
// cast the stored obj to MyXferObj type
MyXferObj obj = Session["MYXFEROBJECT"] ;

... Repopulate the page controls with the values from "obj"
}
My questions are:
1) I'm concerned about memory leaks - do I need to "release", set to
null the MyXferObj somewhere? At each page, my
Session["MYXFEROBJECT"] would be set to the "latest" incarnation of a
new MyXferObj - do I need to free the "old" one somehow (if so,
how?) ?

I beleive this is cleared out for you when the session expires.. it is
essentially disposed.
2) Is there a more preferred way to pass information between pages?
I prefer to have a single "data-struct" object passed if possible,
rather then creating an entry in the Session object for each data
item.

Yeah.. a very common method is to create a struct for your data and
store that in the Session. you can then retrieve and update it as you
go along... Or use a hashtable which will let you store any number of
objects. This is the most extensible but the downside is that it is not
essentially strongly typed, so that it relies on you to know what you
have stored under what key.

================================================== ==================

There are all sorts of other ways to store data. If you wish to get a
bit of better performance you could disable Session state and instead
store things in the ViewState.. you can do this as long as the objects
you store are Serializable.

Or you could put it into some hidden field on your form (which is
essentially the old school way of doing things and this is what the
viewstate is doing anyway..)

It all depends on how much data you have, how often it changes and how
long you wish it to be available for.

Ahh.. decisions, decisions.

Dirc

--

Nov 19 '05 #3
Okay, you're using Server.Transfer, so you're passing the entire HttpContext
when you pass the execution to the second page. Therefore, there is no need
to store anything in Session, or to do anything but expose the data via a
public field, method, or property in the first page. Example (for second
page code):

Page1ClassName firstPage = (Page1ClassName)Context.Handler;
// Get whatever you need from the first page now, as "firstPage"
1) I'm concerned about memory leaks - do I need to "release", set to null
the MyXferObj somewhere?
Setting a variable to null does nothing to release memory. As soon as the
thread which owns an object is removed from the stack (goes out of scope),
the object is available for Garbage Collection. If an object implements
IDisposable, it should be disposed. Otherwise, you can count on Garbage
Collection to manage memory for you.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"ESmith" <el**********@hotmail.com> wrote in message
news:uX****************@TK2MSFTNGP14.phx.gbl...I have multi-page form where I pass an object between pages as follows:

On each page:

// What page to go to next
private void btnContinue_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
{
MyXferObj obj = new MyXferObj();
obj.dataitemX = somevalue;
obj.dataitemY = somevalue;

Session["MYXFEROBJECT"] = obj;
Server.Transfer("nextwebpage.aspx");
}
}

// Re-populate the page controls, handles "back" button and postbacks
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["MYXFEROBJECT"] != null)
RehydrateObj();
}
}

// Re-constitute the "pass-along" object and use the data fields required
for this page
private void RehydrateObj()
{
// cast the stored obj to MyXferObj type
MyXferObj obj = Session["MYXFEROBJECT"] ;

... Repopulate the page controls with the values from "obj"
}
My questions are:
1) I'm concerned about memory leaks - do I need to "release", set to null
the MyXferObj somewhere?
At each page, my Session["MYXFEROBJECT"] would be set to the "latest"
incarnation of a new MyXferObj - do I
need to free the "old" one somehow (if so, how?) ?

2) Is there a more preferred way to pass information between pages? I
prefer to have a single "data-struct" object passed if
possible, rather then creating an entry in the Session object for each
data item.


Nov 19 '05 #4

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

Similar topics

1
by: r_burgess | last post by:
I am looking for some guidance on transferring data between two pages in my ASP.net Web app (intranet). I have a form that will have a button and a text box on it (among other controls of course)...
2
by: Boban Dragojlovic | last post by:
I'm building a complex web-based reservations system. Gathering the user's data requires between 8 and 15 pages (depending on which options they are interested in). I use the "Session" object to...
11
by: Vishal | last post by:
Hello, can anybody tell me how I can extend the session expiry time? Is it done via code or via IIS? Sorry I am new and dont know about this.
4
by: abcd | last post by:
I have an ASP application. It instantiaties some COM components and we put those COM components in Session variables...COM components have license restrictions...We have written new ASPX page ( for...
4
by: Michael Kujawa | last post by:
Hi all, If anyone has experience with Dynu FTP object maybe you could help me out. Hopefully it is a simple solution. I "have" referred to the component documentation and I am still having...
1
by: Mike Hofer | last post by:
I really need some help, and I'd appreciate any that you folks can provide. The ASP.NET application in question uses version 1.1 of the .NET Framework. All of the pages use a common base class...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
4
by: Don Miller | last post by:
I am using a Session variable to hold a class object between ASP.NET pages (in VB). In my constructor I check to see if the Session variable exists, and if it doesn't, I create one and populate it...
3
by: janetopps | last post by:
I have a news website, with asp pages, which was on Access, and i upgraded to MySQL, i used Bullzip to transfer the data. It had about 1000 pages, which im now able to pull up on the public side. Im...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.