This article may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. To maintain the flow of the article, we've left these URLs in the text, but disabled the links.
|
Ad Hoc Web Reporting with ADO 2.0
Johnny Papa and Charles Caison |
Ever been frustrated with Web sites that just don't give you data in the format you want? With ADO 2.0 and Active Server Pages, you can make these annoyances a thing of the past. |
As Web developers, we are often faced with the challenge of moving existing reports to the Web for our clients. In fact, the Web is the most logical place to showcase reports because Web browsers themselves are ultra-thin clients. If you compare traditional client-server architecture to the Web architecture, you can see some very real advantages to using the Web. For example, a standard client-server application requires that each client PC has a copy of the presentation tier software installed on it. This means that for every user of the system, you must install a copy of the client software on their PC so they can actively use the system. However, a Web application only requires a Web browser to be installed on users' PCs. With Web applications, we no longer need to support client installations of our products. We simply update the Web server and the client's browser receives the update automatically the next time they contact the Web server. None of this is news to you, however. The demand for more accessible and highly flexible reports appears to be never ending. Often, the demand for new reports stems from the sales department's need for remote access to reports while they are on the road. Other times the demand comes from upper management, who has decreed that all existing reports must be accessible from all nationwide branches in a standard format. Wherever the call originates for the reporting development, we have to determine how to accommodate the needs of our clients. This is where the Web and ADO make our lives much simpler.
ADO 2.0 Steps In
One Page at a Time
|
Figure: A Sample Web Report |
If you are not familiar with paging, now is a good time to learn. Paging is the ability to show a user-defined number of records on the screen, then allow the user to move forward and backward through the report to different pages of records. For example, if we click on the Next link on the report in Figure 1, the report will display the second page of data. The display of 10 records on a page is a user-configurable setting. We can set the number of rows to be displayed on each page of the report using the Records per page text box. By changing this value and clicking the Refresh Report link in the upper-left corner of the report, the report will be redisplayed showing the number of records per page that we entered. These reports can even remember the page number we were on. For example, let's assume we are on the third page of a 25-record report that displays 10 records per page. We then change the page size from 10 to 7 records per page and refresh the report. ADO reconfigures the page size to display 7 records at a time (with a total of 4 pages). So what about the times when we are on page 10, for example, then we change the page size so that there are only 3 pages? In this case, we have the report go to the last available page. Otherwise, if we tell ADO to go to an invalid page number, it will raise an error. Alternatively, we could have coded this to go to the first page every time the page size changes. In any case, this decision is up to the developer, which ultimately means it is up to the client who commissions the project. |
Figure 2: Displaying All Records |
Of course, paging is great for situations when we do not want to see all of the records at once. But what if we really want to see all of the records on a single page so we can scroll through it? With this report, we simply click the Show All Records link, as shown in Figure 2.
A Report of a Different Sort
Under the Hood
|
|
Now that we have sorted the ADO recordset, we need to determine if the page size has changed or if the user moved to a different page. First, we check to see if the page size changed. If it did, we set the page size accordingly. If the user decided to go to another page, we navigate to that page directly. All of this is moot if the user requests that all records be displayed on a single page, in which case there is no need for paging, so we simply display all records. We used some JScript® to set the page size, as you'll see in Figure 6. The MoveToPage function in Figure 7 is called by the onchange event of the Go to page combobox. When a user goes to a page, this function sets the page number and submits the page to refresh the report. The ShowAllRecs function fires when the user clicks on the hyperlink to display all records on a single page. This function sets the page size to the number of records in the ADO recordset, then submits the page to refresh the report. Finally, the ReSort function sets the column to sort by and submits the page to refresh the report. All of this script occurs on the client side and is written in JavaScript to support both Microsoft® Internet Explorer and Netscape Navigator. Once we have coded this functionality into the page, the rest of the script is relatively routine. First, we create the report headers, as shown in Figure 8. Then we create the hyperlinks to show all records, refresh the report, and allow the user to go directly to a page (see Figure 9). We can make a hyperlink submit a page indirectly by having the hyperlink's href specify some JavaScript using the following syntax: |
|
Pointing the href to a client-side JavaScript function, we can call our functions as we outlined in Figure 7. (This method is common among Web developers who want to find a way around having submit buttons all over a screenalthough they could serve our purposes too.) At this point we need to start displaying the column headings. This is pretty easy with ADO since the fields and their attributes are exposed through the Fields collection. We can loop through this collection and display each field in the ADO recordset using the Name property, as shown here: |
|
But we also need to allow the user to sort by columns, so we make the column heading a hyperlink. The hyperlink uses some client-side JavaScript to call the ReSort function, passing the name of the column heading. This will be used by the ReSort function to determine which column to sort by. Once we have the column headings and the code behind their links, we need to tackle the display of the data rows, as shown in Figure 10. This is a simple process of looping through each row using the MoveNext method of the ADO recordset. Within each record, we must loop again, this time through the Fields collection to display each column's value. Now, in Figure 11, we begin the report footer by determining whether we require the Previous or Next hyperlinks. These hyperlinks call the MoveToPage client-side JavaScript function with the next or previous page number, as appropriate. But first we need to decide if these links make sense or not. For example, why have a Previous link if we are on the first page? A quick check of the intCurrentPage variable makes this determination for us, while a comparison between the same variable and the ADO recordset's PageCount property reveals whether we need the Next link. Making sure we touch all of the bases, we display a message stating there were no records retrieved by the report's SQL: |
|
We mentioned earlier that we would store values in hidden text boxes that would be evaluated by server-side ASP. These hidden HTML text boxes are set by the client-side JavaScript functions from Figure 7. |
|
For example, if a user clicks on the hyperlink to sort the records by a particular column, the JavaScript sets the SortBy text box to the sort column and the ReSort text box to yes. Finally, we save the disconnected recordset in the Session variable Report so we can use it again. |
|
This is why it is important to use a disconnected recordset; otherwise, we would have to keep a constant connection
open with the database. Imagine if you had a hundred
users reading your Web reports! That would mean many
open (and little-used) connections wasting your database
server's resources.
Ad Hoc Reporting
One Step Further
|
From the December 1998 issue of Microsoft Interactive Developer.