Stored procedure for custom paging in gridview

regarding the stored procedure from
http://www.tipsntracks.com/316/gridview-custom-paging-in-asp-net-3-5-with-sql-server-stored-procedure.html
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
)
AS
BEGIN
 
– 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 +‘%’ 
 
SET ROWCOUNT @intPageSize
SELECT sName, sAddress,ClassName FROM  @tblStudentDtl WHERE SrNo > @start
SET ROWCOUNT 0
 
END

Open in new window

LVL 1
doramail05Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cyberkiwiCommented:
CREATE procedure sp_StudentDtl
(
    @strSearchText varchar(100),
    @intPageSize int,
    @intCurrentPage int,
    @intTotalRecords int output
)
AS
BEGIN
 
-- Author: <Chetankumar Akarte>
-- Description: <Return Records depending on @intPageSize and @intCurrentPage.>

SELECT @intTotalRecords=Count(*)
FROM tblStudent A
JOIN tblClass B ON A.ClassId=B.ClassId
WHERE sName like @strSearchText +'%' 

SELECT sName, sAddress,ClassName
FROM
(
	SELECT TOP (@intCurrentPage*@intPageSize)
		sName, sAddress,ClassName,
		RN=ROW_NUMBER() over (order by SrNo)
	FROM tblStudent A
	JOIN tblClass B ON A.ClassId=B.ClassId
	WHERE sName like @strSearchText +'%'
	order by SrNo
) X
WHERE RN > (@intCurrentPage-1)*@intPageSize
 
END

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cyberkiwiCommented:
There is no need to dump records into a temp table, as it will only churn temp storage or RAM.

The COUNT(*) is interesting, because there is no way to avoid a full scan on the index on sName or (classId, sName) -- you better have one.

For the 2nd part, the TOP only produces ROW_NUMBER up to as far as required and never more, then the outer query returns only the required last page (in the range numbered)
0
doramail05Author Commented:
the Row_Number() returns the exact table row disregards of the identity being set rite?

another thing is, the tblClass is just a dummy table where there's a join table case rite?
JOIN tblClass B ON A.ClassId=B.ClassId
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

cyberkiwiCommented:
You haven't really indicated where the fields

sName, sAddress,ClassName

come from. They could either come from A or B.

If A to B has a one-to-many or many-to-many relationship, the result of the join can be more than the number of unique A or B records (cartesian product).  Therefore the count(*) and row_number() both need to work on the join between A and B.
0
doramail05Author Commented:
can you elaborate this part :

SELECT sName, sAddress,ClassName
FROM
(
      SELECT TOP (@intCurrentPage*@intPageSize)
            sName, sAddress,ClassName,
            RN=ROW_NUMBER() over (order by SrNo)
      FROM tblStudent A
      JOIN tblClass B ON A.ClassId=B.ClassId
      WHERE sName like @strSearchText +'%'
      order by SrNo
) X
WHERE RN > (@intCurrentPage-1)*@intPageSize

lets say current page is 2,
so it should be SELECT TOP (2 * 10) = 20

WHERE RN > (2 - 1) * 10
             RN > 10

so that it will be selecting records on 2nd page (RN more than 10) disregards of what the sequential ids is, because we are using RN=ROW_NUMBER() over (order by SrNo) , which is getting the exact table row ?
0
cyberkiwiCommented:
Yes correct. It won't bother to number more than 2 pages worth (20 records), hence the TOP 20.

With those 20, it will only get RN>10.  This also works for the last page if TOP 500 returns only 497, so RN > 490 will get only 7 records.

Yes row_number() produces a positional number without gaps, and does not rely on any id.
0
doramail05Author Commented:
o.o ~~  phew ok.
0
doramail05Author Commented:
back to that, that the SrNo hasn't been declare, pls advise
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.