Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

SqlException error when trying to implement pagination in .net core web api

$
0
0

Hello All,

I have these two pieces of code to implement pagination in .net core.

The first piece is a class as follows:

public class QueryParameters
{
    const int _maxSize = 100;
    private int _size = 50;

    public int Page { get; set; }

    public int Size 
    {  
        get { return _size; }
        set { _size = Math.Min(_maxSize, value); }
    }
}

The second one is my HttpGet code:

private readonly NSContext _context;

    public UsersController(NSContext context)
    {
        _context = context;
    }

    [HttpGet]
    public async Task<IActionResult> GetAllUSers([FromQuery] QueryParameters queryParameters)
    {
        IQueryable<User> users = _context.User;
        users = users
            .Skip(queryParameters.Size * (queryParameters.Page - 1))
            .Take(queryParameters.Size);
        return Ok(await users.ToArrayAsync());
    }

but when I run the program I get this error:

Does anybody have a solution for this problem?

Thanks in advance.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>