ViewState and DataTable

  • Thread starter Thread starter Peter Hemmingsen
  • Start date Start date
P

Peter Hemmingsen

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
 
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:

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
 
Back
Top