Preserving Data During Round Trips


Let me squeeze in just a little more background information before you look at how to create databases in Web Matrix. The concepts I ll describe here are important to understand when you re working with Web pages and data access.

Picture how a Web page is going to work with data. For example, let s imagine that you have a database table with the names of your friends in it. You want to create a Web page that lists all those names, perhaps in a table or grid. When the page runs, it executes a database query, and for each row in the result set, you get a line in the grid with the name of one of your friends. The page consumes the entire result set in one go.

As you know by now, every time users click a button in an ASP.NET Web page, the page is posted back to the server, where it is created again from scratch. In a Web page that includes data access, this can mean that with every postback, the page executes your database query again. For small queries on low-traffic servers, executing a query every time the page makes a round trip might not be a problem. However, requerying on each postback can result in a noticeable slowdown in your pages especially for high-traffic servers, so it s usually a good idea to cache the data between round trips.

If you re displaying the data in a control such as in the aforementioned grid you can rely on the control itself to cache the data. Controls use viewstate to maintain their values during round trips, so you just need to fill the control when the page first loads (when IsPostBack is false).

At other times, however, you might want to use the same data repeatedly in different ways. For example, you might want to move back and forth among the records returned by the query. In that case, you can keep the data in a dataset, which is an object that works something like a local copy of the database. You can cache a dataset and then when your page makes a round trip, it can read its data from the dataset instead of executing a query against the database again. You ll look into datasets in Chapter 12. For now, just be aware that page round trips can potentially mean rerunning database queries. If re-executing a query with each round trip becomes an issue, you can cache the results of the query.

About Data Binding

The purpose of most of your database queries, of course, will be to display data in your Web pages. The controls that you put on a Web page in Web Matrix allow you to display data in controls using data binding, which allows control properties to bind to specific values from the database. To bind a control to data, you specify where a control should retrieve its data from. You then execute the database query, and when the execution is finished, you call the control s DataBind method to tell the control to read the result set and display the data.

Data binding is actually easier to show than it is to explain. I ll show you plenty of examples of using data binding in the next chapters, so I ll let the examples do the rest of the explaining.




Microsoft ASP. NET Web Matrix Starter Kit
Microsoft ASP.NET Web Matrix Starter Kit (Bpg-Other)
ISBN: 0735618569
EAN: 2147483647
Year: 2003
Pages: 169
Authors: Mike Pope
BUY ON AMAZON

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net