Click here to Skip to main content
11,416,787 members (68,699 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL EF StoredProcedures
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.

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 31-Jan-13 22:32pm
gwyder511

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

Try this

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
  Permalink  
v2
Comments
gwyder at 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 J at 1-Feb-13 5:32am
   
Not with EF
gwyder at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,270
1 OriginalGriff 7,512
2 Maciej Los 3,710
3 Abhinav S 3,324
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web03 | 2.8.150427.4 | Last Updated 1 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100