473,322 Members | 1,714 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,322 software developers and data experts.

How to avoid two DB calls during page life cycle.

I have an interesting issues that, although I not blocking me, is not very
performance. Maybe someone can suggest a better way of doing this.

1) I have a page that displays a grid with command links such as Delete.
Obviously if this link will delete the item of the current line.
2) The grid is initialized during the Init event so that the events can be
bound. It is my understanding that events do not get fired unless the
object structure is complete, so I do this in the Init. A call to the
database gets the list of current items.
3) Then the command event fires and I make a call to the database to remove
the item.
4) On PreRender, I then query the database again for the list and build it.
This is because the currently bound data is for the list containing the item
that was just removed. I need to refresh it.

As you can see, I am querying the database twice, when the page first
initializes, then before it renders. Do I have to do it this way, or is
there some way to only query the database one.
Nov 19 '05 #1
5 1503
Why do you to query in PageLoad on postback? BTW, PreRender event is not
good for getting data. PreRender is meant to be the very last stop before
rendering, when all data is already there.

Eliyahu

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:eg**************@TK2MSFTNGP12.phx.gbl...
I have an interesting issues that, although I not blocking me, is not very
performance. Maybe someone can suggest a better way of doing this.

1) I have a page that displays a grid with command links such as Delete.
Obviously if this link will delete the item of the current line.
2) The grid is initialized during the Init event so that the events can be bound. It is my understanding that events do not get fired unless the
object structure is complete, so I do this in the Init. A call to the
database gets the list of current items.
3) Then the command event fires and I make a call to the database to remove the item.
4) On PreRender, I then query the database again for the list and build it. This is because the currently bound data is for the list containing the item that was just removed. I need to refresh it.

As you can see, I am querying the database twice, when the page first
initializes, then before it renders. Do I have to do it this way, or is
there some way to only query the database one.

Nov 19 '05 #2
Okay, you fetch data to "get the list of current items." Later, you fetch
the same data to bind to the list. Am I following you so far? And you want
to avoid the second db call?

The Page is a class. A class can have fields and/or properties which are
global to the class, right? So, if you were to fetch the data the first
time, store it in a private field, you should be able to fetch it from that
field the second time, right?

I mean, what you're describing is like getting your morning paper, looking
over the headlines, throwing it away, and then buying a paper on the way to
work so you can read it at lunch. The remedy is not to throw away the paper
in the first place.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:eg**************@TK2MSFTNGP12.phx.gbl...
I have an interesting issues that, although I not blocking me, is not very
performance. Maybe someone can suggest a better way of doing this.

1) I have a page that displays a grid with command links such as Delete.
Obviously if this link will delete the item of the current line.
2) The grid is initialized during the Init event so that the events can
be bound. It is my understanding that events do not get fired unless the
object structure is complete, so I do this in the Init. A call to the
database gets the list of current items.
3) Then the command event fires and I make a call to the database to
remove the item.
4) On PreRender, I then query the database again for the list and build
it. This is because the currently bound data is for the list containing
the item that was just removed. I need to refresh it.

As you can see, I am querying the database twice, when the page first
initializes, then before it renders. Do I have to do it this way, or is
there some way to only query the database one.

Nov 19 '05 #3
The list that I am loading is a DataGrid with a Delete command column.
Unless I don't understand the way the server works, I believe that in order
for the events to be dispatched, the grid must be fully populated. I know
this is the case with other controls such as listboxes. If the item does
not exist in the grid when events are handled, the DeleteCommand event will
not fire. That is why I populate it here.

Now the DeleteCommand event if fired, which will go out to the database and
remove the item. The item no longer appears in the database. So, the grid
and the list from the database no longer match.

Now, I update the grid again with the new list of items since there is one
fewer. That is why I repopulate it in the PreRender.

It is not like I am loading the same list twice. I understand that is bad.
I am loading the grid twice with different results (the second has one less
item then the first). Is there a better way of handling this scenario?

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:#k**************@tk2msftngp13.phx.gbl...
Okay, you fetch data to "get the list of current items." Later, you fetch
the same data to bind to the list. Am I following you so far? And you want
to avoid the second db call?

The Page is a class. A class can have fields and/or properties which are
global to the class, right? So, if you were to fetch the data the first
time, store it in a private field, you should be able to fetch it from that field the second time, right?

I mean, what you're describing is like getting your morning paper, looking
over the headlines, throwing it away, and then buying a paper on the way to work so you can read it at lunch. The remedy is not to throw away the paper in the first place.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
What You Seek Is What You Get.

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:eg**************@TK2MSFTNGP12.phx.gbl...
I have an interesting issues that, although I not blocking me, is not veryperformance. Maybe someone can suggest a better way of doing this.

1) I have a page that displays a grid with command links such as Delete. Obviously if this link will delete the item of the current line.
2) The grid is initialized during the Init event so that the events can
be bound. It is my understanding that events do not get fired unless the object structure is complete, so I do this in the Init. A call to the
database gets the list of current items.
3) Then the command event fires and I make a call to the database to
remove the item.
4) On PreRender, I then query the database again for the list and build
it. This is because the currently bound data is for the list containing
the item that was just removed. I need to refresh it.

As you can see, I am querying the database twice, when the page first
initializes, then before it renders. Do I have to do it this way, or is
there some way to only query the database one.


Nov 19 '05 #4
> It is not like I am loading the same list twice. I understand that is
bad.
I am loading the grid twice with different results (the second has one
less
item then the first). Is there a better way of handling this scenario?
So, what you're telling me is that the 2 result sets are the same, except
that the second doesn't have one record that is in the first, right?

Now, hitting a database is one of the most expensive operations you can
perform. Hitting it twice to get the same data is just a crime. Note that
you're not getting different data. You're getting the same data, with one
less row.

If you look in the .Net SDK's reference for the DataGrid.DeleteCommand
event, you'll see an example, In the example, the data is fetched one time,
and stored as a DataTable in Session. A Custom DataView is created for
displaying the data. When the DeleteCommand is received, the DataView
removes the record from the DataTable. No repeat visit to the database is
necessary.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:Oi**************@TK2MSFTNGP12.phx.gbl... The list that I am loading is a DataGrid with a Delete command column.
Unless I don't understand the way the server works, I believe that in
order
for the events to be dispatched, the grid must be fully populated. I know
this is the case with other controls such as listboxes. If the item does
not exist in the grid when events are handled, the DeleteCommand event
will
not fire. That is why I populate it here.

Now the DeleteCommand event if fired, which will go out to the database
and
remove the item. The item no longer appears in the database. So, the
grid
and the list from the database no longer match.

Now, I update the grid again with the new list of items since there is one
fewer. That is why I repopulate it in the PreRender.

It is not like I am loading the same list twice. I understand that is
bad.
I am loading the grid twice with different results (the second has one
less
item then the first). Is there a better way of handling this scenario?

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:#k**************@tk2msftngp13.phx.gbl...
Okay, you fetch data to "get the list of current items." Later, you fetch
the same data to bind to the list. Am I following you so far? And you
want
to avoid the second db call?

The Page is a class. A class can have fields and/or properties which are
global to the class, right? So, if you were to fetch the data the first
time, store it in a private field, you should be able to fetch it from

that
field the second time, right?

I mean, what you're describing is like getting your morning paper,
looking
over the headlines, throwing it away, and then buying a paper on the way

to
work so you can read it at lunch. The remedy is not to throw away the

paper
in the first place.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
What You Seek Is What You Get.

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:eg**************@TK2MSFTNGP12.phx.gbl...
>I have an interesting issues that, although I not blocking me, is not very >performance. Maybe someone can suggest a better way of doing this.
>
> 1) I have a page that displays a grid with command links such as Delete. > Obviously if this link will delete the item of the current line.
> 2) The grid is initialized during the Init event so that the events
> can
> be bound. It is my understanding that events do not get fired unless the > object structure is complete, so I do this in the Init. A call to the
> database gets the list of current items.
> 3) Then the command event fires and I make a call to the database to
> remove the item.
> 4) On PreRender, I then query the database again for the list and
> build
> it. This is because the currently bound data is for the list containing
> the item that was just removed. I need to refresh it.
>
> As you can see, I am querying the database twice, when the page first
> initializes, then before it renders. Do I have to do it this way, or
> is
> there some way to only query the database one.
>
>



Nov 19 '05 #5
I see, so basically I would delete it from two places, the DataTable and the
database itself. Makes sense.

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl...
It is not like I am loading the same list twice. I understand that is
bad.
I am loading the grid twice with different results (the second has one
less
item then the first). Is there a better way of handling this scenario?
So, what you're telling me is that the 2 result sets are the same, except
that the second doesn't have one record that is in the first, right?

Now, hitting a database is one of the most expensive operations you can
perform. Hitting it twice to get the same data is just a crime. Note that
you're not getting different data. You're getting the same data, with one
less row.

If you look in the .Net SDK's reference for the DataGrid.DeleteCommand
event, you'll see an example, In the example, the data is fetched one

time, and stored as a DataTable in Session. A Custom DataView is created for
displaying the data. When the DeleteCommand is received, the DataView
removes the record from the DataTable. No repeat visit to the database is
necessary.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
What You Seek Is What You Get.

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:Oi**************@TK2MSFTNGP12.phx.gbl...
The list that I am loading is a DataGrid with a Delete command column.
Unless I don't understand the way the server works, I believe that in
order
for the events to be dispatched, the grid must be fully populated. I know this is the case with other controls such as listboxes. If the item does not exist in the grid when events are handled, the DeleteCommand event
will
not fire. That is why I populate it here.

Now the DeleteCommand event if fired, which will go out to the database
and
remove the item. The item no longer appears in the database. So, the
grid
and the list from the database no longer match.

Now, I update the grid again with the new list of items since there is one fewer. That is why I repopulate it in the PreRender.

It is not like I am loading the same list twice. I understand that is
bad.
I am loading the grid twice with different results (the second has one
less
item then the first). Is there a better way of handling this scenario?

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:#k**************@tk2msftngp13.phx.gbl...
Okay, you fetch data to "get the list of current items." Later, you fetch the same data to bind to the list. Am I following you so far? And you
want
to avoid the second db call?

The Page is a class. A class can have fields and/or properties which are global to the class, right? So, if you were to fetch the data the first
time, store it in a private field, you should be able to fetch it from

that
field the second time, right?

I mean, what you're describing is like getting your morning paper,
looking
over the headlines, throwing it away, and then buying a paper on the way
to
work so you can read it at lunch. The remedy is not to throw away the

paper
in the first place.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
What You Seek Is What You Get.

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:eg**************@TK2MSFTNGP12.phx.gbl...
>I have an interesting issues that, although I not blocking me, is not

very
>performance. Maybe someone can suggest a better way of doing this.
>
> 1) I have a page that displays a grid with command links such as

Delete.
> Obviously if this link will delete the item of the current line.
> 2) The grid is initialized during the Init event so that the events
> can
> be bound. It is my understanding that events do not get fired unless

the
> object structure is complete, so I do this in the Init. A call to

the > database gets the list of current items.
> 3) Then the command event fires and I make a call to the database to
> remove the item.
> 4) On PreRender, I then query the database again for the list and
> build
> it. This is because the currently bound data is for the list containing > the item that was just removed. I need to refresh it.
>
> As you can see, I am querying the database twice, when the page first
> initializes, then before it renders. Do I have to do it this way, or
> is
> there some way to only query the database one.
>
>



Nov 19 '05 #6

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

Similar topics

5
by: Hardy Wang | last post by:
Hi all: I developed a web site, it passed various testing internally. When I put this site in production, I receive some error reports from this site saying "the viewstate is invalid for this page...
6
by: MooreSmnith | last post by:
When I navigate to the next page using Response.Rediect("MyNextPage.aspx") current page Page_Load event is called. What I may wrongly understood is that post back will happen whenever there is any...
6
by: jim | last post by:
Hi All, I like to know the life cycle of an ASP .NET Application( incudieng server application, such as .NET Web Service). That means from initialization to fully running and how to reboot it or...
3
by: | last post by:
Hi all, when are code render blocks rendered in asp.net page life cycle? what method does the code render block rendering? thanks! ingo
2
by: clintonG | last post by:
I'm having problems referencing public properties in a MasterPage from a class located in App_Code. I need to learn more about the page life cycle and when the classes in App_Code are accessible to...
3
by: Griff | last post by:
I want to build a set of compiled user controls to incorporate into a web site. The majority of user controls will be placed on specific web page and these controls would share a common...
3
by: aaryan | last post by:
hi all, just want to know the life cycle of an asp.net page with relevant example for each stage. i find many web sites demonstrating them but i feel they are too complex to follow. so can anybody...
4
by: lander | last post by:
I've read the page life cycle thing in msdn, still, i'm getting a bit confused of thinking how all the things are going under the hood... I know that when page loading, that the controls'...
11
Niheel
by: Niheel | last post by:
http://bytes.com/images/howtos/information_overloaded.jpgPaul Graham wrote an interesting article a few months back about how the internet is leading to information overload for information workers...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.