Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stored Procedure return values with linq data context

I am trying to access the return value of a stored procedure with Linq

DECLARE @ValidToken int = 0 //I have also tried using a bit instead of an int here.

IF EXISTS(SELECT 1 FROM Tests WHERE TestToken = @Token)
    select @ValidToken = 1

return @ValidToken

This works when running the SP through sql studio. However I am trying to run it with linq using the datacontext class and it is always returning -1.

using (DataEntities dataEntities = new DataEntities())
    {
        int query = data.ValidateToken(id);
        Response.Write(query.ToString());
    }

query will always equal -1 and I am not sure why.

I have looked online and it would appear that there are easier ways to get the return value however I would rather stick to Linq as that is what the rest of the program is using.

like image 448
Jamesla Avatar asked Apr 17 '13 11:04

Jamesla


1 Answers

Why are you all using a stored procedure as function?

Though a stored procedure has return, it is not a function, so you should not use return to return data,

Rather, you should use Output parameters and other ways to return data, as documented at MSDN

And the use of return is documented at MSDN

misusing return is probably the reason why LINQ does not understand your stored procedures.

like image 183
ZZZ Avatar answered Oct 01 '22 14:10

ZZZ