Click here to Skip to main content
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
gwyder491

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 295
1 OriginalGriff 274
2 Shweta N Mishra 216
3 PIEBALDconsult 184
4 CPallini 155
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 1 Feb 2013
Copyright © CodeProject, 1999-2014
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