Return Output parameter from Stored Procedure using ExecuteSqlCommand in ASP.Net Core MVC

kas12345
 
on Jul 12, 2021 11:11 PM
463 Views

Sir

how to define output parameter for stored procedure in ExecuteSqlCommand in asp.net core

public void InsertCustomer(CustomerModel customer)
{
    SqlParameter pId = new SqlParameter("@CustomerId", customer.CustomerId);
    SqlParameter pName = new SqlParameter("@Name", customer.Name);
    SqlParameter pCountry = new SqlParameter("@Country", customer.Country);
    this.Database.ExecuteSqlCommand("EXECUTE Customers_InsertCustomer @CustomerId,@Name,@Country", pId, pName, pCountry);
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jul 12, 2021 11:17 PM

Hi kas12345,

Refer below code.

public string InsertCustomer(CustomerModel customer)
{
    SqlParameter pId = new SqlParameter("@CustomerId", customer.CustomerId);
    SqlParameter pName = new SqlParameter("@Name", customer.Name);
    SqlParameter pCountry = new SqlParameter("@Country", customer.Country);
    var pOutId = new SqlParameter
    {
        ParameterName = "Id",
        DbType = System.Data.DbType.String,
        Size = 100,
        Direction = System.Data.ParameterDirection.Output
    };
    var result = this.Database.ExecuteSqlCommand("EXECUTE Customers_InsertCustomer @CustomerId,@Name,@Country,@Id", pId, pName, pCountry, pOutId);
    return Convert.ToString(result.Value);
}