|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
Introduction
Searching on the web, I found a lot of useful articles about this new feature of SQL Server 2005 and among them I mention the
Jason Witty’s article - that was a starting point for me. We have been waiting for a long time for this new feature of SQL
Server 2005. I don’t want to compare this new kind of ASP .NET paging with the well-known paging using Code
In the PageNum = 1;
PageSize = Convert.ToInt16(PageSizeTxt.Text);
BindGridView();
In the Go button event click, where we go to a page number:
if (GoToPageTxt.Text != "")
{
int maxPage = Convert.ToInt32(TotalPages.Text);
int goToPage = Convert.ToInt32(GoToPageTxt.Text);
if (goToPage <= maxPage)
{
PageNum = goToPage;
BindGridView();
}
}
Here is the GridView HTML code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" > <Columns> <asp:BoundField DataField="RowNumber" HeaderText="RowNumber" /> <asp:BoundField DataField="Name" HeaderText="Name" /> <asp:BoundField DataField="SalesYTD" HeaderText="SalesYTD" /> <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" /> </Columns> </asp:GridView> I built the stored procedure with a SQL Server 2005 Books Online example. As you can see, it returns the ROW_NUMBER for the
salespeople in AdventureWorks database, based on the year-to-date sales.
My intention is not to explain how set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
CREATE PROCEDURE [dbo].[sp_GridView_RowNumber]
(
@PageNum int,
@PageSize int,
@TotalRowsNum int output
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Use ROW_NUMBER function
WITH Salespeople_AdventureWorks As
(
SELECT 'RowNumber' = ROW_NUMBER() OVER(ORDER BY SalesYTD DESC),
'Name' = c.FirstName + ' ' + c.LastName, s.SalesYTD, a.PostalCode
FROM Sales.SalesPerson s JOIN Person.Contact c on s.SalesPersonID = c.ContactID
JOIN Person.Address a ON a.AddressID = c.ContactID
WHERE ((TerritoryID IS NOT NULL) AND (s.SalesYTD <> 0))
)
-- Query result
SELECT *
FROM Salespeople_AdventureWorks
WHERE RowNumber BETWEEN (@PageNum - 1) * @PageSize + 1 AND @PageNum * @PageSize
ORDER BY SalesYTD DESC
-- Returns total records number
SELECT @TotalRowsNum = count(*)
FROM Sales.SalesPerson s JOIN Person.Contact c on s.SalesPersonID = c.ContactID
JOIN Person.Address a ON a.AddressID = c.ContactID
WHERE ((TerritoryID IS NOT NULL) AND (s.SalesYTD <> 0))
END
The source code includes the solution in Visual Studio 2005 and the script for the stored procedure. You have to change your connection string in the code to run the sample.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||