getCategories.asp Module Code

The getCategories.asp module gets a list of Product Categories by storing a Recordset into session variable arrays. The main steps are:

  1. The Connection Object: Making and opening a connection

    This application first defines a connection to a database by setting up a Connection object to reference the data source. The steps taken are:

    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 Recordset Object: Executing a command with the Connection object that returns a Recordset

    After creating and opening a connection, the following code executes a query that returns the number of rows in the database:

    SQL = "SELECT row_count = count(*) from Category"Set rsPubs = connPubs.Execute(SQL)iRowCount = rsPubs("row_count")
  3. The Field Object: Extracting data from a Recordset and assigning the data to session variable arrays

    The following code executes a query that returns a Recordset object. Then the application loops through the Recordset, extracts the values of the Field objects, and stores the values in session variable arrays. The application closes the connection when it reaches the end of the Recordset.

    SQL = "SELECT * from Category"Set rsPubs = connPubs.Execute(SQL)
    i = 1
    do until rsPubs.EOF    
    aipkCategoryIDs(i) = rsPubs("CategoryID")
    acCategoryNames(i) = rsPubs("CategoryName")
    acCategoryDescriptions(i) = rsPubs("CategoryDescription")
    rsPubs.MoveNext
    i = i+1
    Loop
    rsPubs.close
    Session("aipkCategoryIDs") = aipkCategoryIDs
    Session("acCategoryNames") = acCategoryNames
    Session("acCategoryDescriptions") = acCategoryDescriptions