I have in the past always had quite a few problems maintaining the session state with WebMatrix, and handling them when expecting them to be timed-out (mainly because I am still learning).
The user input must always be remembered, because if they submit and a custom error occurs, I don't want them to have to re-input all of the information back into the form.
I think I have a method figured out (finally) that can properly maintain and manage timed-out session variables in the future, when they are needed.
I realize that there are several other methods of transferring data between web pages, and believe me, when applicable, they will certainly be used first. This question only pertains to when I feel like the session variable is my best option.
My problem is, I always think I have it figured out, and then, only after I have set up over half of the coding, do I realize why it doesn't quite work, and then find some sloppy (but effective) work around that will at least work for that project.
The projects I will use session variables in use the common database with web interface combination. Usually there are 3 main pages: an add entry page, an edit entry page, and a lookup entry page. The add entry page and the lookup entry page eventually post and redirect to the edit entry page.
Before I begin my next project, I thought it would be wise to inquire if my method is at least aiming in the right direction, or if I am still not approaching this quite right.
Here is what I think might work (although, it seems a bit too easy):
In other words: I will (when necessary) use the session variables only to transfer the data which will happen in a completely acceptable amount of time (not allowing the session state enough time to timeout), and then referring only to the local variables when needed, per page.
Am I crazy or is this the best way (or at least a decently viable way) to handle this, when forced to work with the session state in WebMatrix?
Sorry if this is a stupid question xD
In my opinion, you should not use Session variables to remember what the user entered into the form. The form - add or edit - should post to itself for validation. That way, the values are available in the Request.Form collection. You can easily repopulate the form in the event of a validation error. On successful submission, you insert or update the database, and then redirect to another page to prevent duplicate submission.
Session variables are really easy to use if used appropriately. I suspect your difficulties arise because you are using them for the wrong scenarios.