Data Access  «Prev  Next»

Add Navigation Controls - Exercise

Adding navigation controls

Objective:Add a RecordsetNavBar control to Specials.asp.

Exercise scoring

This is a hands-on exercise worth 2 points and will be automatically scored. When you are finished, just click the Submit button to receive full credit.
  • Background and Overview In this exercise you will add a RecordsetNavBar control to the Specials.asp page to provide a way to display other records in the recordset.
This exercise is written using JavaScript (Node.js/Express with modern front-end technologies) for Visual Studio 2022.
This assumes:
  1. ASP (Classic) is deprecated, so we migrate to a modern Express.js-based server.
  2. Database interaction should be handled using Node.js and an ORM (like Sequelize for SQL databases or MongoDB for NoSQL).
  3. The front-end uses HTML, JavaScript (client-side), and AJAX for database interaction.

1. Setting Up a Modern Node.js Server (Express):
This replaces "ASP server-side processing" with a REST API.
Step 1: Install Dependencies:
Open a terminal in Visual Studio 2022: and run:
npm init -y
npm install express mysql2 sequelize ejs

Step 2: Create `server.js` (Express API):
This file serves the :BookRecords API:.
const express = require('express');
const { Sequelize, DataTypes } = require('sequelize');

// Initialize Express app
const app = express();
const PORT = 3000;

// Setup database connection (Replace with actual credentials)
const sequelize = new Sequelize('bigbookdb', 'username', 'password', {
    host: 'localhost',
    dialect: 'mysql'
});

// Define BookRecords Model
const BookRecords = sequelize.define('BookRecords', {
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    title: { type: DataTypes.STRING },
    author: { type: DataTypes.STRING }
}, { timestamps: false });

// Middleware
app.use(express.static('public'));
app.use(express.json());
app.set('view engine', 'ejs');

// Route to fetch book records
app.get('/books', async (req, res) => {
    try {
        const books = await BookRecords.findAll();
        res.json(books);
    } catch (error) {
        res.status(500).json({ error: "Database error" });
    }
});

// Serve the HTML page
app.get('/', (req, res) => {
    res.render('index');
});

// Start the server
app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));

2. Creating the Front-End (Client-Side JavaScript):
This replaces :ASP-generated HTML with JavaScript (AJAX) for dynamic content.:
Step 3: Create `views/index.ejs`:
This serves as the :HTML front-end with JavaScript:.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Book Records</title>
    <script defer src="/script.js"></script>
</head>
<body>
    <h1>Book Records</h1>
    <table border="1">
        <thead>
            <tr>
                <th>ID</th>
                <th>Title</th>
                <th>Author</th>
            </tr>
        </thead>
        <tbody id="bookTableBody"></tbody>
    </table>
</body>
</html>

3. Fetching Data with JavaScript (AJAX):
Step 4: Create `public/script.js`:

This JavaScript :fetches data from the Node.js API:.
document.addEventListener('DOMContentLoaded', () => {
    fetch('/books')
        .then(response => response.json())
        .then(data => {
            const tableBody = document.getElementById('bookTableBody');
            data.forEach(book => {
                let row = document.createElement('tr');
                row.innerHTML = `${book.id}${book.title}${book.author}`;
                tableBody.appendChild(row);
            });
        })
        .catch(error => console.error('Error fetching books:', error));
});

Summary of Improvements:
Next Steps:
  • If using SQL Server instead of MySQL, install mssql instead of mysql2.
  • If requiring authentication, implement express-session for user management.
  • If needing pagination/search, extend the API with query parameters.
  • Instructions
    1. Add a RecordsetNavBar control to the Specials.asp.
    2. Open Specials.asp in the source editor
    3. Drag the RecordsetNavbar DTC into the Web page below the </Table> tag
    4. Set the properties
    5. Also, add a standard HTML heading to the top of the display. The heading should read "Current Specials"