We help IT Professionals succeed at work.
Get Started

Stored procedure for custom paging in gridview

Last Modified: 2012-05-11
regarding the stored procedure from
where it gives the custom paging functionality for ASP.NET gridview,
notice that the SrNo which is the identity,
given that
SELECT sName, sAddress,ClassName FROM  @tblStudentDtl WHERE SrNo > @start
and from
SET @start=@intPageSize*(@intCurrentPage - 1)

if we have pagesize = 10 , currentpage = 2,
it will have the equation of @start = 10 * (2 - 1)
which is  @start = 10

with that,
SELECT sName, sAddress,ClassName FROM  @tblStudentDtl WHERE SrNo >10
we will have been selecting (with pagesize = 10)
11, 12, 13, 14, 15, 16, 17, 18, 19, 20.

if for any reason we deleted records on ids
1,2, ... 20       (from 1 to 20)
and left 1001, 1002, 1003, 1004, 1005, 1006, -- 1020.

then with currentpage = 2
SELECT sName, sAddress,ClassName FROM  @tblStudentDtl WHERE SrNo >10
it will select
1001, 1002, --- 1010.
as they are records from 1st page rather than selecting record from 2nd page.

looking for clarification , and if any suggestions are okay.
CREATE procedure sp_StudentDtl
    @strSearchText varchar(100),
    @intPageSize int,
    @intCurrentPage int,
    @intTotalRecords int output
– Author: <Chetankumar Akarte>
– Description: <Return Records depending on @intPageSize and @intCurrentPage.>
DECLARE @tblStudentDtl TABLE(
SrNo int IDENTITY not null,
sName varchar(50) ,
sAddress varchar(100),
ClassName varchar(100)
DECLARE @start int
SET @start=@intPageSize*(@intCurrentPage - 1)
INSERT INTO @tblStudentDtl (sName,sAddress,ClassName)
SELECT sName, sAddress,ClassName FROM tblStudent A JOIN tblClass B ON A.ClassId=B.ClassId WHERE sName like @strSearchText +‘%’ 
SELECT @intTotalRecords=Count(*) FROM tblStudent A JOIN tblClass B ON A.ClassId=B.ClassId WHERE sName like @strSearchText +‘%’ 
SELECT sName, sAddress,ClassName FROM  @tblStudentDtl WHERE SrNo > @start

Open in new window

Watch Question
Expert of the Quarter 2010
Expert of the Year 2010
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE