2

I have a store procedure that works "manually" but when it's executed via dapper it returns a "-1" and doesn't error.

The sproc:

  ... PROCEDURE [ts].[uspAddClientForm](
   @ClientId int,
   @SystemFormId bigint,
   @Year int,
   @Name nvarchar(255),
   @CreatedBy nvarchar(2000),
   @CreateDate datetime = NULL,
   @Active bit = 1,
   @ClientFormId bigint OUTPUT
  )
  AS
  BEGIN
    SET NOCOUNT ON  
    INSERT INTO [ts].[ClientForm] ([ClientId], [SystemFormId], [Year] 
    ,[Name] ,[CreatedBy] ,[CreateDate], [Active]) 
    VALUES(@ClientId, @SystemFormId, @Year, @Name, @CreatedBy, GETDATE(),    @Active)

    SET @ClientFormId = SCOPE_IDENTITY() 
  END

The dapper related code:

   var formParams = new DynamicParameters();
       formParams.Add("@ClientId", clientForm.ClientId);
       formParams.Add("@SystemFormId", clientForm.SystemFormId);
       formParams.Add("@Year", clientForm.Year);
       formParams.Add("@Name", clientForm.Name);
       formParams.Add("@CreatedBy", clientForm.Username);
       formParams.Add("@CreateDate", DateTime.Now);
       formParams.Add("@Active", 1);
       formParams.Add("@ClientFormId", DbType.Int64, direction: ParameterDirection.Output);

       var result =  dbConnection.Execute("ts.uspAddClientForm", formParams, commandType: CommandType.StoredProcedure);

   // retrieve output value
   var clientId = formParams.Get<long>("@ClientFormId");

I also tried, unsuccessfully, removing the OUTPUT parameter using ExecuteScalar and Query returning the SCOPE_IDENTITY() in a SELECT. I'd prefer to use the OUTPUT way but I'm open to anything that works and generally subscribes to best practices.

0

2 Answers 2

2

Following Line not properly so modify and try it.

     formParams.Add("@ClientFormId", DbType.Int64, direction: ParameterDirection.Output);

To,

     formParams.Add("@ClientFormId","",dbType: DbType.Int64, direction: ParameterDirection.Output);
Sign up to request clarification or add additional context in comments.

Comments

0

You can try to select identity as something:

 SELECT SCOPE_IDENTITY() AS ClientFormId 

And in your dapper code: // assuming that your identity is an int .ExecuteScalar<int>("ts.uspAddClientForm", ...)

And also remove the output parameter, as you won't need that one.

1 Comment

Thanks, that did it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.