views:

525

answers:

7

I have question which I hope some one can answer, what is the best way of transferring Datatable to another .aspx page so it can gathered and binded to Gridview in C#?

  • Cookies?
  • Sessions?
  • Cache?
+2  A: 

Cookies- definitely not. Session- possibly, depending on the number of users/load on server. Cache- probably not a good idea unless the same grid is view by lots of users (ie the data warrants being cached).

Can't you provide some information to the other page that would allow that page to get the data for the gridview itself- perhaps something as simple as a querystring parameter,e.g. productId=10?

Also read about cross page posting in ASP.NET.

RichardOD
+1  A: 

If you perform a redirect to the other page, the Session is a good place. But if the DataTable is not too expensive to recreate I would probably send a new query to the database.

Darin Dimitrov
A: 

It depends on the contents of your datatable.

I would exclude Cookies because it forces you to transfer all the data to the client and back(guessing that if it's a DataTable it can have lots of records).

Sessions and cache will both work but take in consideration that they will be stored in memory probably for as long as the user stays with the session active.

If the query doesnt take to long to execute, i would consider just to run it again.

Sergio
A: 

You could serialize it in the ViewState.

cxfx
storing the more data in view state will increase the page size during the page rendering.
solairaja
ViewState is specific to one page
Ismail
Secondly it will send large data to and from client which will make page response slow
Ismail
ViewState can be transferred to another page using cross-page posting. The data will only be as large as the DataTable, so judgement is required.
cxfx
A: 

session will be the best opition.

solairaja
Cache is *not* the best option because you can't guarantee that the data will still be there.
Murph
Cache is the worst option. It's volatile and unpredictable. Items can be removed at any time when server detects memory pressure, which could happen between page transfers. It's also not ideal for supporting multiple requests, each of which would require its own unique cache key. Go with Session, ViewState, or persist to a database.
cxfx
@solairaja - doesn't matter, this is quite emphatically *not* what cacheing is for and use of the asp.net cache is therefore liable, at some point, to produce weird errors when the application is under load. Yes, it will probably work, but that doesn't make it right or acceptable.
Murph
@cxfx- I'd say cache is the worst option after cookies.
RichardOD
+4  A: 

There is no single "best" - as it will depend on the structure of the application and the size of the lump of data you want to transfer. It will also depend on what you are doing with it at either end.

At the most basic level the answer is in Session state - cookies are inappropriate because (generalising) the lump of data you want to move is too big. Cache is there to avoid you having to reload things, but (again generalising) you shouldn't use it for things that you require to be there when you go back to look which leaves you with session.

Of course that assumes that you keep the datatable at all. The other way to do this is to just maintain the key value(s) that allow you to retrieve the datatable from store - so you load the table once for the first page, do stuff, persist the key value(s) navigate to the second page, reload the datatable from your storage and then do such updates as are required. This is notionally a better model (its a tradeoff between the overhead or serializing/deserializing the table to session vs pulling the data from the datastore and of course between datastore and application is an appropriate opportunity to cache data) and if you go down this route you can use either session or, if you want, a cookie which in turn frees you from dependency on session.

As I say, the simple, practical, answer is in Session - but you need to be aware of the overhead and of the other constraints this places upon you.

Murph
Another factor to decide between Session and Cache: Session is user-specific while the Cache is application wide. So an item stored in the Cache is visible to all users, which is not the case for Session.
Hans Kesting
@Hans - I'd completely missed that's despite it being so obvious it hurts (well ish, depends on the how you key things). Shall bear it in mind.
Murph
+1  A: 

Cross page posting is one thing which you can do by exposing a property which holds your DataTable. Another option is populating the datatable again on the next page if it is not too expensive to do it again. Cache and session, I think, will be expensive. Cookies is definately not an option as it will expose all the things to client.

Ismail