Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Stored procedures with output parameters


Stored procedures with output parameters

Author
Message
dobinf
dobinf
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 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
Dugi
Dugi
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1780 Visits: 3511
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/
dobinf
dobinf
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 14
ok I am new to this site. Can you please solve my problem?
Dugi
Dugi
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1780 Visits: 3511
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!
Cool

============================================================
SELECT YOUR PROBLEM FROM SSC.com WHERE PROBLEM DESCRIPTION =
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Matt Miller (4)
Matt Miller (4)
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10467 Visits: 18487
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?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search