The most important component of ASP is the Database component, which is nothing less than the ADO. I left this topic for last so that we can use the objects discussed earlier in this chapter to build more elaborate and practical examples. Although you can use other components to access databases on the server, most Web developers use the ADO component,
As you recall from our discussion in Chapter 18, the ADO is the simplest object for accessing databases. Its object model is much simpler than that of DAO (Data Access Objects) or even ROO (Remote Data Objects). Yet, the ADO is the most powerful component and used on most sites ~t deploy Active Server Pages. You can also use the DAO component and even develop your own Active X components to access databases on the server. This approach will work, but if you are setting up a new Web site, you should seriously consider ADO. It’s faster, consumes fewer resources on the server (an important consideration for servers with a heavy load), and will eventually replace the DAO.
Setting Up an ODBC Data Source
To access a database on the server through the Database Component, you first define a System ODBC data source for the database, using the ODBC Administrator. Follow these steps:
1. Choose Start >- Settings> Control Panel.
2. Double-click 32bit ODBC (the ODBC Administrator) to open the ODBC Data Source Administrator dialog box:
The ALL PRODS.ASP Page
The ALLPRODS.ASP page displays a list of all the product categories in the NWIND database, as shown.
The ASP page retrieves the category names and formats them as hyperlinks. When the visitor clicks a category name, the hyperlink takes him or her to another ASP page, the PRODCAT.ASP page, which displays all the products in the selected category, along-with their prices and the number of units in stock The output of the PROOCAT.ASP page is shown.
Let’s examine the code of the ALLPRODS.ASP page. There’s very little HTML code on this page; it’s a server-side script that opens the NWINDDB database, creates a Record set with all the category names, and displays them as hyperlinks.
The line that inserts the hyperlinks is the most interesting code in this listing. If you ignore the expression surrounded by the server-side script tags, it’s a simple HTML tag for inserting a hyperlink. Because the hyperlink’s name and destination are not known at the time of this file’s design, we insert them as expressions, which are substituted by the Active Server Pages when the file is processed.
Notice that all hyperlinks call the same URL on the server (the file PRODCAT.ASP), but each hyperlink passes a different parameter to it. The parameter is the ID of the category since this is the field stored in the Products table of the NWlND database, along with each product. The PRODCAT.ASP page uses these parameters to retrieve the products that correspond to the selected category. In addition, it passes the name of the category as the second parameter. This value is not needed in extracting the requested products from the database, but the script uses it to display the name of the selected_ category. Had we omitted this parameter, the PRODCAT.ASP page would have to open the Categories table to find out the name of the selected category, display it in the page’s header, and then open the Products table to retrieve the product names. By passing the name of the category as a parameter, we save the script from establishing a new connection to the database and reading a value that already known.