In this article I will explain with an example, how to return
Output parameter from
Stored Procedure in ASP.Net Core Razor Pages.
Database
I have made use of the following table Fruits with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Stored Procedure
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;
Razor PageModel (Code-Behind)
The PageModel consists of following Handler method.
Handler method for handling GET operation
This Handler method left empty as it is not required.
Handler method for handling POST operation
This Handler method accepts FruitModel class object as parameter.
Inside this Handler 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.
Finally, the Fruit Name is set into a
ViewData object.
public class IndexModel : PageModel
{
public FruitModel Fruit { get; set; }
public IConfiguration Configuration { get; set; }
public IndexModel(IConfiguration configuration)
{
this.Configuration = configuration;
}
public void OnGet()
{
}
public void OnPostSubmit(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();
ViewData["Message"] = "Fruit Name: " + fruitName;
}
}
}
}
Razor Page (HTML)
Inside the Razor Page, the ASP.Net TagHelpers is inherited.
The HTML of the Razor Page consists of an HTML INPUT TextBox, a
Submit Button and a
ViewData object to display the Fruit Name.
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.
@page
@model OutputParameter_Core_Razor.Pages.IndexModel
@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">
Enter Fruit Id:
<input type="text" asp-for="Fruit.FruitId" />
<br/>
<input type="submit" value="Submit" asp-page-handler="Submit" />
<br/>
<br/>
<span>@ViewData["Message"]</span>
</form>
</body>
</html>
Screenshot
Downloads