Link to home
Create AccountLog in
Avatar of jamesdean666
jamesdean666

asked on

C# and Return Stored Procedure result

I have the following Stored Procedure in SQL Server 2005:

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

DROP PROCEDURE [dbo].[spBct_PhnxMrtgee_UserProfile]  

GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

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

AS


IF @UserId = 'JDIBIASE' OR @UserId = 'HENKV' OR @UserId = 'CHARMENE' OR @UserId = 'JEFF'
BEGIN
SELECT @UserId = 'Y'
END

ELSE
SELECT @UserId = 'N'

  SELECT @UserId AS RESULT

  RETURN RESULT

GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO


I have the following code in my Data Layer in a C# VS 2008 Project, and I am trying to return the result of the above 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 = "spBct_PhnxMrtgee_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();


THIS IS WHAT I AM TRYING TO FIGURE OUT:

                result = Convert.ToString(cmd.Parameters["@UserId"].Value);


               
            }
            catch (Exception ex)
            {
                log.ErrorException(System.Reflection.MethodBase.GetCurrentMethod().Name, ex);

                throw;

            }
            finally
            {
                cmd.Connection.Close();
            }

            return result;
        }


How can I update my C# code to return the result of my stored procedure?

Thanks,

JD
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of jamesdean666
jamesdean666

ASKER

Thanks.