tags:

views:

53

answers:

3

I know that there is another question with nearly the same title, but it doesn't answer my question. I have a stored procedure, which returns the unique identifier after insertion (@@identity). I tried it in the server explorer and it works as expected (@RETURN_VALUE = [identifier]). In my code I added a parameter called "@RETURN_VALUE", with ReturnValue direction first, than any other parameters, but whene I run my query with ExecuteNonQuery() that parameter remains empty. I don't know what I've done wrong. My SPROC:

    ALTER PROCEDURE dbo.SetAuction
 (
  @auctionID int,
  @itemID int,
  @auctionType tinyint,
  @reservationPrice int,
  @maxPrice int,
  @auctionEnd datetime,
  @auctionStart datetime,
  @auctionTTL tinyint,
  @itemName nchar(50),
  @itemDescription nvarchar(MAX),
  @categoryID tinyint,
  @categoryName nchar(50)
 ) AS
 IF @auctionID <> 0
  BEGIN
   BEGIN TRAN T1

   UPDATE Auction
   SET  AuctionType   = @auctionType,
     ReservationPrice = @reservationPrice,
     MaxPrice    = @maxPrice,
     AuctionEnd    = @auctionEnd,
     AuctionStart   = @auctionStart,
     AuctionTTL    = @auctionTTL
   WHERE AuctionID    = @auctionID;

   UPDATE Item
   SET
    ItemName  = @itemName,
    ItemDescription = @itemDescription
   WHERE
    ItemID = (SELECT ItemID FROM Auction WHERE AuctionID = @auctionID);

   COMMIT TRAN T1

   RETURN @auctionID
  END
 ELSE
  BEGIN
   BEGIN TRAN T1
    INSERT INTO Item(ItemName, ItemDescription, CategoryID)
    VALUES(@itemName, @itemDescription, @categoryID);

    INSERT INTO Auction(ItemID, AuctionType, ReservationPrice, MaxPrice, AuctionEnd, AuctionStart, AuctionTTL)
    VALUES(@@IDENTITY,@auctionType,@reservationPrice,@maxPrice,@auctionEnd,@auctionStart,@auctionTTL);
   COMMIT TRAN T1
   RETURN @@IDENTITY
  END

And my code is:

                cmd.CommandText = cmdText;
                SqlParameter retval = new SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int);
                retval.Direction = System.Data.ParameterDirection.ReturnValue;
                cmd.Parameters.Add(retval);
                cmd.Parameters.AddRange(parameters);
                cmd.Connection = connection;

                connection.Open();
                cmd.ExecuteNonQuery();

                return (int)cmd.Parameters["@RETURN_VALUE"].Value;
A: 

Do you get the value of you EXEC in TSQL? I wonder if refactoring the TSQL would help (and using SCOPE_IDENTITY():

so change:

COMMIT TRAN T1
RETURN @@IDENTITY

to:

SET @auctionID = SCOPE_IDENTITY()
COMMIT TRAN T1
RETURN @auctionID

(I would also change the other @@IDENTITY to SCOPE_IDENTITY())


As a minor optimisation, you could also use:

return (int)retval.Value;

but this side of things should have worked "as is" from what I can see (hence why I'm focusing on the TSQL).

Marc Gravell
Changed what you've suggested, but no effect.I use "EXEC SetAuction @auctionID, ..." as CommandText. I get the return value if I run it from server explorer (Visual Studio).
WebMonster
A: 

Just tried on my box and this works for me:

In SQL Server:

DROP PROCEDURE TestProc;
GO
CREATE PROCEDURE TestProc
AS
   RETURN 123;
GO

In C#

        string cnStr = "Server=.;Database=Sandbox;Integrated Security=sspi;";
        using (SqlConnection cn = new SqlConnection(cnStr)) {
            cn.Open();
            using (SqlCommand cmd = new SqlCommand("TestProc", cn)) {
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter returnValue = new SqlParameter();
                returnValue.Direction = ParameterDirection.ReturnValue;
                cmd.Parameters.Add(returnValue);

                cmd.ExecuteNonQuery();
                Assert.AreEqual(123, (int)returnValue.Value);
            }
        }

Greets Flo

Florian Reischl
id get's the value 0.
WebMonster
I extended the example. Maybe you would try this.
Florian Reischl
+1  A: 

I solved the problem: you have to set SqlCommand.CommandType to CommandType.StoredProcedure in order to get return values and/or output parameters. I haven't found any documentation about that, but now everything works.

WebMonster