I have some entity framework code to run a stored procedure which returns a parameter. Every time I run the code the parameter comes back as null. Does anyone have any ideas what could be causing this?
Thanks
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);
var TotalRecords = new SqlParameter
{
ParameterName = "TotalRecords",
Value = 0,
Direction = ParameterDirection.Output
};
var List = db.ExecuteStoreQuery<ENT_SearchBusinessResult>("exec usp_BusinessUser_Search @Business,@Location,@PageNumber,@RecordsPerPage,@TotalRecords out", Business, Location, PageNumber, RecordsPerPage, TotalRecords);
I used Sql profiler and found it was doing the following:
declare @p7 int
set @p7=53
exec sp_executesql N'exec usp_BusinessUser_Search @Business,
@Location,@PageNumber,@RecordsPerPage,
@TotalRecords out',
N'@Business nvarchar(14),@Location nvarchar(14),
@PageNumber int,
@RecordsPerPage int,@TotalRecords int output',
@Business=N'Food
and Drink',@Location=N'United Kingdom',@PageNumber=1,@RecordsPerPage=10,
@TotalRecords=@p7 output
select @p7
this is fine, it shows the return value if you run this query but is not returning back to my code :(
This article says that you need to read all the results before trying to access the value of an out parameter. Your code isn't complete enough to tell whether you are doing this (try ToList() if you are not.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With