Link to home
Start Free TrialLog in
Avatar of DJDoug
DJDougFlag for United States of America

asked on

Prevent Form Data Loss in ASP.NET on Application Error

Hi.

I was wondering what is the best (and most secure) way to prevent a user from losing all of their entered form data if an application error (unhandled exception) occurs when they submit the form on an ASP.Net page.  I try to handle errors as much as possible in the page, but sometimes there are a few bugs I didn't catch or a server error outside my control.  I am using custom error pages, and if an unhandled exception occurs, the user loses all data entered on a form (even if they use the browsers back button).

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of naveenkohli
naveenkohli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DJDoug

ASKER

If I am understanding correctly:

OnLoad --> Store Data in Session using state engine or external DB support.
OnPreRender --> Clear session.

So, on the OnLoad event, I would first check to see if the session had been cleared before storing the data.  If it had not been cleared, then I would load the data from the Session.

OK, what happens if the User closes the browser?  (Such as in the case the error is not immediately fixed.  Some forms are quite long.)  Could I apply this same concept by writing to an encrypted cookie?  Or, if the web form is for an authenticated user, I guess I could write to a temp db based on the user?

I'll increase the points for a good code sample (prefer VB.Net) or article.
Avatar of naveenkohli
naveenkohli

The same concept should still work...
If user is moving from page to page on your system, then you can add logic to each page and making sure that it first clears the previous session variables and then starting the new save.
If you use Response.Redirect to go to new page, then catch ThreadAbortException throw by Redirect method. This exception is not an error. It is framework's way of indicating that it is aborting the current thread.

So if user closed the browser, that means they never wen to next page. So state will exist in DB (session). So you can load it when they come back.