In this article I will explain with an example, how to return Output parameter from Stored Procedure in ASP.Net Core MVC.
Note: For beginners in ASP.Net Core 7, please refer my article ASP.Net Core 7: Hello World Tutorial with Sample Program example.
 
 

Database

I have made use of the following table Fruits with the schema as follows.
Return Output parameter from Stored Procedure in ASP.Net Core
 
I have already inserted few records in the table.
Return Output parameter from Stored Procedure in ASP.Net Core
 
Note: You can download the database table SQL by clicking the download link below.
           Download SQL file
 
 

Stored Procedure

The SQL Server Stored Procedure accepts the following two Parameters.
1. FruitId – This is an INPUT Parameter used to pass the Id of the Fruit.
2. FruitName – This is an OUTPUT Parameter used to fetch the Name of the Fruit based on its FruitId.
Note: Output Parameter is identified by the keyword OUTPUT.
 
CREATE PROCEDURE [GetFruitName]
      @FruitId INT,
      @FruitName VARCHAR(30) OUTPUT
AS
BEGIN
      SET NOCOUNT ON;
 
      SELECT @FruitName = FruitName
      FROM Fruits
      WHERE FruitId = @FruitId
END
 
 

Model

The Model class consists of following properties.
public class FruitModel
{
    public int FruitId { get; set; }
    public string FruitName { get; set; }
}
 
 

Namespaces

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

Controller

The Controller consists of following Action methods.

Action method for handling GET operation

Inside this Action method, simply the View is returned.
 

Action method for handling POST operation

This Action method accepts FruitModel class object as a parameter.
Inside this Action method, first the connection string is read from the ConnectionStrings section of the AppSettings.json file.
The SqlCommand class object is created and FruitId entered in the TextBox is added as a parameter
Then, the second parameter FruitName is added which is an Output parameter hence it cannot be added using AddWithValue function hence it is added using the Add method of SqlCommand class with its Data Type and Size specified.
Once the FruitName parameter is added, its Direction is set to Output since by default the Direction of all parameters are Input.
Then, ExecuteNonQuery function of SqlCommand class is called and the FruitName is fetched using Value property from the Output parameter.
Note: For more details on ExecuteNonQuery method, please refer my article Understanding SqlCommand ExecuteNonQuery in C# and VB.Net.
 
Finally, the Fruit Name is set into a ViewBag object.
public class HomeController : Controller
{
    public IConfiguration Configuration { get; set; }
 
    public HomeController(IConfiguration _configuration)
    {
        this.Configuration = _configuration;
    }
    public IActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public ActionResult Index(FruitModel fruit)
    {
        string spName = "GetFruitName";
        string constr = this.Configuration.GetSection("ConnectionStrings")["MyConn"];
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(spName, con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                // Adding Input parameter.
                cmd.Parameters.AddWithValue("@FruitId", fruit.FruitId);
                // Adding Output parameter.
                cmd.Parameters.Add("@FruitName", SqlDbType.VarChar, 30);
                cmd.Parameters["@FruitName"].Direction = ParameterDirection.Output;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                string fruitName = cmd.Parameters["@FruitName"].Value.ToString();
                ViewBag.Message = "Fruit Name: " + fruitName;
            }
        }
        return View(fruit);
    }
}
 
 

View

Inside the View, in the very first line the FruitModel class is declared as Model for the View.
The View consists of an HTML Form which has been created using the following TagHelpers attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The HTML of the View consists of an HTML INPUT TextBox, a Submit Button and a ViewBag object to display the Fruit Name.
@model OutputParameter_Core.Models.FruitModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
     <form method="post" asp-controller="Home" asp-action="Index">
          Enter Fruit Id:
          <input type="text" asp-for="FruitId" />
          <br/>
          <input type="submit" value="Submit" />
          <br />
          <br />
          <span>@ViewBag.Message</span>
    </form>
</body>
</html>
 
 

Screenshot

Return Output parameter from Stored Procedure in ASP.Net Core
 
 

Downloads