I am implementing a service in ASP.NET Core but it gives me a procedure error, as I understand it is because of the type of variable that I am entering and I sent a string instead of int , I changed it but I still get the same error, what else should I do implement or change so that it works for me and stops that error. Also, as I would return several returns in my postman, I mean that it executes several queries in a single service, one by one. Thanks for your help beforehand.
this is my controller
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using Salesws.Context;
using Salesws.Models;
using Salesws.Service;
using Salesws.Service.Interfaces;
using System;
using System.Collections.Generic;
using System.Data;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace Salesws.RestControllers
{
[Route("api/[controller]")]
[ApiController]
public class LoginController : ControllerBase
{
private IConfiguration _config;
//Contexto de la base de datos SVM
private readonly SVMcontext _context;
private readonly IFechaService _fechaService;
private readonly ILambSelLiquidacionService _iamb_Sel_Liquidacion;
public LoginController(IConfiguration config, SVMcontext context,IFechaService fechaService,ILambSelLiquidacionService amb_Sel_LiquidacionService)
{
_config = config;
_context = context;
_fechaService = fechaService;
_iamb_Sel_Liquidacion = amb_Sel_LiquidacionService;
}
[HttpGet("liqui/{celular}/{cod_liquidacion}")]
public ActionResult liqui()
{
var liqui = _iamb_Sel_Liquidacion.liquidacion();
return Ok(liqui);
}
}
}
this is my repository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Salesws.Service.Interfaces
{
public interface ILambSelLiquidacionRepository
{
int liquidacion();
}
}
this my service
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Salesws.Service.Interfaces
{
public interface ILambSelLiquidacionService
{
int liquidacion();
}
}
I implement my service
using Salesws.Service.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Salesws.Service
{
public class AmbSelLiquidacionService:ILambSelLiquidacionService
{
private ILambSelLiquidacionRepository _liqui;
public AmbSelLiquidacionService(ILambSelLiquidacionRepository liqui)
{
_liqui = liqui;
}
int ILambSelLiquidacionService.liquidacion()
{
return _liqui.liquidacion();
}
}
}
implement the procedure
using Microsoft.EntityFrameworkCore;
using Salesws.Context;
using Salesws.Service.Interfaces;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Threading.Tasks;
namespace Salesws.Models
{
public class AmbSelLiquidacionRepository:ILambSelLiquidacionRepository
{
private readonly SVMcontext _context;
public AmbSelLiquidacionRepository(SVMcontext context)
{
_context = context;
}
public int liquidacion()
{
int liqui = 0;
using (DbCommand command = _context.Database.GetDbConnection().CreateCommand())
{
command.CommandText = "dbo.amb_sel_liquidacion";
command.CommandType = CommandType.StoredProcedure;
_context.Database.OpenConnection();
using (var resultado = command.ExecuteReader())
{
while (resultado.Read())
{
liqui = (int)resultado[0];
}
}
_context.Database.CloseConnection();
}
return liqui;
}
}
}
The database server is waiting for the p_cod_settlement parameter to be supplied.
you should try something like this:
or you can do this:
but it is susceptible to SQL injection.