Entity Framework: how do I run a stored procedure and return a value?

c# entity-framework linq stored-procedures

Question

Is it possible to execute a stored procedure using the Entity Framework, passing in variables and returning data along with a return value?

Please do not leave any code about function mappings with Entity Framework.

I have tried this and I keep getting the same damn error:

Procedure or function 'usp_BusinessUser_Search' expects parameter '@TotalRecords', which was not supplied.

Here's my code:

SqlParameter Business = new SqlParameter("Business", Search.Term);
SqlParameter Location = new SqlParameter("Location", Search.Location);
SqlParameter PageNumber = new SqlParameter("PageNumber", Search.CurrentPage);
SqlParameter RecordsPerPage = new SqlParameter("RecordsPerPage", Search.RecordsPerPage);
SqlParameter TotalRecords = new SqlParameter("TotalRecords", 0);

SqlParameter parm = new SqlParameter()
{
    ParameterName = "@TotalRecords",
    Value = 0,
    SqlDbType = SqlDbType.Int,
    Direction = System.Data.ParameterDirection.Output
};

var List = db.ExecuteStoreQuery<ENT_SearchBusinessResult>("exec usp_BusinessUser_Search",Business,Location,PageNumber,RecordsPerPage,parm); 

Does anyone have any idea what is causing this?

Thanks

EDIT:

Stored procedure code:

ALTER PROCEDURE [dbo].[usp_BusinessUser_Search] (   @Business nVarChar(255) = NULL
                                                    , @Location nVarChar(255) = NULL
                                                    , @PageNumber Int = 1
                                                    , @RecordsPerPage Int = 10
                                                    , @TotalRecords Int OUTPUT)
AS 
BEGIN
    SET NOCOUNT ON;

    DECLARE @SQL NVARCHAR(MAX)
            , @CacheTable SYSNAME
            , @TotalRows Int
            , @Category VarChar(255)
            , @BusinessCategory Int 
            , @TownCounty VarChar(50)
            , @PcodeTownCounty VarChar(50)

    INSERT  Voucher_SearchHistory (Keyword, Location)
    SELECT  NULLIF(@Business,''), NULLIF(@Location,'')
1
3
8/2/2012 2:51:47 PM

Accepted Answer

3
8/2/2012 2:25:08 PM

Popular Answer

You are not passing TotalRecords Sql Parameter in Excecute

var List = db.ExecuteStoreQuery<ENT_SearchBusinessResult>(
"exec usp_BusinessUser_Search",Business,
Location,PageNumber,RecordsPerPage,parm);


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