ASP.NET - Database - Display Records

Here is an example code to display records in an HTML table using ASP.NET and C# with an Access database:

First, create a new ASP.NET web application project in Visual Studio and add a new Web Form named "DisplayRecords.aspx".

In the HTML code of DisplayRecords.aspx, add the following code to create a simple table to display the records:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DisplayRecords.aspx.cs" Inherits="YourProjectName.DisplayRecords" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Display Records</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h2>Student Records</h2>
            <table border="1">
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Email</th>
                </tr>
                <%-- Table rows will be added here dynamically from code-behind --%>
            </table>
        </div>
    </form>
</body>
</html>

 

Then, in the code-behind file (DisplayRecords.aspx.cs), add the following code to fetch the records from the Access database and display them in the HTML table:

using System;
using System.Data.OleDb;
using System.Web.UI.WebControls;
namespace YourProjectName
{
    public partial class DisplayRecords : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\YourDatabase.accdb;Persist Security Info=False;";
            OleDbConnection conn = new OleDbConnection(connectionString);
            conn.Open();
            OleDbCommand cmd = new OleDbCommand("SELECT * FROM Students", conn);
            OleDbDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                TableRow row = new TableRow();
                TableCell idCell = new TableCell();
                TableCell nameCell = new TableCell();
                TableCell emailCell = new TableCell();
                idCell.Text = reader["ID"].ToString();
                nameCell.Text = reader["Name"].ToString();
                emailCell.Text = reader["Email"].ToString();
                row.Cells.Add(idCell);
                row.Cells.Add(nameCell);
                row.Cells.Add(emailCell);
                tblStudents.Rows.Add(row);
            }
            reader.Close();
            conn.Close();
        }
    }
}

In the code above, we first create a connection to the Access database using the OleDbConnection class and open the connection. Then, we execute a SELECT query to fetch all records from the "Students" table and read the data using the OleDbDataReader class.

We then iterate through the records using a while loop and create a new table row for each record. Inside the loop, we create a new table cell for each field in the record, set its text to the value of the corresponding field, and add it to the table row.

Finally, we add the table row to the table in the HTML code using the Rows.Add() method.

Note that in the HTML code, we have left a comment where the table rows will be added dynamically from the code-behind.

You can customize the code above according to your specific needs and database schema.