Stored procedures with output parameters

  • Hello everyone,

    I am using a stored procedure with output parameters, But i get the value of the output parameter as Zero. I have checked in query analyser and i get a value greater than zero,but in code i get the value as zero, Can anyone help me. the code is as follows

    conn.Open();

    SqlDataReader dr;

    SqlCommand cmd = new SqlCommand("GetAllImages", conn);

    cmd.CommandType = CommandType.StoredProcedure;

    //Create a SqlParameter object to hold the output parameter value

    SqlParameter img_count = new SqlParameter("@img_count",SqlDbType.Int);

    //IMPORTANT - must set Direction as Output

    img_count.Direction = ParameterDirection.Output;

    cmd.Parameters.Add(img_count);

    dr = cmd.ExecuteReader();

    int intCountOfImages = Convert.ToInt32(img_count.Value);

    The SQL statement is as follows:

    set ANSI_NULLS ON

    set QUOTED_IDENTIFIER ON

    go

    ALTER PROCEDURE [dbo].[GetAllImages]

    @img_count int OUTPUT

    AS

    BEGIN

    SET NOCOUNT ON;

    SET @img_count=(Select count(img_id) from images);

    SELECT img_name,img_path from images;

    END

    ANy suggestions would be greatly appreciated, Thankyou in Advance

    Regards,

    Judef

  • Don't do double posts! :w00t:

    The original is here :

    Steve it should be closed!

    ============================================================
    SELECT YOUR PROBLEM FROM SSC.com WHERE PROBLEM DESCRIPTION =
    http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

  • ok I am new to this site. Can you please solve my problem?

  • dobinf (12/29/2008)


    ok I am new to this site. Can you please solve my problem?

    Sure that you are new here and you are welcome ... but please read carefully all the sections in board and do the posts in right sections!

    Someone will respond to your problem as soon as possible!

    Success!

    😎

    ============================================================
    SELECT YOUR PROBLEM FROM SSC.com WHERE PROBLEM DESCRIPTION =
    http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

  • ExecuteReader is a little funky when dealing with output parameters. The output parameters are available only AFTER the recordset is used and closed. From MSDN:

    Using a DataReader to Process the RETURN Value and OUTPUT Parameters

    This same query can be executed directly with the Command.ExecuteReader (or ExecuteNonQuery if you don't have a rowset to process), but there are several other steps you'll need to take to process the returned values. Remember, you'll have to complete processing for all rowsets before attempting to capture the Return value or OUTPUT parameters. The following code shows how to use the ExecuteReader and a loop to process the rowsets, and then capture the Return value and OUTPUT parameters. You'll find that OUTPUT parameters (even a lot of them) can be handled far faster than even a single row of data returned by a SELECT

    You may be better off using a dataset instead - the output parameters handling is more straightforward.

    http://msdn.microsoft.com/en-us/library/ms971497.aspx#gazoutas_topic4

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply