Asp.Net : Storing a DataTable between PostBacks

P1ST0LPETE used Ask the Experts™
Hi Experts,

A web page I'm working on contains a custom data grid that I have made.  When the page first loads I am making a trip to the database, loading data into a DataTable, and then populating the custom data grid from the DataTable.  The users are allowed to make changes to the data grid, and as they do so I am storing the changes by updating the DataTable.  Only when the user is completely done making changes and clicks a "Save" button, does the data from the DataTable get pushed back to the database.

So, with that in mind, when changes are made to the data grid that require a postback, (i.e. adding/removing rows which are firing an asynchronous postback), where can I store the DataTable in between postbacks?

Originally, I had it setup where a static class was holding the DataTable, but then I learned that static classes are basically shared between all sessions of a website, and so the results would not be satisfactory.  Also, I know I could store the DataTable in a session variable, but I am reading more and more these days that session variables should be avoided at all costs, especially when talking about ADO objects.

So what is the best method for storing/passing a DataTable between postbacks?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Developer User Interface
If u must absolutely save datatable across postbacks u can save it in ViewState variable.

But then that will make the page more and more heavy.

ViewState["dt"] = datatable;

DataTable dt = (DataTable)ViewState["dt"];


What exactly is the difference between storing it in the ViewState vs. storing it in a Session variable?

Also, why do you say "If you must absolutely save datatable across postbacks"?  Is saving a DataTable/DataSet between postbacks frowned upon?


Never mind, googled it, and discovered the difference.  ViewState definitely seems the way to go when storing data during postbacks to the same page, as the data is held with in an encrypted html string on the page, instead of being held in the web server's memory like session variables are.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial