Skip to content
Advertisement

How to get output parameter and also a table when executing a stored procedure

Scenario

  • I’m working with SQL Server 2017 (not possible to change)
  • I’m using Visual Studio 2019 in C# console and .NET Framework 4.5 (possible to change)
  • I’m using ADO.NET because several years before we couldn’t use Entity Framework, as the system is made to work with a stored procedure that returns at least 100k rows (possible to change)

Situation

I have an USP that returns a table that is at least 100k of rows by 20 fields. I need to add an output parameter in order to get also an ID created by the USP itself. So, the situation is that I need return a table and an ID (called ProcMonitorId). I don’t know if this is even so possible (See workarounds section)

At the SQL level is seems to be so far so good:

SQL execution:

enter image description here

And at repository layer (only relevant lines, someone were surprised for health of example):

Everything worked fine until the addition of the output at C# level. It returns in the line:

string ProcMonitorId = outPutParameter.Value.ToString();

it returns NullReferenceException because Value is null (that can’t be) and of course, can’t convert to String. I would solve this situation by adding a ? but if that’s situation happens for real, I need catch it any way as error. The main idea is that Value can not be null.

As I don’t have any ORM map, (and my expertise is not ADO.NET but Entity Framework) I can’t understand why is null (No, is not null at SQL layer, always return a value)

Question

How can I solve this error or how can I return a BIGINT parameter and ALSO a table result?

Workarounds

As I first glance I have to solve it quickly, I made a:

in order to simulate a “header” and “data” rows on one single table. But I don’t like this solution and is not very elegant and flexible. I’ve to parse the header every time.

Thanks in advance and please comment anything, tip, help, workaround, I will be glade to update my answer if more information is needed.

Also I can make my Framework to .NET Core or change to Entity Framework. That I can’t change is my SQL version

Update #2
No changes in SQL – Still working as screenshot
In C# – Hangs out for ever

Advertisement

Answer

The parameter value won’t be available until after you consume the resultset, eg

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement