Tuesday, January 1, 2008

ASP.NET - Store ViewState in Database using VB.NET

We had finally crossed the barrier of ViewState performance and were watching our application performance drop signficantly. I did the usual websearches and arrived at two solutions: 1) start changing major bits of code to not use ViewState and/or 2) store ViewState in the database.

This page made it seem easy however the one issue was that we're using VB.NET and not C#. From some additional searching it appeared that no one had made the change to store it in the database using VB.NET so I decided to go after it. It really didn't take long to recode in VB.NET however I know there are a few issues with the code (i.e. it is ugly and uncommented) however I wanted to get something out there in the event it helps someone out sooner than waiting and not getting it posted for another month or two.

SqlViewState Class
Database Code

To use just change your code to inherit from the class instead of the System.Web.UI.Page. The performance difference in our case was significant. We know we need to move to AJAX however when we started the AJAX stuff was not mature enough for our use so this solution is easy to implement and we saw a 5% to 30% increase in performance. If you look at the source of the page you'll see the following:

<input type="hidden" name="__VIEWSTATEGUID" value="130adb08-0644-470a-9c3b-0cc630defd32" />
<input type="hidden" name="__VIEWSTATE" value="" />