Search code examples
c#asp.netdatatableviewstatestate-management

Is assigning DataTable to ViewState a good way to persist data?


I'm getting a DataTable from a database and assigning to ViewState like below because I don't want to hit my database every time and just want to check a DataTable for subsequent look ups.

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

GetDataTable() is a method which retrieves 1000 records from the database.

Is this the best way or is there a better way to handle this?


Solution

  • There are several options. Which one is best to use depends a lot on your requirements, environments settings, etc.

    Viewstate:

    The viewstate is stored in a hidden field rendered as an <input /> tag in the final HTML sent to the browser. When the user initiates a postback (via button click, etc.), the data is sent back to the server as part of the submitted form data.

    If you store large amounts of data in the ViewState, you will have to incur a penalty when the user attempts to download the page because all that data will be part of your HTML and also when the user attempts to submit the form because again this data will be sent back to server.

    In addition, the ViewState is easily lost. It is only preserved as long as the user is submitting the form. If the user clicks a hyperlink to another page, the form is not submitted and therefore data contained within the ViewState is lost.

    It is recommended to use the ViewState if the data is relatively small.

    If we consider security options, ViewState data is encoded in base64, which can be easily decoded. This is a classic example for hacking a website, so crosscheck what data exactly you are storing. You can overcome this issue by setting EnableViewStateMac to true.


    Session

    For large amounts of data, Session is a good option. If you are able to detect when any user is done with a particular block of data, set the Session variable to null, to counter memory overhead issues. If you can't always do this, the Session will also expire, and the memory will be reclaimed automatically. Lowering the Session timeout can also help, but take care in setting it to a value that interferes with normal site usage.

    Also, the data in the Session is actually present on the web server between page loads. This helps in keeping the page size small; it only has to utilize the Session ID.


    Caching

    One last option is to use caching. Check the Microsoft Learn article ASP.NET Caching: Techniques and Best Practices by Steven A. Smith for more information.