473,503 Members | 11,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

refresh page on database change

Ciary
247 Recognized Expert New Member
hey all,

i'm going to post my next question here since i have no idea where to start. my problem is this:
i'm trying to this. i have a page from where i read all records from a table. when the admin adds a new record to a database, all visitors should get a refresh request or they should automatically refresh.

a more practical example (to better explain what i mean) a radiostation keeps a playlist (starting from their last played song) on their index page. when the radiostation starts a new song, it is added to the database. then, all users visiting the main page at that moment should refresh their browser.

what i do now is read the time of the song and fill it in into a header("refresh") but this can be done a lot better i think.

does anyone have any idea how this can be done?
May 27 '09 #1
10 5876
mbewers1
68 New Member
An example based on C#

You should try looking at the ASP.NET Life cycle and understand - one of the page events should call the method which you used to load the database in the first place.

This site is good:
http://www.15seconds.com/issue/020102.htm

If I was doing this, I would go for the OnPreRender() method and put the code you want to be called ONCE in an IsPostBack clause, then all the code you want to be called each time, including your method to load the database outside the clause, like so:

Expand|Select|Wrap|Line Numbers
  1. protected override void OnPreRender(EventArgs e)
  2.     {
  3.         // code run each time the page is rendered
  4.         // e.g. - the method to load your database
  5.  
  6.         if (!IsPostBack)
  7.         {
  8.             // code to be run only once
  9.         }
  10.  
  11.         base.OnPreRender(e);
  12.     }
  13.  
May 27 '09 #2
Ciary
247 Recognized Expert New Member
so if i understand correctly i need all things executed once in the postback clause so i dont execute things twice (like insert queries for example) the query in which i read the database will be outside this clause.

so far so good, but how do i trigger events like onDatabaseChange.or how do i add a changelistener to it??

already tnx, ive never used ASP.NET before so i have no idea how to code anything in it.
May 27 '09 #3
Frinavale
9,735 Recognized Expert Moderator Expert
@Ciary
Web applications (like an ASP.NET web application) are stateless apps.

This is rather limiting....let's examine this a bit:

ASP.NET applications exist on a Web Server. They are compiled (unless they are already precompiled) and executed when a browser makes a request that involves them.

The ASP.NET application does stuff (like retrieves things from databases etc) and then sends a the resulting response (a web page) to the browser...which displays the web page to the end user.

Now the admin makes a change database...and your data access layer raises an onDatabaseChange event....

Even if your ASP.NET application was listening for the event, what is it supposed to do with it?

The only way that the ASP.NET application can send information to the browser is if a request was made to it in the first place... Otherwise how would it know where (what browser, what IP) to send it to? How would it even send the response if the web browser isn't waiting for a response from the server?

Since it can't know these things, it can't do anything with the event...it can't update any web browsers currently displaying the web page because it simply cannot do this without a request from the web browser for the page.


My point here is if you are developing a web application (you never stated the type of app you're developing) it doesn't matter if the database has changed until a web browser makes a request to the web application.

The onDatabaseChange event is not needed if you implement the web application so that it accesses a live database (which contains the updated information) on every request is made by the browser.

This would mean that you would not cache the data retrieved from the database first time the page is loaded.

There is a neat way to cache your data...in ASP.NET though.

See:
It's a lot easier if you just simply use a live database and refresh your data on every web request.


In order to achieve what you are attempting (updating all browsers displaying the web page when the database is updated), you'd have to constantly ask the server for an updated list. You'll have to have some sort of client side (JavaScirpt/Ajax) timer that pings the server constantly since the web server cannot send a response to a browser without the browser asking for it. This is probably not a good idea because it's going to waste bandwidth and server resources.
May 27 '09 #4
mbewers1
68 New Member
If you have placed a request to lead the database in your OnPreRender() method, then when you or someone using your site clicks a button to confirm a new row, the OnPreRender() will execute the same functionality as you have told it to do at the beginning (i.e. - Load the database records using an SQL query).

Frinavale's post was a bit complicated but he's right - you have to right code to change the state of controls - adding a row into your SQL table by hand won't automatically refresh the page.

Good luck!
Matt
May 27 '09 #5
Ciary
247 Recognized Expert New Member
so basically, since you're saying it isnt possible, what i'm doing now is the best option:
which means i use header(refresh) in php and calculate the time depending on the songs length(retreived from the database)?
-> leads to asynchronous refreshes

- or -

i can fill a database with refreshtimes. php will then calculate the time 'till the next refresh and use it in header(refresh)
-> will synchronize all refreshes but might slow the server on a refresh

maybe i should use the last method and then add a random generated number from 0 to 15 seconds to the refresh-time so there is a small spread in which there is refreshed
May 28 '09 #6
Markus
6,050 Recognized Expert Expert
How annoying would that be? You're doing something on a webpage, reading an article, whatever, and then, out-of-the-blue, the page refreshes.

Updating the whole page would be overkill. Use javascript to update just a portion of it; that way a visitor isn't broken away from whatever they're doing.
May 30 '09 #7
Frinavale
9,735 Recognized Expert Moderator Expert
@Markus
This happened to me when I was playing a game of majon...when it posted back it destroyed my game even though I was doing really well that round! So annoying!

@Markus
Markus is right, consider using Ajax to update only the portion of the page that requires refreshing.
Jun 1 '09 #8
acoder
16,027 Recognized Expert Moderator MVP
Instead of poll/pull, you'll want to look at push technologies. See Comet - also known as Reverse Ajax, Http Streaming, Ajax Push, Server Push, Slow Load Technique and so on.
Jun 1 '09 #9
Frinavale
9,735 Recognized Expert Moderator Expert
@acoder
Interesting!
I think I'm going to have to try the long polling technique later today.

Thanks a lot acoder.
Jun 1 '09 #10
Frinavale
9,735 Recognized Expert Moderator Expert
After investigating the issues that occur with keeping an HTTP connection open for a long time, I found that there are other ways to accomplish this. Most solutions recommend that you use a Flash Object but I've discovered that Silverlight now supports Sockets.

I'm enjoying this article on how to use Silverlight to push notifications with Socket Server. I hope to implement a solution using this technique.

Thanks again acoder!
I would never have known this was possible if you hadn't mentioned Comet :)
Jun 30 '09 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

10
6453
by: Conax | last post by:
Hi there, My boss is hoping that I can come up with a page that displays some information. The information will always be displayed on specific part of the page, with auto refresh. But he...
11
3733
by: Wolfgang Kaml | last post by:
Hello All, I have been working on this for almost a week now and I haven't anything up my sleeves anymore that I could test in addition or change.... Since I am not sure, if this is a Windows...
1
5403
by: Przemek P. | last post by:
I need a button, on click you can go previous page (like histry.go(-1)) and then the page is immediately refreshed. The first page is table from database. When you click to change item, another...
10
6915
by: Bo Rasmussen | last post by:
Hi, I have a problem : I have a form with some buttons. When one of these buttons is pressed a new URL with some parameters to e.g. delete something from a database. The problem is that when the...
3
2977
by: SS | last post by:
Hi, I don't want to refresh a web page to query and display database. I want the page shows information immediately when database changes. What kind of technology or method would be able to...
10
32827
by: phforum | last post by:
Hi, I wrote a PHP page for user input the information to search the database. And the database data will update every second. I want to set the auto refresh to get the data from database every...
10
3135
by: Piotr Nowak | last post by:
Hi, Say i have a server process which listens for some changes in database. When a change occurs i want to refresh my page in browser by notyfinig it. I do not want to refresh my page i.e....
7
8009
by: seanmatthewwalsh | last post by:
Hi I have a page (default.aspx) that pulls it's HTML from a database. I then have a "content management" page (editpage.aspx) that allows the user to edit the HTML in the database. When the...
2
2228
by: malcster2 | last post by:
hello, i am a beginner to ajax. i have created a mysql database, which i would like to access from a web page. i have created 3 files, a html to display the data, a php file to extract the data,...
0
7098
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
7364
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
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4696
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...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.