Persisting a dataset between event handlers

G

Guest

I'm relatively new to ASP and .NET and having trouble getting my head round
this.
I want to read a datset from a database, and display information from one
row at a time, using a Next and a Previous button to navigate through the
dataset.
I read the dataset in the first Page_Load event ie. inside an if
(!IsPostBack) condition, but it is gone again once I get to the button click
event.
Is there no alternative but to reopen the connection and read the entire
dataset every time a button is pushed? Or am I missing something?
 
J

Jan Peter Stotz

Dave said:
I want to read a datset from a database, and display information from one
row at a time, using a Next and a Previous button to navigate through the
dataset.
I read the dataset in the first Page_Load event ie. inside an if
(!IsPostBack) condition, but it is gone again once I get to the button click
event.

Have you checked that your code is executed in the same request (both the
dataset loading and the button click)?
If you click a button a postback is initiated and you wrote that you load
the database entry only if the request is *not* a postback...

Jan
 
L

Lucas Tam

According to what I've read since my last post, the cache is in the
application domain, ie. it behaves like the Application object, not
like the Session oibject, which means there is only one for all the
users of the application. I'm not exactly sure of the difference
between the Application object and the Cache object, but I think the
Cache is more efficient - there's lots of material out there. It was
no use to me because it is in the Application domain.

Yup, and the OP seemed to want to persist data for a specific user, for a
specific transaction... so the cache object may not be the best place to
(easily) store such data.
 
J

Juan T. Llibre

Lucas,

while the data might be pertinent for one user,
it will be pertinent to *every* user that visits the site.

That will result in the dataset being stored in memory
*once for each user* that accesses the page in question.

That might add up to a lot of memory resources.

OTOH, if the Cache object is used, ONE dataset
in memory will serve the needs of many visitors,
saving a lot of memory resources.
 
J

Jan Peter Stotz

Dave said:
I don't read the data in the button_click event, I only read it in the
Page_Load event.
If I read the database in the Page_Load event even when it is a postback,
then I read the whole recordset from the database every time the user pushes
a button. My question is, must I do this, or is there a better way.

You can avoid reloading the data if you include it into the viewstate,
which should be done by default. But now the data will be inserted into the
HTML response twice (one time the visible text and the second time in the
see hidden viewstate input control), so this should not be done if you want
to display large amounts of data from the database.

Jan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top