Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 18th, 2008, 05:36 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default SELECT statement nullifying RETURN value

Hi all,

This is a thorny one I've been struggling with for two days. I have some ADO.NET code that executes a sproc which loads a DataReader and evaluates the sproc's RETURN value, or at least it should. I'll post the sproc in a second, but if you look at it, there is a SELECT statement (commented out) right before the RETURN statement. The procedure works fine in SQL Server Express 2005 even uncommented. However, when the procedure is called from my ADO.NET code, the RETURN value is NULL if I uncomment the SELECT statement. With the SELECT statement commented out, the ADO.NET code gets the correct RETURN value. I can't figure out why??? Any thoughts much appreciated.

Here's the ADO.NET:
Code:
public static void TestReturn(string connectionString) 
        {

            using (SqlConnection connection = new SqlConnection(connectionString)) 
            {

                connection.Open();
                using (SqlCommand command = connection.CreateCommand()) 
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = "TestReturn";

                    command.Parameters.Add("@pageCount", SqlDbType.Int);
                    command.Parameters["@pageCount"].Direction = ParameterDirection.ReturnValue;

                    command.Parameters.Add("@pageNum", SqlDbType.Int);
                    command.Parameters["@pageNum"].Value = 1;

                    command.Parameters.Add("@pageSize", SqlDbType.Int);
                    command.Parameters["@pageSize"].Value = 5;

                    using (SqlDataReader reader = command.ExecuteReader()) 
                    {

                        // RETURN value is null with SELECT statement uncommented.
                        int returnValue = (int)command.Parameters["@pageCount"].Value;

                        if (reader.HasRows) 
                        {

                            while (reader.Read()) 
                            {
                                Debug.WriteLine(reader["id"].ToString());
                                Debug.WriteLine(reader["itemdate"].ToString());
                                Debug.WriteLine(reader["title"].ToString());
                            }
                        }
                    }
                }
            }
        }


Here's the sproc:

Code:
ALTER PROCEDURE [dbo].[TestReturn]
(
    @pageNum int,
    @pageSize int
)
AS
    DECLARE @rows INT
    DECLARE @keydate DATETIME
    DECLARE @keyid INT
    DECLARE @rowCount FLOAT 

    IF @pageNum = 1
        BEGIN
            SET @keydate= 0
            SET @keyid=0
        END
    ELSE
        BEGIN
            /* get the values for the date and row */
            SELECT @rows = (@pageNum-1) * @pageSize
            SET ROWCOUNT  @rows
            SELECT @keydate=itemdate, @keyid=id FROM announcements ORDER BY itemdate ASC, id ASC  
        END

    SELECT @rowCount=COUNT(*) FROM announcements

    SET  ROWCOUNT @pageSize

    /*BEGIN
        SELECT id, itemdate, title FROM Announcements 
        WHERE (itemdate > @keydate OR
                (itemdate = @keydate) AND (id > @keyid))
        ORDER BY itemdate ASC, id ASC
    END*/

RETURN CEILING(@rowCount/@pageSize)

 
Old January 18th, 2008, 05:46 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Bon,

Take a look here: http://technet.microsoft.com/en-us/library/aa259204(SQL.80).aspx

Maybe NOCOUNT (or the lack thereof) is messing with your head? Try wrapping your code in a SET NOCOUNT ON / SET NOCOUNT OFF block.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old January 18th, 2008, 09:17 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi Imar,

Thanks for the link. I don't think my issue has a solution, though - at least not in the direction I've been taking. I'm pretty convinced it isn't possible to get ADO.NET to retieve both a multiple result set, and return values/output parameters from a sproc. If you want return values/output parameters, I think you're limited to scalar, output parameter-based result sets.

I've already soundly scolded myself for cross-posting (I should know by now that you and Peter are ubiquitous), but I had a little more to say about the matter at:

http://p2p.wrox.com/topic.asp?TOPIC_ID=68430

Thought maybe I had a weird Sql Server bug, but I think I understand whats going on now.

Best,

Bob






Similar Threads
Thread Thread Starter Forum Replies Last Post
Return all or nothing SQL Statement help... brux2dc SQL Language 1 May 30th, 2007 03:44 AM
Select from another select statement to a repeater simsen ASP.NET 2.0 Professional 0 May 2nd, 2007 04:34 PM
How to get "SELECT COUNT(*)" return information? datagram ASP.NET 1.0 and 1.1 Basics 3 December 23rd, 2004 01:21 AM
return to value in <SELECT> statement mikeuk Beginning PHP 3 September 25th, 2004 08:46 AM
Return Statement CodeMonkeys C# 4 August 25th, 2004 11:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.