Session variables and reference

Discussion in 'ASP .Net' started by Vince13 via DotNetMonster.com, Jul 19, 2007.

  1. 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 = new cut(((cut[])Session["allCuts"]));
    //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
    Vince13 via DotNetMonster.com, Jul 19, 2007
    #1
    1. Advertising

  2. 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@uwe> wrote 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 = new cut(((cut[])Session["allCuts"]));
    > //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
    >
    Cowboy \(Gregory A. Beamer\), Jul 19, 2007
    #2
    1. Advertising

  3. 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 = new cut(((cut[])Session["allCuts"]));
    > //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
    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jul 19, 2007
    #3
  4. Vince13 via DotNetMonster.com

    sloan Guest

    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@uwe> wrote 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 = new cut(((cut[])Session["allCuts"]));
    > //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
    >
    sloan, Jul 19, 2007
    #4
  5. 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
    Vince13 via DotNetMonster.com, Jul 19, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Earl Teigrob

    Global Session Variables and Session State

    Earl Teigrob, Dec 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    4,272
    Alvin Bruney
    Dec 17, 2003
  2. Replies:
    1
    Views:
    933
  3. =?Utf-8?B?Um9iSEs=?=
    Replies:
    4
    Views:
    5,275
    =?Utf-8?B?Um9iSEs=?=
    Apr 11, 2007
  4. Replies:
    9
    Views:
    942
  5. Jazzis
    Replies:
    2
    Views:
    244
    Jazzis
    Sep 23, 2003
Loading...

Share This Page