In this article I will explain with an example, how to return
Output parameter from
Stored Procedure in ASP.Net MVC.
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;
using System.Configuration;
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 Web.Config 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
ViewBag object.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(FruitModel fruit)
{
string spName = "GetFruitName";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
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 Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – 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 TextBox created using
Html.TextBoxFor method, a
Submit button and a
ViewBag object to display the Fruit Name.
@model OutputParameter_MVC.Models.FruitModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<span>Enter Fruit Id:</span>
@Html.TextBoxFor(m => m.FruitId)
<br/>
<input type="submit" value="Submit" />
<br />
<br />
<span>@ViewBag.Message</span>
}
</body>
</html>
Screenshot
Downloads