4

I have a stored procedure in SQL Server :

CREATE PROCEDURE [dbo].[GET_AVAILABLE_PLACES] 
-- Add the parameters for the stored procedure here
@eventGuid uniqueidentifier,
@placeGuid uniqueidentifier,     
@dateGuid dateTime 
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
DECLARE @ReservedPlaces int;
DECLARE @TotalPlaces int;    
SELECT @ReservedPlaces = RESERVED_PLACES FROM dbo.EVENT_DATE_PLACE
WHERE EVENT_GUID = @eventGuid 
and DATE_BEGIN = @dateGuid
and PLACE_GUID = @placeGuid    
SELECT @TotalPlaces = NUMBER_PLACES FROM dbo.PLACES
WHERE GUID = @placeGuid    
RETURN @TotalPlaces - @ReservedPlaces;     
END

But I can not seem to read the result returned

private int SelectByStoredProcedureGetAvailablePlaces(string entryParam1, string entryParam2, DateTime entryParam3)
{
        int results;
        //PlanningElement plan = GetPlanningElement(entryParam1, entryParam2, entryParam3.ToString(), "31/12/2012 00:00:00", "150");

        using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;

            sqlCommand.CommandText = "GET_AVAILABLE_PLACES";
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Parameters.AddWithValue("eventGuid", entryParam1);
            sqlCommand.Parameters.AddWithValue("placeGuid", entryParam2);                
            sqlCommand.Parameters.AddWithValue("dateGuid", entryParam3);

            sqlConnection.Open();

            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            results = sqlDataReader.GetInt32(0);

            sqlConnection.Close();
        }

        return results;
    }

What is the problem ?

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
artoon
  • 729
  • 2
  • 14
  • 41
  • 1
    The reader reads rows, not return codes. Your existing code will work if you replace the `RETURN @TotalPlaces - @ReservedPlaces;` with `SELECT @TotalPlaces - @ReservedPlaces;` – Preet Sangha Feb 18 '13 at 20:11
  • 1
    http://stackoverflow.com/questions/749622/how-to-get-return-value-of-a-stored-procedure shows alternatives – Preet Sangha Feb 18 '13 at 20:18

5 Answers5

2

The GetInt32 method will read from a selected table output. You want a return value, so you can change the code to be

SqlParameter returnValueParam = sqlcomm.Parameters.Add("@ReturnValue", SqlDbType.Int);
returnValueParam.Direction = ParameterDirection.ReturnValue;
sqlCommand.Parameters.Add(returnValueParam);
...
sqlCommand.ExecuteNonQuery();
result = returnValueParam.Value;
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
1

You can alter your stored procedure, replace

RETURN @TotalPlaces - @ReservedPlaces;

By:

SELECT (@TotalPlaces - @ReservedPlaces) AS [AvailablePlaces]
RETURN;

Your can also get a return value from your stored procedure, but that requires some more modifications. See this question for more information.

Community
  • 1
  • 1
Jacco
  • 3,251
  • 1
  • 19
  • 29
0

That particular type of return value must be read by using an additional parameter in the SqlCommand.Parameters collection with direction of System.Data.ParameterDirection.ReturnValue

To read it as you are attempting, your SQL procedure would need to do:

SELECT @TotalPlaces - @ReservedPlaces As MyResult

Then the result will be returned as a resultset instead of a return value.

maelstrom
  • 1,111
  • 8
  • 7
0

So my stored procedure must be as this ?

    CREATE PROCEDURE [dbo].[GET_AVAILABLE_PLACES] 
-- Add the parameters for the stored procedure here
@eventGuid uniqueidentifier,
@placeGuid uniqueidentifier,     
@dateGuid dateTime 
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
DECLARE @ReservedPlaces int;
DECLARE @TotalPlaces int;    
SELECT @ReservedPlaces = RESERVED_PLACES FROM dbo.EVENT_DATE_PLACE
WHERE EVENT_GUID = @eventGuid 
and DATE_BEGIN = @dateGuid
and PLACE_GUID = @placeGuid    
SELECT @TotalPlaces = NUMBER_PLACES FROM dbo.PLACES
WHERE GUID = @placeGuid    
SELECT @TotalPlaces - @ReservedPlaces As ReturnValue;   
END

And my function as this ?

private int SelectByStoredProcedureGetAvailablePlaces(string entryParam1, string entryParam2, DateTime entryParam3)
{
        int results;
        //PlanningElement plan = GetPlanningElement(entryParam1, entryParam2, entryParam3.ToString(), "31/12/2012 00:00:00", "150");

        using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;

            sqlCommand.CommandText = "GET_AVAILABLE_PLACES";
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Parameters.AddWithValue("eventGuid", entryParam1);
            sqlCommand.Parameters.AddWithValue("placeGuid", entryParam2);                
            sqlCommand.Parameters.AddWithValue("dateGuid", entryParam3);

            SqlParameter returnValueParam = sqlCommand.Parameters.Add("@ReturnValue", SqlDbType.Int);
            returnValueParam.Direction = ParameterDirection.ReturnValue;
            sqlCommand.Parameters.Add(returnValueParam);

            sqlConnection.Open();

            sqlCommand.ExecuteNonQuery();
            result = returnValueParam.Value;

            sqlConnection.Close();
        }

        return results;
    }
artoon
  • 729
  • 2
  • 14
  • 41
0

Try this in yourth strored procedure.

CREATE PROCEDURE [dbo].[GET_AVAILABLE_PLACES] 
-- Add the parameters for the stored procedure here
@eventGuid uniqueidentifier,
@placeGuid uniqueidentifier,     
@dateGuid dateTime,

@ReservedPlaces int output,
@TotalPlaces int output   

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here

SELECT @ReservedPlaces = RESERVED_PLACES FROM dbo.EVENT_DATE_PLACE
WHERE EVENT_GUID = @eventGuid 
and DATE_BEGIN = @dateGuid
and PLACE_GUID = @placeGuid    
SELECT @TotalPlaces = NUMBER_PLACES FROM dbo.PLACES
WHERE GUID = @placeGuid    
SELECT @TotalPlaces - @ReservedPlaces As ReturnValue;   
END

And this in your programm

private int SelectByStoredProcedureGetAvailablePlaces(string entryParam1, string   entryParam2, DateTime entryParam3)
{
        int results;
        //PlanningElement plan = GetPlanningElement(entryParam1, entryParam2, entryParam3.ToString(), "31/12/2012 00:00:00", "150");

        using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;

            sqlCommand.CommandText = "GET_AVAILABLE_PLACES";
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Parameters.AddWithValue("eventGuid", entryParam1);
            sqlCommand.Parameters.AddWithValue("placeGuid", entryParam2);                
            sqlCommand.Parameters.AddWithValue("dateGuid", entryParam3);

            SqlParameter returnValueParam = sqlCommand.Parameters.Add("@ReturnValue", SqlDbType.Int);
            returnValueParam.Direction = ParameterDirection.Output;

            sqlConnection.Open();

            sqlCommand.ExecuteNonQuery();
            result = returnValueParam.Value;

            sqlConnection.Close();
        }

        return results;
Librit
  • 1