• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 715
  • Last Modified:

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

0
doramail05
Asked:
doramail05
  • 4
  • 4
1 Solution
 
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
 
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now