Catalog.asp Module Code

The Catalog.asp module retrieves the list of products by using a Command object with a stored procedure and displays the list. The main steps are:

  1. The Connection Object: Making and opening a connection

    This application first uses the following steps to define a connection to a database by setting up a Connection object to reference the data source:

    1. Set up the Connection object.

    2. Return the number of seconds to wait when creating a connection before stopping the attempt and returning an error specified in the global.asa module.

    3. Return the number of seconds to wait when executing a command before stopping the attempt and returning an error specified in the global.asa module.

    4. Specify the data source and user name to apply when opening the connection.

    5. Open the connection.

    The corresponding code follows:

    Set connPubs = Server.CreateObject("ADODB.Connection")
    connPubs.ConnectionTimeout = Session("accts_ConnectionTimeout")
    connPubs.CommandTimeout = Session("accts_CommandTimeout")
    connPubs.ConnectionString = "DSN=Sample;UID=sa;"
    connPubs.open
    
  2. The Command Object: Specifying a query that is a stored procedure

    After creating and opening a connection, the application sets up a Command object by using the following steps:

    1. Set up the Command object.

    2. Specify the text of the command to execute.

    3. Specify that the command text is a stored procedure.

    The corresponding code follows:

    Set cmdPubs = Server.CreateObject("ADODB.Command")cmdPubs.CommandText = "sp_get_products_by_category"
    cmdPubs.CommandType = adCmdStoredProc
    
  3. The Parameter Object: Specifying the input parameters of a command

    After creating a Command object, the application sets up an input Parameter object using the following steps:

    1. Set up the Parameter object collection.

    2. Adds the following input parameters to the collection:

      @piCategoryID of type Integer with a size of 4

    The corresponding code follows:

    set p = cmdPubs.Parameters

    p.Append cmdPubs.CreateParameter("@piCategoryID", adInteger, adParamInput, 4)

  4. The Parameter Object: Assigning a value to the input parameter of a command

    The following code requests and assigns a value to the input parameter:

    cmdPubs("@piCategoryID") = Request("cCategoryID")
    
  5. The Recordset Object: Creating an active connection and executing a command that returns a Recordset

    The following code specifies the connection to use and then executes the query that returns the Recordset object:

    Set cmdPubs.ActiveConnection = connPubs
    set rsPubs = cmdPubs.Execute
    
  6. The Field Object: Extracting data from a Recordset, assigning the data to variables, and displaying the data

    The following code loops through the Recordset, extracts the values of the Field objects, and displays them. The application closes the connection when it reaches the end of the Recordset.

    if rsPubs.EOF then
           %>
    <TR>
    <TD>
    No items found
    <TD>
    <%
    else
    do until rsPubs.EOF    
    htmlProductID = rsPubs("ProductID")
    htmlCategoryID = rsPubs("CategoryID")
    htmlProductPrice = rsPubs("ProductPrice")
    htmlProductName = rsPubs("ProductName")
    htmlProductDescription = rsPubs("ProductDescription")
    %>
    <TR>
    <TD><A HREF="Product.asp?iProductID=<% = htmlProductID %>">
    <% = htmlProductName %></A>
    <TD><% = htmlProductPrice %>
    <%
    rsPubs.MoveNext
    Loop
    rsPubs.close

end if