In this article, you will learn how to implement SQL paging in ASP.Net. I have used an ASP.Net Repeater control but the same concept can be used for an ASP.Net GridView control too.
Required software's are:
- Visual Studio 2008/2010
- SQL Server 2005/2008
I am using Visual Studio 2010 and SQL Server 2008.
Implementation Steps
Step 1
Download the attached file to get more information about it. The images and CSS files are also in the attached file.
Step 2
Before creating a web site, run the database script. In the SQL query analyzer create a table and a stored procedure.
SET
ANSI_NULLS ON
GO
SET
QUOTED_IDENTIFIER
ON
GO
SET
ANSI_PADDING ON
GO
CREATE
TABLE [dbo].[Employee](
[EmployeeID] [int] IDENTITY(1,1)
NOT NULL,
[FirstName] [varchar](100)
NULL,
[MiddleName] [varchar](100)
NULL,
[LastName] [varchar](100)
NULL,
[Address1] [varchar](max)
NULL,
[City] [varchar](50)
NULL,
[Country] [varchar](50)
NULL,
[Age] [int] NULL,
[DOB] [datetime] NULL
)
ON [PRIMARY]
GO
SET
ANSI_PADDING OFF
GO
SET
ANSI_NULLS ON
GO
SET
QUOTED_IDENTIFIER
ON
GO
CREATE
PROCEDURE [dbo].[DisplayDataPaging]
(
@PageNumber INT,
@PageSize INT=''
)
AS
SET
NOCOUNT ON;
CREATE TABLE
#Employee
(
EmployeeID int
,FullName
Varchar(500)
,Address1 Varchar(max)
,City Varchar(50)
,Country Varchar(100)
,Age int
,DOB DateTime
,RowNumber Int
IDENTITY(1,1)
)
DECLARE @intTotal
INT
DECLARE @SQL
varchar(MAX)
SET @intTotal
= @PageSize * @PageNumber
Select
COUNT(EmployeeID)
As TotalRecord
FROM Employee
INSERT INTO
#Employee
SELECT EmployeeID
,(FirstName +
' ' +
LastName) As
FullName
,Address1
,City
,Country
,Age
,DOB
FROM Employee
Select
EmployeeID
,FullName
,Address1
,City
,Country
,Age
,DOB
,RowNumber
From #Employee
WHERE RowNumber
between ((@PageNumber
* @PageSize)-(@PageSize-
1)) AND
(@PageNumber
* @PageSize)
Declare @TotalRecords
Integer
Declare @TotalPage
Integer
SELECT @TotalRecords=MAX(RowNumber)
from #Employee
if(@TotalRecords
is not
NULL)
begin
if(@TotalRecords%@PageSize
= 0)
begin
SET @TotalPage =
@TotalRecords/@PageSize
end
else
begin
SET @TotalPage =
@TotalRecords/@PageSize
+ 1
end
end
else
begin
set @TotalPage =
1
end
Select @TotalPage [TotalPages],
@TotalRecords [TotalRecords]
DROP Table
#Employee
GO
Insert some records into Employee table.
Step 3
Create a new web site called <PagingSite> in Visual Studio 2010.

Step 4
Change the connection string in the web.config file according to your database server credentials.
<connectionStrings>
<add
name="connectionStringKey"
connectionString
="Data Source=server1;Initial Catalog=TestDatabase;uid=user1;pwd=password1"/>
</connectionStrings>
Step 5
Create 2 classes called clsDatabase.cs and JobServices.cs as:

And write the following code
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Data.SqlClient;
using
System.Data;
using
System.Configuration;
///
<summary>
/// Summary description
for clsDatabase
///
</summary>
public
class
clsDatabase
{
public clsDatabase()
{
//
// TODO: Add constructor logic
here
//
}
private
SqlConnection objConnection =
null;
private string
connectionString = string.Empty;
public
string ConnectionString
{
get {
return this.connectionString; }
set { this.ConnectionString
= value; }
}
///
<summary>
///
Open the connection.
///
</summary>
private
SqlConnection funOpenConnection()
{
try
{
string connStr =
"";
objConnection = new
SqlConnection();
this.connectionString =
ConfigurationManager.ConnectionStrings["connectionStringKey"].ToString();
//this.connectionString =
ConfigurationSettings.AppSettings["connectionStringKey"];
objConnection = new
SqlConnection(connectionString);
if (objConnection.State !=
ConnectionState.Open)
objConnection.Open();
}
catch (Exception
objException)
{
return
null;
}
return objConnection;
}
///
<summary>
///
Close the connection.
///
</summary>
public
void CloseConnection()
{
if (objConnection !=
null)
{
if (objConnection.State ==
ConnectionState.Open)
objConnection.Close();
}
}
///
<summary>
///
Close the connection.
///
</summary>
public
void funCloseConnection()
{
if (objConnection !=
null)
objConnection.Close();
}
///
<summary>
///
Release resources.
///
</summary>
public
void Dispose()
{
// make sure connection is closed
if (objConnection !=
null)
{
objConnection.Dispose();
objConnection = null;
}
}
public void
RunProcedure(string sProcName,
SqlParameter[] objaPrams,
out
DataSet objDataSet)
{
try
{
SqlDataAdapter
objDataAdapter = new
SqlDataAdapter();
SqlCommand objCommand
= funCreateCommand(funOpenConnection(), sProcName, objaPrams);
objDataAdapter.SelectCommand = objCommand;
objDataSet = new
DataSet();
objDataAdapter.Fill(objDataSet);
}
catch (Exception
ex) { objDataSet = null; }
finally {
this.CloseConnection(); }
}
private
SqlCommand funCreateCommand(SqlConnection
objConnection, string sProcName,
SqlParameter[] objaPrams)
{
SqlCommand objCommand =
new
SqlCommand(sProcName, objConnection);
objCommand.CommandType =
CommandType.StoredProcedure;
// add
proc parameters
if (objaPrams !=
null)
{
foreach (SqlParameter
objParameter in objaPrams)
objCommand.Parameters.Add(objParameter);
}
// return objaPrams
objCommand.Parameters.Add(
new
SqlParameter("ReturnValue",
SqlDbType.Int, 4,
ParameterDirection.ReturnValue,
false, 0, 0,
string.Empty,
DataRowVersion.Default,
null));
return
objCommand;
}
public
SqlParameter MakeInParameter(string
sParamName, SqlDbType objDbType,
int iSize, object
objValue)
{
return MakeParameter(sParamName,
objDbType, iSize, ParameterDirection.Input,
objValue);
}
///
<summary>
///
Make input param.
///
</summary>
///
<param name="sParamName">Name
of param.</param>
///
<param name="objDbType">Param
type.</param>
///
<param name="iSize">Param
size.</param>
///
<returns>New
parameter.</returns>
public
SqlParameter MakeOutParameter(string
sParamName, SqlDbType objDbType,
int iSize)
{
return MakeParameter(sParamName,
objDbType, iSize, ParameterDirection.Output,
null);
}
///
<summary>
///
Make stored procedure param.
///
</summary>
///
<param name="sParamName">Name
of param.</param>
///
<param name="objDbType">Param
type.</param>
///
<param name="iSize">Param
size.</param>
///
<param name="objDirection">Parm
direction.</param>
///
<param name="objValue">Param
value.</param>
///
<returns>New
parameter.</returns>
public
SqlParameter MakeParameter(string
sParamName, SqlDbType objDbType,
Int32 iSize,
ParameterDirection objDirection,
object objValue)
{
SqlParameter objParameter;
if (iSize > 0)
objParameter = new
SqlParameter(sParamName,
objDbType, iSize);
else
objParameter = new
SqlParameter(sParamName,
objDbType);
objParameter.Direction = objDirection;
if (!(objDirection ==
ParameterDirection.Output &&
objValue == null))
objParameter.Value = objValue;


Konemore StoryPosted Apr 20, 2018, 1:21 PM
What is RecentJobsSize
Pradip PandeyPosted Aug 6, 2012, 11:52 PM
Thanks Rohtash and yes we can display total no. of records with paging number, because if you see, the result returned by the stored procedure also contains total no. of records.
Rohatash KumarPosted Aug 6, 2012, 11:25 PM
Thanks so much Sir, I really appreciate your effort really I found this article useful. Can you display total number of records with paging number.