With the output argument, run SQLCommand.

asp.net-mvc asp.net-mvc-3 entity-framework

Question

I'm using Entity Framework in an ASP.NET MVC3 application and I'm trying to use the following code:

var token = "";
this.Database.ExecuteSqlCommand("exec dbo.MyUsp", new SqlParameter("token", token));

My stored proc signature is:

CREATE PROCEDURE MyUSP(@token varchar(10) OUT)
(...)

When I use this code I get an error saying that parameter "@token" was expected but not supplied.

How do I tell EF that the token parameter is for output?

1
19
7/28/2011 3:40:36 PM

Accepted Answer

I ended up using this to get it working, but I'm sure there's a more optimal way:

var p = new SqlParameter
{
    ParameterName = "token",
    DbType = System.Data.DbType.String,
    Size = 100,
    Direction = System.Data.ParameterDirection.Output
};
var resp = this.Database.SqlQuery<String>("exec dbo.usp_GetRequestToken @token", p);

return resp.First();
25
7/7/2015 3:20:36 PM

Popular Answer

var outParam = new SqlParameter();
outParam.ParameterName = "OutPutParametname";
outParam.SqlDbType = SqlDbType.Bit;//DataType Of OutPut Parameter
outParam.Direction = ParameterDirection.Output;
db.Database.ExecuteSqlCommand("EXEC ProcedureName @Param1,@Param2 OUTPUT", new SqlParameter("Param1", value), outParam);
object outParamValue = Convert.ToBoolean(outParam.Value);


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow