1

I am building a .NET Core 3.1 Web API that uses stored procedures.

One of the stored procedures takes input and select values from a form and returns a ticket once the form is submitted in the front end.

My SQL Server stored procedure that looks like this:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[CallSummaryAddO]
    @SedonaUser NVarCHar(100),
    @SystemID Int,
    @ProblemID Int,
    @ResolutionID Int,
    @NextStepID Int,
    @CustomerComments NVaRChar(1000),
    @TechNotes NVaRChar(1000),
    @CustomerOnCall NVarChar(200),
    @CustomerCallBackPhone NVarChar(15)
AS
    DECLARE @ServiceTicketID Int,
            @CustomerID Int, 
            @CustomerSiteID Int,
            @NextTechID Int,
            @TicketStatus NVarCHar(10),
            @Today DateTime,
            @ClosedDate DateTime,
            @ServiceLevel Int,
            @Route Int,
            @AutoNotify NVarChar(320)= '',
            @CustomerBillID Int,
            @ServiceCoordinator Int = 1,
            @TicketNumber Int,
            @TicketNumberAdded Int 

    EXEC Sandbox.dbo.Service_Ticket_Next @TicketNumber Output
 
    SET @TicketNumberAdded = @TicketNumber
    SET @TicketStatus = CASE WHEN @ResolutionID = 1 THEN 'OP' ELSE 'CL' END
    SET @ClosedDate = CASE WHEN @ResolutionID = 1 THEN '1899-12-30' ELSE GETDATE() END

    SELECT
        @CustomerID = S.Customer_id, 
        @CustomerSiteID = S.Customer_Site_id, 
        @ServiceLevel = Service_Level_Id, 
        @Route = Route_Id, 
        @CustomerBillID = Ste.Customer_Bill_Id
    FROM
        Sandbox.dbo.ar_customer_system S
    INNER JOIN 
        Sandbox.dbo.ar_customer_site ste ON ste.Customer_Site_Id = S.Customer_Site_Id
    WHERE 
        Customer_System_Id = @SystemID

    SET @Today = GETDATE()
    SET @ServiceCoordinator = @NextStepID


    INSERT INTO Sandbox.dbo.[SV_Service_Ticket]
           ([Ticket_Status],[Ticket_Number],[Customer_Id],[Customer_Site_Id],[Customer_System_Id]
           ,[Multiple_Systems],[Creation_Date],[Requested_By],[Requested_By_Phone],[Problem_Id]
           ,[Scheduled_For],[Last_Service_Tech_Id],[Estimated_Length],[Resolution_Id],[Billable]
           ,[Billed],[Equipment_Charge],[Labor_Charge],[Other_Charge],[TaxTotal],
           [Regular_Hours],[Overtime_Hours],[Holiday_Hours],[Trip_Charge],[Invoice_Id],[Regular_Rate],[Overtime_Rate],[Holiday_Rate],[Bypass_Warranty],[Bypass_ServiceLevel],[IsInspection]
           ,[ClosedDate],[Manual_Labor],[Service_Company_Id],[Priority_Id],[Category_Id],[Expertise_Level]
           ,[Entered_By],[Invoice_Contact],[Signer],[Remittance],[Signature_Image],[Payment_Received]
           ,[Sub_Problem_Id],[Service_Level_Id],[UserCode],[Edit_Timestamp],[PO_Number],[CustomerComments],[Number_Of_Dispatches],[Route_Id]
           ,[Sub_Customer_Site_ID],[Customer_CC_Id],[Customer_Bank_Id],[Ticket_Status_Id],[Customer_EFT_Id],[Auto_Notify]
           ,[Customer_Bill_Id],[Customer_Contact_Id],[Requested_By_Phone_Ext] ,[Inspection_Id],[Service_Ticket_Group_Id]
           ,[Service_Coordinator_Employee_Id],[Resolved_Date],[Inspection_Incremented],[OPT_rowguid],[Bypass_TicketServiceCompany]
           ,[EntrySource])
    VALUES
           (@TicketStatus, @TicketNumber,@CustomerID, @CustomerSiteID, @SystemID,
           'N', @Today, @CustomerOnCall, @CustomerCallBackPhone, @ProblemID,
           '1899-12-30', 1, 60, @ResolutionID, 'N',
           '', 0,0,0,0,
           0,0,0,0,1,0,0,0,'N','N','N',
           @ClosedDate,'N',56,2,2,3,
           @SedonaUser,'','','N','0x4E6F205369676E6174757265204F6E2046696C65','N',
           1,@ServiceLevel,@SedonaUser,@Today,'', @CustomerComments,0,@Route,
           1,0,0,1,1,@AutoNotify,@CustomerBillID,1,'',1,1,@ServiceCoordinator,@ClosedDate,'N',NewID(),'N','Service')

    SET @ServiceTicketID = @@IDENTITY


    INSERT INTO Sandbox.dbo.SV_Service_Ticket_Notes (Service_Ticket_Id, Notes, Access_Level, UserCode, Entered_Date, Edit_UserCode, Edit_Date, Is_Resolution)
    VALUES (@ServiceTicketID, @TechNotes,1,@SedonaUser, @Today, @SedonaUser, @Today, Case When @TicketStatus = 'CL' then 'Y' else 'N' end)

    DECLARE @UserComments NVarChar(100)
    SET @UserComments = 'Added Service Ticket ' + CONVERT(NVarChar,@TicketNumber)

    INSERT INTO Sandbox.dbo.SY_Edit_Log (UserCode, Edit_Timestamp, TableName, Edit_Type_AUD, KeyField, KeyData, UserComments, SystemComments, Edit_Column_Name, OldData, NewData)
    VALUES (@SedonaUser, @Today, 'SV_Service_Ticket', 'A', 'Ticket_Number', @TicketNumber, @UserComments, '','','','')

    IF @TicketStatus = 'CL'
    BEGIN
        SET @UserComments = 'Closed Service Ticket ' + CONVERT(NVarChar,@TicketNumber)

        INSERT INTO Sandbox.dbo.SY_Edit_Log (UserCode, Edit_Timestamp, TableName, Edit_Type_AUD, KeyField, KeyData, UserComments, SystemComments, Edit_Column_Name, OldData, NewData)
        VALUES (@SedonaUser, @Today, 'SV_Service_Ticket', 'U', 'Ticket_Number', @TicketNumber, @UserComments, '','','','')
    END

    RETURN @TicketNumberAdded 

My repository looks like this:

public async Task<CallSummaryAddResult> InsertCallSummaryAddResult(CallSummaryAddResult callSummaryAddResult)
{
    using (SqlConnection sql = new SqlConnection(_connectionString))
    {
        using (SqlCommand cmd = new SqlCommand("dbo.CallSummaryAddO", sql))
        {
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@SedonaUser", SqlDbType.NVarChar)).Value = callSummaryAddResult.SedonaUser;
            cmd.Parameters.Add(new SqlParameter("@SystemID", callSummaryAddResult.SystemID));
            cmd.Parameters.Add(new SqlParameter("@ProblemID", callSummaryAddResult.ProblemID));
            cmd.Parameters.Add(new SqlParameter("@ResolutionID", callSummaryAddResult.ResolutionID));
            cmd.Parameters.Add(new SqlParameter("@NextStepID", callSummaryAddResult.NextStepID));
            cmd.Parameters.Add(new SqlParameter("@CustomerComments", SqlDbType.NVarChar)).Value = callSummaryAddResult.CustomerComments;
            cmd.Parameters.Add(new SqlParameter("@TechNotes", SqlDbType.NVarChar)).Value = callSummaryAddResult.TechNotes;
            cmd.Parameters.Add(new SqlParameter("@CustomerOnCall", SqlDbType.NVarChar)).Value = callSummaryAddResult.CustomerOnCall;
            cmd.Parameters.Add(new SqlParameter("@CustomerCallBackPhone", SqlDbType.NVarChar)).Value = callSummaryAddResult.CustomerCallBackPhone;
            //cmd.Parameters.Add(new SqlParameter("@TicketNumber", SqlDbType.Int));
            //cmd.Parameters["@TicketNumber"].Direction = ParameterDirection.Output;

            await sql.OpenAsync();
            await cmd.ExecuteNonQueryAsync();

            _callSummaryAddResults.Add(callSummaryAddResult);
            return callSummaryAddResult;
        }
    }
}

When I execute this in SSMS, I am getting a new ticket number, but in code, I am only getting 0 back. I want to display the actual returned ticket number and not a 0 or 1 (success or failure).

My job is to return the TicketNumber and display this to the end-user once available. How can I make this happen?

Thanks in advance.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
carltonstith
  • 154
  • 3
  • 9
  • 5
    Think you need to use output paramater and not the Return statement in your SP here: https://www.sqlservertutorial.net/sql-server-stored-procedures/stored-procedure-output-parameters/ – Brad Dec 10 '20 at 20:44
  • 1
    Which dbms are you using? (That code is product specific.) – jarlh Dec 10 '20 at 20:45
  • 1
    @jarlh I am using SQL Server – carltonstith Dec 10 '20 at 20:49
  • 4
    `ParameterDirection.Output` is for `output` parameters. Have you tried `ParameterDirection.ReturnValue` instead? REF: [ParameterDirection Enum](https://learn.microsoft.com/en-us/dotnet/api/system.data.parameterdirection) – AlwaysLearning Dec 10 '20 at 21:09
  • @AlwaysLearning `ParameterDirection.ReturnValue` when used allows me to submit my data using Postman but my JSON response value for ticketNumber is 0. – carltonstith Dec 10 '20 at 21:15
  • 1
    The C# code you've posted literally doesn't do what you claim is happening. – Ian Kemp Dec 10 '20 at 22:04
  • To emphasize Ian Kemp - it is your c# code. Here is the concept - it has to do with parameter direction on a "dummy" parameter ( @ReturnVal in this case) https://stackoverflow.com/a/6210055/2080879 – Sql Surfer Dec 10 '20 at 23:42
  • Although you can use the stored procedure return value to return integer data, I suggest you avoid doing so. The return code is SQL Server is intended to indicate success or warning/error (zero success, non-zero error). – Dan Guzman Dec 11 '20 at 02:44
  • Thanks, everyone. I was able to resolve my issue by refactoring the stored procedure to include the correct output parameter. – carltonstith Dec 11 '20 at 13:50

0 Answers0