Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I am trying to return multiple variables from my SQL procedure to C# code but they are being returned as 0. I have tried a variety of things but below is my current code.

What I have tried:

C#
using (SqlConnection conn = new SqlConnection(clsGlobals.SQLConnCRM))
           {
               try
               {
                   //select all in temp table
                   using (SqlCommand command = new SqlCommand("DDExportService_Select_TotalAmountProcessed", conn))
                   {
                       command.CommandTimeout = 0;
                       command.CommandType = CommandType.StoredProcedure;
                       command.Parameters.AddWithValue("@Company", company);
                       command.Parameters.AddWithValue("@TotalAmountProcessed", returnTotalRecords);
                       command.Parameters.AddWithValue("@TotalSumProcessed", returnSumRecords);

                       conn.Open();
                       command.ExecuteNonQuery();

                       var returnTotal = command.Parameters["@TotalAmountProcessed"].Value;
                       var returnSum = command.Parameters["@TotalSumProcessed"].Value;

                       conn.Close();

                       returnTotalRecords = Convert.ToInt32(returnTotal);
                       returnSumRecords = Convert.ToDecimal(returnSum);

                   }
               }





SQL
<pre lang="SQL">
USE [CRM_vs.net]
GO
/****** Object:  StoredProcedure [dbo].[DDExportService_Select_TotalAmountProcessed]    Script Date: 16/02/2022 10:39:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ===================================================
-- Author:		nstrain
-- Create date: 15/02/2022
-- Description:	gets a total count of  
--				records update to Paid in 
--				DD export for that day to send
--				notitification email.
-- 
-- for CRM DD Export console service

-- EXEC [dbo].[DDExportService_Select_TotalAmountProcessed]
-- ===================================================
ALTER PROCEDURE [dbo].[DDExportService_Select_TotalAmountProcessed]
(

@Company AS INT	,
@TotalAmountProcessed AS INT OUTPUT,
@TotalSumProcessed AS DECIMAL OUTPUT

)
AS
BEGIN

	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;


		SELECT COUNT(p.MX_paymentId) as PaymentCount, SUM(p.MX_Amount) as PaymentAmount
		INTO #tempRecords
		FROM [MRM_MSCRM].[dbo].mx_payment p
			INNER JOIN [MRM_MSCRM].[dbo].MX_mddussubscription s
			ON s.mx_mddussubscriptionid = p.mx_subscriptionid 
		WHERE mx_paiddate between DATEADD(day, DATEDIFF(day, 0, GETDATE()), '00:00:00') and DATEADD(day, DATEDIFF(day, 0, GETDATE()), '23:59:59')
		   AND p.mx_name = 'Paid'           
           AND mx_processing = 'False'
           AND p.mx_paidby = 'crmadmin' 
		   AND s.mx_company = @Company


		   SET @TotalAmountProcessed = (SELECT PaymentCount FROM #tempRecords)
		   SET @TotalSumProcessed = (SELECT PaymentAmount FROM #tempRecords)


END
Posted
Updated 18-Feb-22 1:33am

Simple:
C#
using (SqlCommand command = new SqlCommand("DDExportService_Select_TotalAmountProcessed", conn))
{
    command.CommandTimeout = 0;
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.AddWithValue("@Company", company);
    
    var pTotalAmount = new SqlParameter("@TotalAmountProcessed", SqlDbType.Int) { Direction = ParameterDirection.Output };
    command.Parameters.Add(pTotalAmount);
    
    var pTotalSum = new SqlParameter("@TotalSumProcessed", SqlDbType.Decimal) { Direction = ParameterDirection.Output };
    command.Parameters.Add(pTotalSum);
    
    conn.Open();
    command.ExecuteNonQuery();
    
    returnTotalRecords = Convert.ToInt32(pTotalAmount.Value);
    returnSumRecords = Convert.ToDecimal(pTotalSum.Value);
}
 
Share this answer
 
v2
Comments
Maciej Los 18-Feb-22 11:16am    
5ed!
In addition to Solution-I, just a minor change, add pTotalSum with second parameter insted of pTotalAmount like:
var pTotalSum = new SqlParameter("@TotalSumProcessed", SqlDbType.Decimal) { Direction = ParameterDirection.Output };
    command.Parameters.Add(pTotalSum);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900