473,413 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Persisting a dataset between event handlers

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?
--
Dave
Nov 19 '05 #1
12 1584
Dave schrieb:
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
Nov 19 '05 #2
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.
--
Dave
"Jan Peter Stotz" wrote:
Dave schrieb:
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

Nov 19 '05 #3
Dave wrote:
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?


Nope. You're not missing anything. CGI programming is stateless. CGI
programming in ASP.NET gives you the ability to hide this fact from
yourself in certain cases, but this is not one of them.

You will need to open a new connection to the database with every
postback to the server. Your page is posting back behind the scenes to
drop you off at that OnClick handler. You'll have access to the
information on the Form, QueryString, Cookies, and Session state.
Hopefully you will have stashed enough information there to enable you
to rebuild your DataSet.

Good luck!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com

Nov 19 '05 #4
"=?Utf-8?B?RGF2ZQ==?=" <Da**@discussions.microsoft.com> wrote in
news:1E**********************************@microsof t.com:
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?


If it takes a long time to execute the query, you could store the entire
dataset as a session variable.
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 19 '05 #5
Aargh!

Please don't store datasets in the Session object.
It's far preferrable to use the Cache object for that.

See sample at :

http://dotnethero.com/hero/Dataset/Cache.aspx?nmx=3_4

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn**************************@127.0.0.1...
"=?Utf-8?B?RGF2ZQ==?=" <Da**@discussions.microsoft.com> wrote in
news:1E**********************************@microsof t.com:
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?


If it takes a long time to execute the query, you could store the entire
dataset as a session variable.
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 19 '05 #6
Thanks Jason, and eveyone else. That's what I thought. I gather from my
reading that the database engines are pretty good at caching, so there
shouldn't be a huge performance hit.
--
Dave
"jasonkester" wrote:
Dave wrote:
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?


Nope. You're not missing anything. CGI programming is stateless. CGI
programming in ASP.NET gives you the ability to hide this fact from
yourself in certain cases, but this is not one of them.

You will need to open a new connection to the database with every
postback to the server. Your page is posting back behind the scenes to
drop you off at that OnClick handler. You'll have access to the
information on the Form, QueryString, Cookies, and Session state.
Hopefully you will have stashed enough information there to enable you
to rebuild your DataSet.

Good luck!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com

Nov 19 '05 #7
Now THAT'S useful information. I realised using the Session object wasn't a
good idea, but I didn't know about the cache object. I'll look for more
information on that. Thanks.
--
Dave
"Juan T. Llibre" wrote:
Aargh!

Please don't store datasets in the Session object.
It's far preferrable to use the Cache object for that.

See sample at :

http://dotnethero.com/hero/Dataset/Cache.aspx?nmx=3_4

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn**************************@127.0.0.1...
"=?Utf-8?B?RGF2ZQ==?=" <Da**@discussions.microsoft.com> wrote in
news:1E**********************************@microsof t.com:
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?


If it takes a long time to execute the query, you could store the entire
dataset as a session variable.
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/


Nov 19 '05 #8
"Juan T. Llibre" <no***********@nowhere.com> wrote in news:#UV2KdvpFHA.2904
@tk2msftngp13.phx.gbl:
Aargh!

Please don't store datasets in the Session object.
It's far preferrable to use the Cache object for that.

See sample at :

http://dotnethero.com/hero/Dataset/Cache.aspx?nmx=3_4


Isn't the cache global?

So if the data is only pertient to one user, it's easier to store it in a
session variable than in the cache.
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 19 '05 #9
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.
--
Dave
"Lucas Tam" wrote:
"Juan T. Llibre" <no***********@nowhere.com> wrote in news:#UV2KdvpFHA.2904
@tk2msftngp13.phx.gbl:
Aargh!

Please don't store datasets in the Session object.
It's far preferrable to use the Cache object for that.

See sample at :

http://dotnethero.com/hero/Dataset/Cache.aspx?nmx=3_4


Isn't the cache global?

So if the data is only pertient to one user, it's easier to store it in a
session variable than in the cache.
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 19 '05 #10
"=?Utf-8?B?RGF2ZQ==?=" <Da**@discussions.microsoft.com> wrote in
news:66**********************************@microsof t.com:
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.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 19 '05 #11
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.

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn**************************@127.0.0.1...
"Juan T. Llibre" <no***********@nowhere.com> wrote in news:#UV2KdvpFHA.2904
@tk2msftngp13.phx.gbl:
Aargh!

Please don't store datasets in the Session object.
It's far preferrable to use the Cache object for that.

See sample at :

http://dotnethero.com/hero/Dataset/Cache.aspx?nmx=3_4

Isn't the cache global?

So if the data is only pertient to one user, it's easier to store it in a
session variable than in the cache.
--
Lucas Tam (RE********@rogers.com)

Nov 19 '05 #12
Dave schrieb:
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
Nov 19 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
8
by: Dennis C. Drumm | last post by:
I have a class derived from a SortedList called SystemList that contains a list of objects indexed with a string value. The definition of the objects contained in the SortedList have a boolean...
4
by: Zürcher See | last post by:
I have in a form a datagrid that display a dataset, but if I start a new thread that fill the dataset the program will crash, that's because the dataset is not synchronized, is not thread safe. I...
5
by: Alastair Anderson | last post by:
I have created a very simple form with which I would like to update a single value in a single row of a database as a proof of concept. The relevant parts of the form are a DBWebTextBox (which...
13
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the...
5
by: Dick | last post by:
I have a GridView bound to an ObjectDataSource. I have a Button that calls GridView.DataBind. I want the row that is selected before the DataBind to still be selected afterwards. This happens...
3
by: Carlos | last post by:
Hi all, I currently have a Gridview that doess sorting correctly using several fields. However, I do have a checkbox field that when it is checked I do not know how to make its value to persist...
16
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some...
14
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.