Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello

This is one quick question about EF and SQL stored procedures, although it doesn't bother me much I would like to know the answer.

Is it possible to get return value and output value in EF using stored procedure.

SQL
CREATE PROCEDURE [dbo].[CheckUser] 
	@UserName varchar(8),
	@Password varchar(12),
        @User_ID int OUTPUT
AS
BEGIN
	SELECT @User_ID = User_ID FROM [User] WHERE UserName = @UserName AND Password = @Password
	SELECT @User_ID
	
	IF @User_ID IS NOT NULL
		RETURN 0
	
	DECLARE @TempUser_ID_1 int
	DECLARE @TempUser_ID_2 int
	
	SELECT @TempUser_ID_1 = User_ID FROM [User] WHERE UserName = @UserName
	SELECT @TempUser_ID_2 = User_ID FROM [User] WHERE Password = @Password
	
	IF @TempUser_ID_1 IS NULL AND @TempUser_ID_2 IS NULL
		RETURN -1
			 
	IF @TempUser_ID_1 IS NULL
		RETURN -2
		
	IF @TempUser_ID_2 IS NULL
		RETURN -3					 
END


When I run this procedure in MSSMS it runs fine and give me the results I want, but when I try to run it through EF I got this error:

The data reader returned by the store data provider does not have enough columns for the query requested.


I set the return value of imported function to int32 and as I understand that's where the problem lays.
When I set the return value to none I get good output value but the return is always -1, hence the function doesn't return anything.

I did found a solution and now I don't use an output parameter, but I'm still wandering is it possible to get both since I believe it's not, but not sure, I do need Your opinions to clear my doubts.

Thanks , Mirza
Posted

1 solution

There is a solution when setting return value to none. You can use select.

Try this

SQL
CREATE PROCEDURE [dbo].[CheckUser] 
	@UserName varchar(8),
	@Password varchar(12),
        @User_ID int OUTPUT
AS
BEGIN
        DECLARE @ret int
	SELECT @User_ID = User_ID FROM [User] WHERE UserName = @UserName AND Password = @Password
	SELECT @User_ID
	
	IF @User_ID IS NOT NULL
		select @ret=0
	
	DECLARE @TempUser_ID_1 int
	DECLARE @TempUser_ID_2 int
	
	SELECT @TempUser_ID_1 = User_ID FROM [User] WHERE UserName = @UserName
	SELECT @TempUser_ID_2 = User_ID FROM [User] WHERE Password = @Password
	
	IF @TempUser_ID_1 IS NULL AND @TempUser_ID_2 IS NULL
		select @ret=-1
			 
	IF @TempUser_ID_1 IS NULL
		select @ret=-2
		
	IF @TempUser_ID_2 IS NULL
		select @ret=-3		
select @ret		 
END
 
Share this answer
 
v2
Comments
gwyder 1-Feb-13 5:31am    
Thanks for the answer, but I am still interested is it possible to use return value and output parameters together.
Santhosh Kumar Jayaraman 1-Feb-13 5:32am    
Not with EF
gwyder 1-Feb-13 5:50am    
Thank you very much :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900