In this article I will explain with an example, how to use SqlDataReader in ASP.Net Core Razor Pages.
The records from the Database table will be read using SqlDataReader and then copied into the Generic List collection of class objects, which is later used for populating HTML Table in ASP.Net Core Razor Pages.
Note: For beginners in ASP.Net Core Razor Pages, please refer my article ASP.Net Core Razor Pages: Hello World Tutorial with Sample Program example.
I have made use of the following table Customers with the schema as follows.
Using SqlDataReader in ASP.Net Core Razor Pages
I have already inserted few records in the table.
Using SqlDataReader in ASP.Net Core Razor Pages
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
You will need to import the following namespace.
using System.Data.SqlClient;
The Model class consists of the following three properties.
public class CustomerModel
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
Razor PageModel (Code-Behind)
Inside the PageModel class, a property of type Generic List collection is defined.
The PageModel consists of following Handler method.
Handler method for handling GET operation
Inside this Handler method, the records are fetched from the Customers Table using SqlDataReader and then using WHILE Loop, the records are copied into the Generic List collection of CustomerModel public property.
public class IndexModel : PageModel
    public List<CustomerModel> Customers { get; set; }
    public void OnGet()
        string constr = @"Data Source=.\SQL2019;Initial Catalog=AjaxSamples;Integrated Security=true";
        using (SqlConnection con = new SqlConnection(constr))
            string query = "SELECT CustomerId, Name, Country FROM Customers";
            using (SqlCommand cmd = new SqlCommand(query, con))
                using (SqlDataReader sdr = cmd.ExecuteReader())
                    Customers = new List<CustomerModel>();
                    while (sdr.Read())
                        Customers.Add(new CustomerModel
                            CustomerId = int.Parse(sdr["CustomerId"].ToString()),
                            Name = sdr["Name"].ToString(),
                            Country = sdr["Country"].ToString()
Razor Page (HTML)
Inside the Razor Page, an HTML Table is used for displaying the records.
The public property of Generic List object of the CustomerModel class is accessed through PageModel inside the Razor Page.
Then, a loop will be executed over the Model which will generate the HTML Table rows with the Customer records.
@model SqlDataReader_Razor_Core.Pages.IndexModel
@using SqlDataReader_Razor_Core.Models;
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width" />
    <table cellpadding="0" cellspacing="0">
        @foreach (CustomerModel customer in Model.Customers)
Using SqlDataReader in ASP.Net Core Razor Pages