Click here to Skip to main content
15,885,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to read the result from a store procedure.
This is the Store Procedure:
ALTER PROCEDURE [dbo].[VALIDAR_CLIENTE_FACTURA]  
@CTE_NUMERO AS VARCHAR(12) OUTPUT
AS  
BEGIN  
 DECLARE @FECHAINICIO DATE  
 DECLARE @FECHAFIN DATE  
  
 SET @FECHAFIN = '2018-01-19'  
 SET @FECHAINICIO = '2016-01-19'  

 SET  @CTE_NUMERO=(
 SELECT   
  CASE WHEN   
   COUNT(*)>=1  
   THEN 'FACTURA VALIDA'   
   ELSE 'FACTURA NO VALIDA' END Resultado  
  FROM CLIENTES AS C   
 INNER JOIN FACTURAS AS F  
 ON F.CTE_NUMERO_EPS=C.CTE_NUMERO_EPS   
 WHERE REPLACE(C.CTE_NUMERO_EPS,'-','') = REPLACE(@CTE_NUMERO,'-','')  
 AND CAST(C.CTE_FECHA_INGRESO  AS DATE) BETWEEN @FECHAINICIO AND @FECHAFIN   
 AND CAST(F.FAC_FECHA  AS DATE) BETWEEN @FECHAINICIO AND @FECHAFIN)   
 
 RETURN @CTE_NUMERO


Here is my code in c#
[HttpGet("{numero}")]
       public async Task<ActionResult<string>> Get(string numero)
       {
           var Result="";
           var output = new SqlParameter();
           output.ParameterName = "@Salida";
           output.SqlDbType = System.Data.SqlDbType.Char;
           output.Direction = System.Data.ParameterDirection.Output;

           Result = await _contextInvoice.Database.ExecuteSqlRawAsync
               ("EXEC validar_cliente_factura @CTE_NUMERO={0},@Salida={1}", numero,output);

           return Result;
       }


But i can not, this line:
Result = await _contextInvoice.Database.ExecuteSqlRawAsync
               ("EXEC validar_cliente_factura @CTE_NUMERO={0},@Salida={1}", numero,output);

says: can not convert int to string, where is the error

What I have tried:

I had read examples, i had tried without the output parameter and return and int.
Posted
Updated 5-Aug-22 3:08am
Comments
PIEBALDconsult 4-Aug-22 19:12pm    
If the Procedure has only one parameter, why provide it two?
And why try to pass values in?
"EXEC validar_cliente_factura @CTE_NUMERO={0},@Salida={1}"

You initialized Result as a string (var Result = ""); at the very least, it should be var Result = 0; (to create an int).

var Result = await ... etc. would have worked also.

But it begs the whole point: why use var when you know it is an int and just get confused otherwise.

And then there's (Task<actionresult<int>>), or did you want to convert an actual int to a string?
 
Share this answer
 
v2
Comments
Luis M. Rojas 4-Aug-22 14:20pm    
Hello,
The result(the store procedure) has return a string: FACTURA VALIDA or FACTURA INVALIDA
Again, thanks to all of you.
The solution was in front of my eyes:
var r = await _contextInvoice.Database.ExecuteSqlRawAsync
               ("EXEC validar_cliente_factura CTE_NUMERO,@RESPUESTA OUTPUT", numero,output);
)


The value i wanted was here: output. Just have to ask for it in this way
(string)output.Value
 
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