1

net i have this Controller that gets data from a table :

Entity/model :

using Microsoft.EntityFrameworkCore;
namespace ESM_DASHBOARD.Data.Entities
{
    [Keyless]
    public class wareh_KPI_IN
    {
        public int total_in { get; set; }
        public Decimal price { get; set; }
        public double total_value { get; set; }
        public int Week_nb { get; set; }    
        public int Month_nr { get; set; }
    }}

this is the code :

using ESM_DASHBOARD.Data;
using ESM_DASHBOARD.Data.Entities;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace ESM_DASHBOARD.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class wareh_KPI_INController : ControllerBase
    {
        private readonly EsmDashboardContext _esmDashboardContext;
        public wareh_KPI_INController(EsmDashboardContext esmDashboardContext)
        {
            _esmDashboardContext = esmDashboardContext;
        }
        [HttpGet]
        public async Task<IActionResult> Get()
        {
            var wareh_KPI_INs = await _esmDashboardContext.wareh_KPI_IN.ToArrayAsync();
            return Ok(wareh_KPI_INs);
        }
    }
}

i tried to add this but its not working it displays (Object reference not set to an instance of an object)

        [HttpGet("{Week_nb}")]
        public async Task<IActionResult> Get(int Week_nb)
        {
            var wareh_KPI_INs = await _esmDashboardContext.wareh_KPI_IN.FindAsync(Week_nb);
            return Ok(wareh_KPI_INs);
        }
    }
}
Chanka1021
  • 81
  • 8
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Dimitris Maragkos Oct 25 '22 at 09:25
  • What is your Week_nb ? Which line has this null error ? – Qing Guo Oct 25 '22 at 09:27
  • Do you set the breakpoint at `wareh_KPI_INs`? Can you get the value? – Qing Guo Oct 25 '22 at 09:34
  • @QingGuo i get all the data on the table (first code ) but i want to specific the week nr – Chanka1021 Oct 25 '22 at 10:15
  • Have your problem been solved ? If not, try to check your if your `_esmDashboardContext ` have the data about wareh_KPI_IN by `int Week_nb`. Could you get the value of `var wareh_KPI_INs = await _esmDashboardContext.wareh_KPI_IN.FindAsync(Week_nb);` ? – Qing Guo Nov 01 '22 at 07:56

1 Answers1

0

In your action, specify that the variable of Week_nb is read from route :

[HttpGet("{Week_nb}")]
public async Task<IActionResult> Get([FromRoute] int Week_nb)
{
    var wareh_KPI_INs = await _esmDashboardContext.wareh_KPI_IN.FindAsync(Week_nb);
    return Ok(wareh_KPI_INs);
}
Sina Bahmanpour
  • 49
  • 2
  • 12