Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Passing Data Between Multiple Pages Session

Tags:

asp.net

So there seems not be any pretty answer to the question of how pass data between multiple pages. After having done a little homework here's why (or at least what I've gleaned):

  1. ViewState variables don't persist across pages.
  2. Session variables are volatile and must be used sparingly.
  3. Cookies have potential safety issues and take time and must be kept small.
  4. Storing vars in the URL has limits to the amount of data and can be unsafe.
  5. Storing vars temporarily in a db is a real pita because you add one table per object that might be potentially passed to another page.

    So far it is looking like I will be using hidden fields to pass a keyid and unique id to the next page and then retrieve the data from the db. What are your thoughts on all of this? What is the best way to go about doing any of it? I am early in the development of this app, so making changes now is preferred.

edit: I am anticipating a lot of users using this application at any one time, does that affect whether or not I should be using SQL Server based Session?
like image 957
LunchMarble Avatar asked Jun 21 '26 12:06

LunchMarble


2 Answers

If you want to persist state, yes store it in the database since you don't have to worry about an expiration. The Session is similar except you have to worry about the Session expiring. In both cases concurrent calls that write similar data to the same area could cause problems and need to be accounted for.

Session is good when you don't have to worry about multiple web servers or timeout issues. The database gives you more scalability but at a cost of doing lots of db read/writes and you have to consider clean up.

Personally I would try to use the following decision tree:

  1. Is the data simple, short and not private -> query string
  2. Is the data less simple but only needs to exist for a short time -> session
  3. Will the data be needed across multiple area and be persistent for long period of time -> database

Of course there is more to it that this but that should give you a basic outline of considerations since you are just starting out. Keep it simple. Don't try to over engineer a solution if a simple query string will suffice. You can always over engineer late as long as you have kept it simple to start.

like image 160
Kelsey Avatar answered Jun 23 '26 02:06

Kelsey


I think context is important here, e.g. what are you trying to pass between pages and why?

If you are dealing with complex, multi-part forms, then you can implement the form in a single page, simply showing or hiding relevant element. Use usercontrols and custom controls as much as possible to facilitate isolation and reusability. This makes life a lot easier across the board.

Anything that is user-generated is almost certainly going to end up in a database anyway - so #5 does not seem relevant. That is you shouldn't have to store data "temporarily" in a database- what data would need to be persisted between pages that isn't part of your application.

Anything else would seem to be session related and not that much data.

I could add some more thoughts if I knew what specifically you were dealing with.

Oh - "cookies have potential safety issues and take time" - you're going to use cookies, unless you don't want to be able to identify return visitors. Any potential safety issues would only be a result of bad implementation, and certainly passing data in hidden fields is no better. And you really don't want to get into writing an ASP.NET app that is designed around pages posting to forms other than itself. That's just a headache for many reasons and I can't think of a benefit of doing this as part of basic application design.

like image 21
Jamie Treworgy Avatar answered Jun 23 '26 01:06

Jamie Treworgy