Go to DotNetFunda.com
  Welcome, Guest!  
LoginLogin  
{ Submit resources and get monthly gifts !!! }
Submit: Article | Interview Question | Joke | Question | Link || Search  
 Skip Navigation Links Home > Articles > Implementing Custom Paging in ASP.NET with SQL Server 2005

All Articles | Post Articles

Implementing Custom Paging in ASP.NET with SQL Server 2005

 Posted on: 6/25/2008 7:45:44 PM by SheoNarayan | Views: 1290 | Category: Sql Server | Level: Advance | Print Article
Many a times we need to present bulk data to the user in our day to day application development. Loading all data at one shot is okay when we have few hundreads data but when it comes to thousands of thousands records, it hardly work.

In scenario like this we need to go for custom paging in our application.
Introduction

Why Custom Paging?
Custom paging allows you to get limited number records from a large database table that saves processing time of your database server as well as your application server and makes your application scalable, efficient and fast.


In this article, I am going to explain how to create a stored procedure in SQL Server 2005 that allows you to pass startRowIndex and pageSize as a parameter and return you the number of records starting from that row index to the page size specified. It was possible in the SQL Server 2000 too but it was not as easy as in SQL Server 2005 is.

Stored Procedure for Custom Paging in SQL Server 2005

In this example, I am assuming that I have a Articles table that contains thousands of records and I have to list articles from this table page wise. To do that I am going to pass two parameter to this SP and getting the custom paging done.

-- EXEC LoadPagedArticles 10, 5

CREATE PROCEDURE LoadPagedArticles

-- Add the parameters for the stored procedure here

@startRowIndex int,

@pageSize int

AS

BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from

-- interfering with SELECT statements.

SET NOCOUNT ON;

-- increase the startRowIndex by 1 to avoid returning the last record again

SET @startRowIndex = @startRowIndex + 1

BEGIN

SELECT * FROM (

Select *,

ROW_NUMBER() OVER (ORDER BY AutoID ASC) as RowNum

FROM Articles

) as ArticleList

WHERE RowNum BETWEEN @startRowIndex AND (@startRowIndex + @pageSize) - 1

ORDER BY AutoID ASC

END

END

GO

In the above stored procedure, I have two parameter @startRowIndex and @pageSize. @startRowIndex receives the row Index from where records to be returned and @pageSize receives the number of records to be returned.

First I am increasing the @startRowIndex by 1 so that I don't get the last record again in my result then writing my select statement specific to paging. Here you need to make sure that you are writing order by statement same in both subquery and main query like I have specified as "ORDER BY AutoID ASC" in my above code snippet. Now you can write your ASP.NET code that access the method that accepts two parameter and give you the desired result.

In my above example, I will execute the SP in SQL Server with followig parameter value for different pages (I am assuming that I have to get 5 records per page), You should pass same value from your code to the SP.

Page 1 - EXEC LoadPagedArticles 0, 5
Page 2 - EXEC LoadPagedArticles 5, 5
Page 3 - EXEC LoadPagedArticles 10, 5

Conclusion

Its always good to write code that is efficien and fast to serve the user request, Custom paging is most efficient way to get desired number of records from a large database table without hitting the performance of the database as well as application.

Please let me know your feedback if any. Happy Coding!!!



Bookmark and Share

About Sheo Narayan

Experience:6 year(s)
Home page:http://sheonarayan.blogspot.com/
Member since:Tuesday, July 08, 2008
Biography:Throughout 1st in all educational exams.
Major qualifications: HDCS, BCA, ADCA, MCA
Locations: Hyderabad, India
 Latest post(s) from SheoNarayan

   ◘ SEO friendly pagination using asp:DataPager control posted on 8/18/2008 9:54:23 AM
   ◘ ListView Tips and Tricks posted on 8/14/2008 5:43:53 AM
   ◘ Working with HttpHandler in IIS 7 posted on 7/28/2008 3:56:22 PM
   ◘ Service Oriented Architecture, a real world example in ASP.NET with C# posted on 7/27/2008 1:20:50 PM
   ◘ Difference between asp:LinkButton, asp:ImageButton, asp:Button and asp:HyperLink control posted on 7/15/2008 9:25:51 AM


Response(s) to this Article
Posted by: Dssmanyam | Posted on: 22 Jul 2008 07:06:11 AM
Great Job! You are helping Beginners and also experienced people. Thank You very much
Question: Why to use www.dotnetfunda.com google search?
Answer: This search has been especially optimized to search technical articles. You may find to-the-point results in comparison with other search.
Google
About Us | Contact Us | Privacy Policy and Terms of Use | Link Exchange | Members | Go Top
All rights reserved to DotNetFunda.com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
This site is best viewed with a resolution of 1280x720 (or higher) and Microsoft Internet Explorer 6.0+ or Firefox 2.0+.