ViewState and DataTable

Discussion in 'ASP .Net' started by Peter Hemmingsen, Oct 22, 2009.

  1. Hi

    Im storing a (very small) DataTable in ViewState as follows:

    DataTable dt = new DataTable();
    DataRow dr = dt.NewRow();
    dt.Rows.Add (dr);
    ViewState ["MyTable"]=dt;

    When a button is pressed I read the DataTable from ViewState and add a row:

    DataTable dt=ViewState["MyTable"];
    DataRow dr = dt.NewRow();
    dt.Rows.Add (dr);

    at this point I would have saved the new datatable dt in ViewState but using
    the debugger I can see that the ViewState datatable is already modified. I
    would have expected

    DataTable dt=ViewState["MyTable"];

    to make a copy but it looks more like a reference?
    I would appreciate if somebody could explain this,- thanks.

    Peter
     
    Peter Hemmingsen, Oct 22, 2009
    #1
    1. Advertising

  2. Peter Hemmingsen

    Linda Smith Guest

    I experienced the same thing. The viewstate seems to change with the datatable. Do you have a solution for this?



    Peter Hemmingsen wrote:

    ViewState and DataTable
    22-Oct-09

    H

    Im storing a (very small) DataTable in ViewState as follows

    DataTable dt = new DataTable()
    DataRow dr = dt.NewRow()
    dt.Rows.Add (dr)
    ViewState ["MyTable"]=dt

    When a button is pressed I read the DataTable from ViewState and add a row

    DataTable dt=ViewState["MyTable"]
    DataRow dr = dt.NewRow()
    dt.Rows.Add (dr)

    at this point I would have saved the new datatable dt in ViewState but usin
    the debugger I can see that the ViewState datatable is already modified.
    would have expecte

    DataTable dt=ViewState["MyTable"]

    to make a copy but it looks more like a reference
    I would appreciate if somebody could explain this,- thanks

    Peter

    Previous Posts In This Thread:

    On Thursday, October 22, 2009 11:30 AM
    Peter Hemmingsen wrote:

    ViewState and DataTable
    H

    Im storing a (very small) DataTable in ViewState as follows

    DataTable dt = new DataTable()
    DataRow dr = dt.NewRow()
    dt.Rows.Add (dr)
    ViewState ["MyTable"]=dt

    When a button is pressed I read the DataTable from ViewState and add a row

    DataTable dt=ViewState["MyTable"]
    DataRow dr = dt.NewRow()
    dt.Rows.Add (dr)

    at this point I would have saved the new datatable dt in ViewState but usin
    the debugger I can see that the ViewState datatable is already modified.
    would have expecte

    DataTable dt=ViewState["MyTable"]

    to make a copy but it looks more like a reference
    I would appreciate if somebody could explain this,- thanks

    Peter


    Submitted via EggHeadCafe - Software Developer Portal of Choice
    Preventing extra HTML Content in File Downloads
    http://www.eggheadcafe.com/tutorial...e-f3dcccf496ad/preventing-extra-html-con.aspx
     
    Linda Smith, Jan 4, 2010
    #2
    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. J. Babe
    Replies:
    1
    Views:
    8,355
    Kevin Spencer
    Aug 15, 2003
  2. Fredrik Rodin

    Sort a datatable and create a new datatable

    Fredrik Rodin, Feb 16, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    9,059
    paul.mariotti
    Sep 28, 2007
  3. =?Utf-8?B?Qmx1ZWZsb3dlcg==?=

    Casting DataTable to class inherit from DataTable

    =?Utf-8?B?Qmx1ZWZsb3dlcg==?=, Apr 2, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    999
    =?Utf-8?B?Qmx1ZWZsb3dlcg==?=
    Apr 2, 2004
  4. jg
    Replies:
    1
    Views:
    729
    =?Utf-8?B?U2hhaWxlc2ggSmFubnU=?=
    Aug 17, 2004
  5. Ersin Gençtürk
    Replies:
    1
    Views:
    3,490
    Ersin Gençtürk
    Oct 6, 2004
Loading...

Share This Page