5

I understand that storing DataTable in session variable in asp.net is bad since it will use a lot of server's memory. What I don't understand is that then what do you do when:

  1. User comes to a page where it requires to load a DataTable object (from SQL Server).
  2. User clicks on radio button for simple event (Ex. some controls get disabled).
  3. If you don't save the DataTable object in the session, you have to load it from the SQL server again upon postback on same page instead of just fetching it from the session?

Thanks for help.

Sumo
  • 4,066
  • 23
  • 40
tony
  • 1,309
  • 3
  • 10
  • 12
  • Why not use the ASP.NET Control's ViewState to retain the values on postback? This is the standard way and would cause less performance problems than storing in Session(with many users) or using the ViewState to store the complete DataTable. – Tim Schmelter Jul 04 '11 at 14:57
  • Just recently asked some questions and forgort to accept the answer. – tony Jul 04 '11 at 15:08

2 Answers2

7

DataTable's are pretty heavy objects and are not recommended to be stored in ViewState or Session for that matter. The scenario you describe is about caching data. So, why not use ASP.NET's cache?

ViewState, while it does not use as much memory on the server as Session or Cache, still requires serialization/deserialization on the server, requiring some temporary memory usage, in addition to providing your users a large payload of data on each request to/from the server (just take a peek at View Source in any browser and you'll see a very large hidden input with base-64 encoded data). If you don't use encryption, anyone can decode that data being delivered in each request, causing a potential security problem if any of that data is sensitive. ViewState is also meant for small amounts of data and is usually best to stick to the primary data types like ints and strings.

Session generally isn't a good idea either as it also requires serialization/deserialization, which adds additional overhead in addition to the strain on memory per user. Session has memory limits that decrease per user as you increase concurrent users. Session data does not "expire" until the actual session expires for each user, which by default is 30 minutes. Session is great for user-specific data, but is recommended to keep very small and again stick to the primary data types like ints and strings.

Cache does not serialize any data and is limited in size only due to the bitness of the OS. On Windows 2003 32-bit, you have 800 MB total application pool size to work with (1.2 or 1.3 GB if you use the /3GB switch). Under 64-bit, there's much more freedom and limitations are realistically only what you configure up to the amount of available system memory. A benefit of cache is that as memory pressure increases, cache can be expired to free memory for more important things. You also have control as to when items get expired when memory pressure isn't a factor (expiry's are not guaranteed). Take an additional step and you can put a cache dependency on data in the database, if using SQL Server, allowing the data itself to decide when to expire your cache, ensuring fresh data.

Lastly, the often forgotten about Application object can be used, but only for data that you know can be shared across users and does not need to change that often (hopefully not until an application restart).

Use Microsoft's documentation for ViewState, Session, Cache, and Application objects, to determine the wisest use of each for your particular scenario. A combination of using these correctly in addition to using AJAX (to avoid full page postbacks) and HTTP compression to reduce the payload delivered to the client can make for a very responsive site.

In more complex scenarios like Web farms and load balancing, there are additional issues to think about. Questions you will need to ask yourself will be things like: Should a new session be created if a user hits a different server than the originally requested one? Should cache work no matter what server a user hits? These questions will bring you to solutions that may change where you store data. InProc Session is more forgiving than using, say SQL Server, as a session server, as there are additional serialization restrictions.

Community
  • 1
  • 1
Sumo
  • 4,066
  • 23
  • 40
1

Another way to store the DataTable, if you only want to use it at page level, is in ViewState. ViewState["dtbl"] = DataTable;

And you can access it from the ViewState Simply DataTable dtbl = (DataTable)ViewState["dtbl"];

Off The Gold
  • 1,228
  • 15
  • 28
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • 7
    Storing a hole DataTable in ViewState normally(in an intranet environment) would cause much more performance problems than storing in Session. – Tim Schmelter Jul 04 '11 at 14:56
  • 1
    But this will not store into Server memory, It depends on situation when to store and when not to. – Muhammad Akhtar Jul 04 '11 at 14:59
  • 1
    but instead of storing the whole DataTable in ViewState i would prefer to use the Control's ViewState(f.e. GridView). – Tim Schmelter Jul 04 '11 at 15:01
  • 1
    No, I think he have different scnario – Muhammad Akhtar Jul 04 '11 at 15:02
  • 4
    Someone at my work thought it was a good idea to store a DataTable in ViewState...then I pointed out the size of the aspx page was 17MB... – Jamie Carruthers Jun 13 '12 at 19:50
  • The ViewState works well for "reasonable sized loads" (depends on connection and definition of "reasonable sized loads"). However, the DT *must* be given a `Name` or the serialization will error. I use a VS/DT only for when I *need* transient per-user/page data. Often times it is sufficient just to re-bind to the SQL (and write better queries ;-) –  Jul 01 '12 at 23:15
  • Oh, and FWIW, SharePoint, Microsoft's Premier ASP.NET-WebControl-Based All-In-One-CMS **does not have SessionState enabled by default**. –  Jul 01 '12 at 23:16