Interesting problem with getting OUTPUT parameters from SQL Server using C#

Last post 11-01-2009 5:48 AM by believer7. 9 replies.

Sort Posts:

  • Interesting problem with getting OUTPUT parameters from SQL Server using C#

    01-21-2008, 6:46 AM
    • Member
      point Member
    • gilberk
    • Member since 01-21-2008, 11:40 AM
    • Posts 1

     I have the following SP:

     

    Create procedure dbo.example

    (

    @test INT OUTPUT

    )

    AS

    BEGIN

    SET @test = 5

    SELECT * from Users

    END

    GO

     

     

    and when I try calling it from C# using an SqlCommand with adding an output parameter and getting it's value, I get that the value is NULL. (I should mention that I use ExecuteReader)

    However,  if I alter the SP and remove the select line, the c# code works and returns the value 5.

     

  • Re: Interesting problem with getting OUTPUT parameters from SQL Server using C#

    01-21-2008, 9:32 AM
    • Member
      155 point Member
    • szyder
    • Member since 06-14-2007, 3:21 PM
    • Posts 39

    try to use return @test

  • Re: Interesting problem with getting OUTPUT parameters from SQL Server using C#

    04-30-2008, 4:46 PM
    • Member
      8 point Member
    • ToddMoon
    • Member since 06-03-2007, 1:20 AM
    • Posts 14

    szyder:

    try to use return @test

     

     
    I am having the exact same problem. This reply is not helpful because the stored procedure may have multiple output parameters. When using return, you will only be able to return one value.

    EDIT: To clarify my problem, and to contradict the original post, the value of that output parameter is not NULL (DbNull.Value), it's null (good old-fashioned C# null). Which implies that the framework is not assigning any value at all to the output parameter after execution of the command. Actually, when I set a value to the parameter before executing the command, it gets replaced with null after execution. That's even more strange.

    More Info:

    If I execute the stored proc in Visual Studio and examine the results in the output window, it behaves exactly as expected. I see my query results, the value of my output parameter, and a @RETURN_VALUE of zero (the default if you don't use a RETURN statement)

    If I don't assign to the output parameter (which I named @ReturnCode) in the proc then I get <NULL> in the window. So, again, it's working exactly as expected when executing in VS. It seems that the SqlClient library is to blame.

    With SELECT and setting the output param:

    MacAddress  
    ------------
    0002680117E7
    No rows affected.
    (1 row(s) returned)
    @ReturnCode = 0
    @RETURN_VALUE = 0

     
    With SELECT and not setting output param:

    MacAddress  
    ------------
    0002680117E7
    No rows affected.
    (1 row(s) returned)
    @ReturnCode = <NULL>
    @RETURN_VALUE = 0
     

    From these results it's clear that it is being set when I set it to zero in the proc (Or returned as NULL if I don't set it), but it's always null when the SqlClient framework finishes executing it.

  • Re: Interesting problem with getting OUTPUT parameters from SQL Server using C#

    05-16-2008, 7:59 AM
    • Member
      736 point Member
    • zirmandli
    • Member since 05-22-2007, 5:45 AM
    • Ahmedabad, India
    • Posts 108

    Hi

    After declaring variables Execute your command  

    db.ExecuteNonQuery(dbCommandWrapper);

    object objRetTest;

    objRetTest= db.GetParameterValue(dbCommandWrapper, "Your variable name that u passes ");

     

    if (objRetTest== DBNull.Value) return -1;

    else

    // return output parameter values

     

    Click on 'Mark as Answer' if this post is helpful.

    -- Benazir Mandli
  • Re: Interesting problem with getting OUTPUT parameters from SQL Server using C#

    06-09-2009, 2:43 PM
    • Member
      4 point Member
    • RealityO
    • Member since 11-17-2008, 1:41 AM
    • Posts 17

     I'm bumping a one year old thread:

     As anyone ever fixed this issue? I'm currently having the same exact problem and there's no solution in the thread.

  • Re: Interesting problem with getting OUTPUT parameters from SQL Server using C#

    07-24-2009, 2:05 PM
    • Member
      2 point Member
    • bcline
    • Member since 07-24-2009, 6:04 PM
    • Posts 1

     I too am having the same problem.  Has anyone found a solution?

  • Re: Interesting problem with getting OUTPUT parameters from SQL Server using C#

    07-24-2009, 2:18 PM
    • Member
      8 point Member
    • ToddMoon
    • Member since 06-03-2007, 1:20 AM
    • Posts 14

    There's a simple but ugly workaround. Rather than use output parameters, select a second result set that contains one row with one column for each output variable you need. You will then have two result sets.

    SELECT * FROM SomeTable --This is your normal select result

    SELECT @Foo AS Foo, @Bar AS Bar, @Zap AS Zap --These are your "output parameters"

    Of course, you would remove the actual output parameters from the stored procedure if doing this, and just use local variables.

    If using a SqlDataReader you can use the NextResult() method to move to the next result. If using a SqlDataAdapter with a DataSet, you should get a second DataTable.

  • Re: Interesting problem with getting OUTPUT parameters from SQL Server using C#

    08-11-2009, 9:57 PM
    • Member
      2 point Member
    • coderdave
    • Member since 08-12-2009, 1:54 AM
    • Posts 1

    RealityO:
    As anyone ever fixed this issue? I'm currently having the same exact problem and there's no solution in the thread.
     

     

    Try the ZipZango Database Library. ( www.zipzango.com ) I wrote it after continually running into the same type of issue. Download the demo. If you think it will work for you, drop me an email and I'll arrange to get you a full copy for free.

    Regards,

    -DG

  • Re: Interesting problem with getting OUTPUT parameters from SQL Server using C#

    10-27-2009, 7:23 PM
    • Member
      2 point Member
    • junfengd
    • Member since 10-27-2009, 11:20 PM
    • Posts 1

    Since row sets will be returned before all output parameters for a stored procedure. I guess that you will need to either retrieve or skip all row sets and then getting the output parameters. 

  • Re: Interesting problem with getting OUTPUT parameters from SQL Server using C#

    11-01-2009, 5:48 AM
    • Member
      8 point Member
    • believer7
    • Member since 07-10-2007, 5:01 AM
    • Posts 4

    Replace this (SET @test = 5) with select @test=5


Page 1 of 1 (10 items)