Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Stored procedures with output parameters Expand / Collapse
Author
Message
Posted Monday, December 29, 2008 5:10 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Yesterday @ 4:02 AM
Points: 7, Visits: 14
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
Post #626488
Posted Monday, December 29, 2008 6:24 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 2:32 AM
Points: 1,260, Visits: 3,423
Don't do double posts!

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/
Post #626536
Posted Monday, December 29, 2008 6:38 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Yesterday @ 4:02 AM
Points: 7, Visits: 14
ok I am new to this site. Can you please solve my problem?
Post #626546
Posted Monday, December 29, 2008 6:44 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 2:32 AM
Points: 1,260, Visits: 3,423
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/
Post #626551
Posted Monday, December 29, 2008 7:41 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:59 PM
Points: 7,064, Visits: 15,278
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?
Post #626560
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse