In this article I will explain with an example, how to call MySQL Stored Procedure with Parameters in ASP.Net Core Razor Pages.
The article explains with a simple example where a Stored Procedure will be passed with a parameter and it returns the matching records. In similar way one can pass multiple parameters.
Note: For beginners in ASP.Net Core Razor Pages(.Net Core 7), please refer my article ASP.Net Core 7 Razor Pages: Hello World Tutorial with Sample Program example.
 
 

Download and Install the MySQL Connector

You will need to download and install the MySQL Connector in order to connect to the MySQL database in ASP.Net Core.
Note: For details on how to download and install the MySQL Connector, please refer my article Download, install and reference MySQL Connector in ASP.Net Core.
 
 

Database

I have made use of the following table Customers with the schema as follows.
ASP.Net Core Razor Pages: Call MySql Stored Procedure with Parameters
 
I have already inserted few records in the table.
ASP.Net Core Razor Pages: Call MySql Stored Procedure with Parameters
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

Stored Procedure

The following Stored Procedure accepts a parameter custId Integer parameter and is matched with the CustomerId field of the Customers Table of MySQL database.
Then, the matching record is returned from Stored Procedure.
DELIMETER //
CREATE PROCEDURE Customers_GetCustomer(IN custId INT)
BEGIN
    SELECT Name
          ,Country
    FROM Customers
    WHERE CustomerId = custId;
END //
DELIMITER ;
 
 

Model

The Model class consists of following properties.
public class CustomerModel
{
    public int CustomerId { getset; }
    public string Name { getset; }
    public string Country { getset; }
}
 
 

Namespaces

You will need to import the following namespaces.
using System.Data;
using MySql.Data.MySqlClient;
 
 

Razor PageModel (Code-Behind)

The PageModel consists of the following Handler method.

Handler method for handling Home GET operation

This Handler method is left empty as it is not required.
 

Handler method for handling Post operation

This Handler method accepts TextBox value i.e. CustomerId as parameter.
An object of CustomerModel class is created and the connection is read from the ConnectionStrings section of the AppSettings.json file.
Note: For more details on how to read Connection String from AppSettings.json, please refer my article .Net Core 7: Read Connection String from AppSettings.json file.
 
An object of MySqlCommand class is created and CustomerId is added parameter and using ExecuteReader the records are fetched from the MySQL database.
Note: For more details on ExecuteReader, please refer my article Using MySqlCommand ExecuteReader Example in ASP.Net with C# and VB.Net.
 
Finally, the CustomerModel class object is set to the public property of CustomerModel class.
public class IndexModel : PageModel
{
    public IConfiguration Configuration { get; set; }
 
    public IndexModel(IConfiguration _configuration)
    {
        this.Configuration = _configuration;
    }
    public CustomerModel Customer { get; set; }
    public void OnGet()
    {
    }
    public void OnPostSearch(int customerId)
    {
        CustomerModel customer = new CustomerModel();
        string constr = this.Configuration.GetSection("ConnectionStrings")["MyConn"];
        string spName = "Customers_GetCustomerById";
        using (MySqlConnection con = new MySqlConnection(constr))
        {
            using (MySqlCommand cmd = new MySqlCommand(spName, con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@custId", customerId);
                con.Open();
                using (MySqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        customer = (new CustomerModel
                        {
                            Name = sdr["Name"].ToString(),
                            Country = sdr["Country"].ToString()
                        });
                    }
                }
                con.Close();
            }
        }
        this.Customer = customer;
    }
}
 
 

Razor Page (HTML)

HTML Markup

The HTML of Razor Page also consists of an HTML TextBox created and a Submit button.
The Submit Button has been set with the POST Handler method using the asp-page-handler attribute.
Note: In the Razor PageModel, the Handler method name is OnPostSubmit but here it will be specified as Submit when calling from the Razor HTML Page.
 

Submitting the Form

When the Submit button is clicked then, the Model is checked for NULL and if it is not NULL then the fetched records are displayed in HTML Table.
@page
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model MySQL_Call_SP_Core_Razor.Pages.IndexModel
@using MySQL_Call_SP_Core_Razor.Models;
@{
    Layout = null;
}
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post">
        <span>Search:</span>
        <input type="text" name="customerId" />
        <input type="submit" value="Search" asp-page-handler="Search" />
        <hr />
        @if (Model.Customer != null)
        {
            <table cellpadding="0" cellspacing="0">
                <tr>
                    <th>Name</th>
                    <th>Country</th>
                </tr>
                <tr>
                    <td>@Model.Customer.Name</td>
                    <td>@Model.Customer.Country</td>
                </tr>
            </table>
        }
    </form>
</body>
</html>
 
 

Screenshot

ASP.Net Core Razor Pages: Call MySql Stored Procedure with Parameters
 
 

Demo

 
 

Downloads