473,402 Members | 2,072 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,402 software developers and data experts.

Session variables and reference

I am trying to set up a page where the user can change data, and then click
cancel and the data will not be saved. Currently, I am saving the data in a
Session variable that is an array of a class I created:

public class cut
{
public cut()
{
ParallelDim = 'G';
cutPoint = ++numCuts;
Knom = 0;
Kmin = 0;
Kmax = 0;
//numCuts++;
}
public cut(cut myCut) //copy constructor
//**note, does NOT increment numCuts
{
ParallelDim = myCut.ParallelDim;
cutPoint = myCut.cutPoint;
Knom = myCut.Knom;
Kmin = myCut.Kmin;
Kmax = myCut.Kmax;
}

public char ParallelDim;
public int cutPoint;
public decimal Knom;
public decimal Kmin;
public decimal Kmax;
public static int numCuts = 0;
}

I realized that when I set my temporary array to equal the session array, it
was passing the values by reference, so I created new objects using a copy
constructor:

for(int i = 0; i < 20; ++i) //uses copy constructor to initiate myCuts and
make copy so the
{ //changes are not made to Session[allcuts]
myCuts[i] = new cut(((cut[])Session["allCuts"])[i]);
//uses copy constructor to make
identical array
}

This works in that it does create a new array that is not just a reference to
the old one, but for some reason I cannot write the data back to the session
array. I am fairly sure it should just work like this:

Session["allCuts"] = myCuts;

but I have also tried using the copy constructor again (reverse of above) and
setting each individual value of the class separately. Nothing will write
the data back to the session array.

If ANYONE has any ideas, I would greatly appriciate it.

--
Message posted via http://www.dotnetmonster.com

Jul 19 '07 #1
4 2316
If you are going the session route, I find it easier to store ina data
format, like a DataSet. This is not applicable for all.

If you are using a single page for the info, consider ViewState instead. You
are still better to control your own destiny, of course.

I have never had problems with objects in Session, but I am not extensive in
my use of Session either, as I can normally find a better way for my
applications.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
Co-author: Microsoft Expression Web Bible (upcoming)

************************************************
Think outside the box!
************************************************
"Vince13 via DotNetMonster.com" <u35350@uwewrote in message
news:75692e15875a8@uwe...
>I am trying to set up a page where the user can change data, and then click
cancel and the data will not be saved. Currently, I am saving the data in
a
Session variable that is an array of a class I created:

public class cut
{
public cut()
{
ParallelDim = 'G';
cutPoint = ++numCuts;
Knom = 0;
Kmin = 0;
Kmax = 0;
//numCuts++;
}
public cut(cut myCut) //copy constructor
//**note, does NOT increment numCuts
{
ParallelDim = myCut.ParallelDim;
cutPoint = myCut.cutPoint;
Knom = myCut.Knom;
Kmin = myCut.Kmin;
Kmax = myCut.Kmax;
}

public char ParallelDim;
public int cutPoint;
public decimal Knom;
public decimal Kmin;
public decimal Kmax;
public static int numCuts = 0;
}

I realized that when I set my temporary array to equal the session array,
it
was passing the values by reference, so I created new objects using a copy
constructor:

for(int i = 0; i < 20; ++i) //uses copy constructor to initiate myCuts and
make copy so the
{ //changes are not made to Session[allcuts]
myCuts[i] = new cut(((cut[])Session["allCuts"])[i]);
//uses copy constructor to make
identical array
}

This works in that it does create a new array that is not just a reference
to
the old one, but for some reason I cannot write the data back to the
session
array. I am fairly sure it should just work like this:

Session["allCuts"] = myCuts;

but I have also tried using the copy constructor again (reverse of above)
and
setting each individual value of the class separately. Nothing will write
the data back to the session array.

If ANYONE has any ideas, I would greatly appriciate it.

--
Message posted via http://www.dotnetmonster.com

Jul 19 '07 #2
Vince13,

drop a Gridview on an ASP.NET page and put the following code in. This
should help explain what's happening with Session:

protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Test");
DataRow row = dt.NewRow();
row["Test"] = "First";
dt.Rows.Add(row);
Session["dt"]=dt;
dt.Rows[0]["Test"] = "Second";
GridView1.DataSource = (DataTable) Session["dt"];
GridView1.DataBind();
}

-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
bogMetaFinder: http://www.blogmetafinder.com

"Vince13 via DotNetMonster.com" wrote:
I am trying to set up a page where the user can change data, and then click
cancel and the data will not be saved. Currently, I am saving the data in a
Session variable that is an array of a class I created:

public class cut
{
public cut()
{
ParallelDim = 'G';
cutPoint = ++numCuts;
Knom = 0;
Kmin = 0;
Kmax = 0;
//numCuts++;
}
public cut(cut myCut) //copy constructor
//**note, does NOT increment numCuts
{
ParallelDim = myCut.ParallelDim;
cutPoint = myCut.cutPoint;
Knom = myCut.Knom;
Kmin = myCut.Kmin;
Kmax = myCut.Kmax;
}

public char ParallelDim;
public int cutPoint;
public decimal Knom;
public decimal Kmin;
public decimal Kmax;
public static int numCuts = 0;
}

I realized that when I set my temporary array to equal the session array, it
was passing the values by reference, so I created new objects using a copy
constructor:

for(int i = 0; i < 20; ++i) //uses copy constructor to initiate myCuts and
make copy so the
{ //changes are not made to Session[allcuts]
myCuts[i] = new cut(((cut[])Session["allCuts"])[i]);
//uses copy constructor to make
identical array
}

This works in that it does create a new array that is not just a reference to
the old one, but for some reason I cannot write the data back to the session
array. I am fairly sure it should just work like this:

Session["allCuts"] = myCuts;

but I have also tried using the copy constructor again (reverse of above) and
setting each individual value of the class separately. Nothing will write
the data back to the session array.

If ANYONE has any ideas, I would greatly appriciate it.

--
Message posted via http://www.dotnetmonster.com

Jul 19 '07 #3
You can check out this article:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!151.entry

There is a 1.1 version at my blog as well.

I find it easier to store 1 (wrapper type object) instead of a bunch of
disconnected items.


"Vince13 via DotNetMonster.com" <u35350@uwewrote in message
news:75692e15875a8@uwe...
>I am trying to set up a page where the user can change data, and then click
cancel and the data will not be saved. Currently, I am saving the data in
a
Session variable that is an array of a class I created:

public class cut
{
public cut()
{
ParallelDim = 'G';
cutPoint = ++numCuts;
Knom = 0;
Kmin = 0;
Kmax = 0;
//numCuts++;
}
public cut(cut myCut) //copy constructor
//**note, does NOT increment numCuts
{
ParallelDim = myCut.ParallelDim;
cutPoint = myCut.cutPoint;
Knom = myCut.Knom;
Kmin = myCut.Kmin;
Kmax = myCut.Kmax;
}

public char ParallelDim;
public int cutPoint;
public decimal Knom;
public decimal Kmin;
public decimal Kmax;
public static int numCuts = 0;
}

I realized that when I set my temporary array to equal the session array,
it
was passing the values by reference, so I created new objects using a copy
constructor:

for(int i = 0; i < 20; ++i) //uses copy constructor to initiate myCuts and
make copy so the
{ //changes are not made to Session[allcuts]
myCuts[i] = new cut(((cut[])Session["allCuts"])[i]);
//uses copy constructor to make
identical array
}

This works in that it does create a new array that is not just a reference
to
the old one, but for some reason I cannot write the data back to the
session
array. I am fairly sure it should just work like this:

Session["allCuts"] = myCuts;

but I have also tried using the copy constructor again (reverse of above)
and
setting each individual value of the class separately. Nothing will write
the data back to the session array.

If ANYONE has any ideas, I would greatly appriciate it.

--
Message posted via http://www.dotnetmonster.com

Jul 19 '07 #4
I see what you're pointing out here, that the assignment operator merely
creates another reference to the same memory, but that's why I'm using the
copy contructor in the first place. That's also the part that works. What
I'm wondering is if the same issue (reference/value) is somehow not allowing
me to wirte back to the session variable.

Peter Bromberg [C# MVP] wrote:
>Vince13,

drop a Gridview on an ASP.NET page and put the following code in. This
should help explain what's happening with Session:

protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Test");
DataRow row = dt.NewRow();
row["Test"] = "First";
dt.Rows.Add(row);
Session["dt"]=dt;
dt.Rows[0]["Test"] = "Second";
GridView1.DataSource = (DataTable) Session["dt"];
GridView1.DataBind();
}

-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
bogMetaFinder: http://www.blogmetafinder.com
>I am trying to set up a page where the user can change data, and then click
cancel and the data will not be saved. Currently, I am saving the data in a
[quoted text clipped - 54 lines]
>>
If ANYONE has any ideas, I would greatly appriciate it.
--
Message posted via http://www.dotnetmonster.com

Jul 19 '07 #5

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

Similar topics

14
by: Paul Yanzick | last post by:
Hello, I am trying to develop a book tracking application for my capstone in school, and am running into a problem. The application is an ASP.Net application written in C#. The first page you...
9
by: Greg Linwood | last post by:
I'm having difficulty understanding Session state in ASP.Net. It's almost embarrassing asking this as I've been using ASP since it was first released & it really shouldn't be this hard to use -...
5
by: Oleg Ogurok | last post by:
Hi all, Is there a way to read other people's session variables? I understand it makes sense that session state is on per-user basis, but still... Is there a way to get a collection of all...
9
by: Randy | last post by:
Hello, I'm having a strange problem. I've got a .NET web app which uses Session variables. Sometime, not all the time, they get cross threaded...that is...one user will have another user's Session...
12
by: Alex D. | last post by:
is it possible? pros and cons?
13
by: | last post by:
Simple question, I think... I'm storing an object in the Session object. In the code behind I read that object: trx = CType(Session("Transaction"), BOCSTransaction) If I change any...
1
by: Santosh | last post by:
Dear All i am writting a code sending mail with attachement. i am writting code for sending mail in one page and code for attaching a file in the next page. aftet attaching a file i am taking...
18
by: BillE | last post by:
When a user opens a new IE browser window using File-New-Window the integrity of an application which relies on session state is COMPLETELY undermined. Anyone who overlooks the fact that...
4
by: Diffident | last post by:
Hi All, I am trying to perform a non-CPU bound operation in an asynchronous fashion using 'Thread' and a delegate. Basically, I am spawning a thread using ThreadStart and Thread. My non-CPU...
6
by: ChrisAtWokingham | last post by:
I have been struggling with unexpected error messages on an ASP.NET system, using SQL and C#. The application draws organisation charts, based on data stored in the SQL database. Some of the chart...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.