views:

224

answers:

5

With the following Stored Procedure in SQL Server 2005

IF OBJECT_ID('[dbo].[UserProfile]') IS NOT NULL

DROP PROCEDURE [dbo].[UserProfile]  

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE [dbo].[UserProfile] 
(      
      @UserId VARCHAR(20)
)

AS

IF @UserId = 'userId'
BEGIN
SELECT @UserId = 'Yes'
END

ELSE
SELECT @UserId = 'No'

  SELECT @UserId AS RESULT 

  RETURN RESULT

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

C# code, needs to obtain the result of the stored procedure

public String UserProfile(string userId)
{
   String result;
   System.Data.Common.DbCommand cmd = this.mConn.CreateCommand();

   try
   {

       cmd.CommandTimeout = this.mCmdTimeout;
       cmd.CommandType = CommandType.StoredProcedure;
       cmd.CommandText = "UserProfile";

       DbParameter p = cmd.CreateParameter();

       p.ParameterName = "@UserId";
       p.DbType = DbType.String;
       p.Value = userId;

       cmd.Parameters.Add(p);

       cmd.Connection.Open();
       cmd.ExecuteNonQuery();
       cmd.Connection.Close();

       //Need to assign the value of the stored procedure's return to result
       result = Convert.ToString(cmd.Parameters["@UserId"].Value);
   }
   catch (Exception ex)
   {
       throw;
   }
   finally
   {
       cmd.Connection.Close();
   }

   return result;
}

How do I return the result of my stored procedure in C#?

+7  A: 

In this case, you can use the ExecuteScalar() method of Command to return the value. ExecuteScalar will look at the first column of the first record returned from the database.

Ryan Brunner
simple stuff, that's what I was looking for. Also you must cast to the type you want, in this case it is a String.
Chris Ballance
Yes, this is by far the simplest answer.
AJ
A: 

Using...?

Linq2Entities? Drag the sproc into the designer and it will be a function in c# returning the correct value. Linq2Sql? Drag the sproc into the designer and it will be a function in c# returning the correct value. ADO? - just assign the result of ExecuteScalar to a variable

Russell Steen
@Russell, that is what Tags are for, they clearly state he is using SQL C# and its a sproc within SQL.
JonH
JonH - Those are all valid ways to do it IN C# with SQL.
Russell Steen
+1  A: 

Or you could of used an Output parameter.

JonH
+3  A: 

Try adding this to the parameter's properties before executing the command:

cmd.Parameters["@UserId"].Direction = System.Data.ParameterDirection.InputOutput;

**Edit: **
As Ryan Brunner stated in comments, you must mark your parameter as OUT in the stored procedure for this to work.

AJ
In order for this to work @UserID needs to be marked as an output parameter in the stored proc as well.
Ryan Brunner
Ah yes, you're right. @Chris Ballance, heed Ryan's advice if you take this approach! I will edit.
AJ
+1 for your post and for the community making your post better
Chris Ballance
+3  A: 

You can use an Output parameter as well.

In storedprocedure:

CREATE PROCEDURE [dbo].[UserProfile] 
(      
      @UserId VARCHAR(20) Output
)

In code:

p.ParameterName = "@UserId";
p.DbType = DbType.String;
p.Direction = System.Data.ParameterDirection.Output;
p.Value = userId;
Mehdi Golchin