Server side pagination is very useful when we are dealing with huge amounts of data. At that time client side pagination will take a long time to get all the data at the same time, so it's better to make a Server call on every page request.
We are going to see all employee data with pagination and we will make a Server call on every page request.
Also, we are going to use Web API for HTTP Service call and will use ADO.NET to access the data from the database. Our database will be SQL Server and we are using AngularJS and ui-bootstrap library to make UI ready.
Let's start with a sample Application for better understanding. We are going to use Visual Studio 2015 for this example.
First, open the Visual Studio and click File -> New - > Project, as shown below.

Now, select ASP.NET Web Application and enter the name as ServerSidePaginationInAngularJsAndWebAPI and click OK, as shown below.

Now, select Empty from a template and check Web API from check box list and click OK, as shown below.

Now, our Web API project is ready. Right click on Controller folder and click Add -> Controller from Solution Explorer, as shown below.

Select Web API 2 Controller - Empty and click Add button, as shown below.

Give the Controller name as EmployeeController and click Add button, as shown below.

Our Controller is ready for writing HTTP action methods. We will write GET Action method later in this article.
Now, right click on Models folder and add click Add -> Class, as shown below.

Give the class name Employee.cs, as shown below.

Write all the properties in Employee.cs class, as shown below.
- namespace ServerSidePaginationInAngularJsAndWebAPI.Models
- {
- public class Employee
- {
- public string Id { get; set; }
- public string Name { get; set; }
- public string Email { get; set; }
- public string Address { get; set; }
- }
- }
In a similar way, add one more class as EmployeeList.cs to get the employee list and total count for pagination details, as shown below.
- using System.Collections.Generic;
- namespace ServerSidePaginationInAngularJsAndWebAPI.Models
- {
- public class EmployeeList
- {
- public List<Employee> employees { get; set; }
- public string totalCount { get; set; }
- }
- }
Now, add one more folder for database operation as DBOperation and inside this folder, add class as EmployeeInfo.cs and write the code snippet given below.
- using System;
- using System.Collections.Generic;
- using System.Data.SqlClient;
- using System.Data;
- using System.Configuration;
- using ServerSidePaginationInAngularJsAndWebAPI.Models;
- namespace ServerSidePaginationInAngularJsAndWebAPI.DBOperation
- {
- public class EmployeeInfo
- {
- public EmployeeList GetEmployees(int pageIndex, int pageSize)
- {
- EmployeeList employeeList = new EmployeeList();
- using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
- {
- SqlCommand cmd = new SqlCommand("GetEmployees", connection);
- cmd.Parameters.Add("@PageIndex", SqlDbType.Int).Value = pageIndex;
- cmd.Parameters.Add("@pageSize", SqlDbType.Int).Value = pageSize;
- cmd.CommandType = CommandType.StoredProcedure;
- try
- {
- connection.Open();
- SqlDataReader dr = cmd.ExecuteReader();
- List<Employee> listEmp = new List<Employee>();
- while (dr.Read())
- {
- Employee emp = new Employee();
- emp.Id = dr["id"].ToString();
- emp.Name = dr["name"].ToString();
- emp.Email = dr["email"].ToString();
- emp.Address = dr["address"].ToString();
- listEmp.Add(emp);
- }
- dr.NextResult();
- while (dr.Read())
- {
- employeeList.totalCount = dr["totalCount"].ToString();
- }
- employeeList.employees = listEmp;
- }
- catch (Exception ex)
- {
- throw;
- }
- }
- return employeeList;
- }
- }
- }
Inside this class, we are using ADO.NET to make a SQL Server database call and retrieve all employee information and the total count of the employees.
Now, time to write our GET Action in the controller has come, where you can get the employees information from GetEmployees methods.
The code snippet for Web API Controller is given below.
- using System.Web.Http;
- using ServerSidePaginationInAngularJsAndWebAPI.Models;
- using ServerSidePaginationInAngularJsAndWebAPI.DBOperation;
- namespace ServerSidePaginationInAngularJsAndWebAPI.Controllers
- {
- public class EmployeeController : ApiController
- {
- public EmployeeList GetEmployees(int pageIndex, int pageSize)
- {
- EmployeeInfo empInfo = new EmployeeInfo();
- EmployeeList empList = empInfo.GetEmployees(pageIndex, pageSize);
- return empList;
- }
- }
- }
In the code given above, we are using two parameters as pageIndex and pageSize to get the current page information and a total number of employees to be displayed respectively.
Now, it’s time to create a database, table and stored procedure.
Create a database
We are going to create a database named as sample.
The query is given below to create a database in our SQL Server.
- USE [master]
- GO
- CREATE DATABASE [sample]
- CONTAINMENT = NONE
- ON PRIMARY
- ( NAME = N'sample', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA\sample.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
- LOG ON
- ( NAME = N'sample_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA\sample_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
- GO
- ALTER DATABASE [sample] SET COMPATIBILITY_LEVEL = 130
- GO
- IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
- begin
- EXEC [sample].[dbo].[sp_fulltext_database] @action = 'enable'
- end
- GO
- ALTER DATABASE [sample] SET ANSI_NULL_DEFAULT OFF
- GO
- ALTER DATABASE [sample] SET ANSI_NULLS OFF
- GO
- ALTER DATABASE [sample] SET ANSI_PADDING OFF
- GO
- ALTER DATABASE [sample] SET ANSI_WARNINGS OFF
- GO
- ALTER DATABASE [sample] SET ARITHABORT OFF
- GO
- ALTER DATABASE [sample] SET AUTO_CLOSE OFF
- GO
- ALTER DATABASE [sample] SET AUTO_SHRINK OFF
- GO
- ALTER DATABASE [sample] SET AUTO_UPDATE_STATISTICS ON
- GO
- ALTER DATABASE [sample] SET CURSOR_CLOSE_ON_COMMIT OFF
- GO
- ALTER DATABASE [sample] SET CURSOR_DEFAULT GLOBAL
- GO
- ALTER DATABASE [sample] SET CONCAT_NULL_YIELDS_NULL OFF
- GO
- ALTER DATABASE [sample] SET NUMERIC_ROUNDABORT OFF
- GO
- ALTER DATABASE [sample] SET QUOTED_IDENTIFIER OFF
- GO
- ALTER DATABASE [sample] SET RECURSIVE_TRIGGERS OFF
- GO
- ALTER DATABASE [sample] SET DISABLE_BROKER
- GO
- ALTER DATABASE [sample] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
- GO
- ALTER DATABASE [sample] SET DATE_CORRELATION_OPTIMIZATION OFF
- GO
- ALTER DATABASE [sample] SET TRUSTWORTHY OFF
- GO
- ALTER DATABASE [sample] SET ALLOW_SNAPSHOT_ISOLATION OFF
- GO
- ALTER DATABASE [sample] SET PARAMETERIZATION SIMPLE
- GO
- ALTER DATABASE [sample] SET READ_COMMITTED_SNAPSHOT OFF
- GO
- ALTER DATABASE [sample] SET HONOR_BROKER_PRIORITY OFF
- GO
- ALTER DATABASE [sample] SET RECOVERY SIMPLE
- GO
- ALTER DATABASE [sample] SET MULTI_USER
- GO
- ALTER DATABASE [sample] SET PAGE_VERIFY CHECKSUM
- GO
- ALTER DATABASE [sample] SET DB_CHAINING OFF
- GO
- ALTER DATABASE [sample] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
- GO
- ALTER DATABASE [sample] SET TARGET_RECOVERY_TIME = 60 SECONDS
- GO
- ALTER DATABASE [sample] SET DELAYED_DURABILITY = DISABLED
- GO
- ALTER DATABASE [sample] SET QUERY_STORE = OFF
- GO
- USE [sample]
- GO
- ALTER DATABASE SCOPED CONFIGURATION SET MAXDOP = 0;
- GO
- ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET MAXDOP = PRIMARY;
- GO
- ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = OFF;
- GO
- ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET LEGACY_CARDINALITY_ESTIMATION = PRIMARY;
- GO
- ALTER DATABASE SCOPED CONFIGURATION SET PARAMETER_SNIFFING = ON;
- GO
- ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET PARAMETER_SNIFFING = PRIMARY;
- GO
- ALTER DATABASE SCOPED CONFIGURATION SET QUERY_OPTIMIZER_HOTFIXES = OFF;
- GO
- ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET QUERY_OPTIMIZER_HOTFIXES = PRIMARY;
- GO
- ALTER DATABASE [sample] SET READ_WRITE
- GO



Faisal PathanPosted Nov 13, 2018, 12:22 AM
Thanks, This code isn't useful for me (I have different coding style) but after reading this article I got an idea, Thanks for sharing.
Soundar Kumar NPosted Oct 6, 2018, 6:59 AM
Hello brother sql management studio 2014 error in offset and fetch please tell me how to fix this error
Ahmad AL AamaPosted Aug 10, 2018, 4:39 AM
How I can use sorting also with this code ? thank you again
Ahmad AL AamaPosted Aug 10, 2018, 4:39 AM
Thank you dear,
v chandanPosted Nov 3, 2017, 12:26 PM
Hi where is the pagination directive code for the pagination ,, can you please share it its not even in the code download folder
Andres Felipe Naranjo HenaoPosted Jun 13, 2017, 3:58 PM
Good Code, Thanks, I am Looking for a good exercise that allow to paginate from Database. Works Great!
Venkat KumarPosted May 26, 2017, 11:45 PM
Hi Vivek,Nice article. I want to know whether its Angular2 or older version Angular 1+?
Raj KumarPosted Mar 28, 2017, 9:15 PM
Nice explanation..keep it up
Former memberPosted Mar 28, 2017, 7:54 AM
How to use smart table in angular js where data will be given by web api.