Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
public List<Model.ReportsDashboard> GetList(string whereCriteria, string orderBy, string direction, int pageSize, int pageNumber)
        {
            switch (orderBy)
            {

                case "ReportsDashboardID":
                    orderBy = "dbo.Reports_Dashboard.id";
                    break;
                case "ReportUrl":
                    orderBy = "dbo.Reports_Dashboard.ReportUrl";
                    break;
                case "ReportName":
                    orderBy = "dbo.Reports_Dashboard.ReportName";
                    break;
                default:
                    orderBy = "dbo.Reports_Dashboard.ReportName";
                    break;
            }

            if (String.IsNullOrEmpty(direction))
                direction = "ASC";

            if (String.IsNullOrEmpty(whereCriteria))
                whereCriteria = "dbo.Reports_Dashboard.Active = 1";

            string sql = @"
                Declare @RowStart int 
                Declare @RowEnd int 
                
                SET @RowStart = @PageSize * @PageNumber + 1; 
                SET @RowEnd = @RowStart + @PageSize - 1 ; 

                With Report_Dashboard AS 
                     ( SELECT ROW_NUMBER() over (order by " + orderBy + " " + direction + @") as RowNumber,
			               dbo.Reports_Dashboard.id, 
		                   dbo.Reports_Dashboard.ReportName, 
		                   dbo.Reports_Dashboard.ReportUrl
		               FROM  dbo.Reports_Dashboard
                       where " + whereCriteria + @") ,
               counts as (select count(*) as TotalRecordCount from Report_Dashboard)

               select Report_Dashboard.*,counts.TotalRecordCount
               from Report_Dashboard
                    cross apply counts 
                Where RowNumber >= @RowStart and RowNumber <= @RowEnd 
            ";

            List<SqlParameter> sqlParameters = new List<SqlParameter>();

            sqlParameters.Add(new SqlParameter("@PageNumber", pageNumber));
            sqlParameters.Add(new SqlParameter("@PageSize", pageSize));

            LoadRecordMethod LoadReport = (SqlDataReader dr) =>
            {
                Model.ReportsDashboard loadreportdashboard = new Model.ReportsDashboard();
                LoadField(loadreportdashboard, dr, GetName(() => loadreportdashboard.id));
                LoadField(loadreportdashboard, dr, GetName(() => loadreportdashboard.ReportName));
                LoadField(loadreportdashboard, dr, GetName(() => loadreportdashboard.ReportUrl));
                LoadField(loadreportdashboard, dr, GetName(() => loadreportdashboard.TotalRecordCount));
                return loadreportdashboard;
            };

            List<Model.ReportsDashboard> list = this.ReturnList(new LoadRecordMethod(LoadRecord), sql, sqlParameters);

            return list;
        }
Posted
Updated 29-Sep-15 22:27pm
v2
Comments
F-ES Sitecore 30-Sep-15 4:29am    
The issue will be a combination of your data and your query. We don't have access to your database so don't know why you're only getting 7 records. Try a very simple version of your query and then build it up bit by bit and that will give you a better understand of what is going on.

1 solution

Look at your sql query - see
Where RowNumber >= @RowStart and RowNumber <= @RowEnd

@RowStart and @RowEnd are calculated from the parameters pageSize AND pageNumber

So for the first page pageNumber should be set to 1 and you will return the first 7 records from your database. For the second page pageNumber = 2 and you will retrieve records 8 to 14 inclusive.

The code is clearly working as it was intended to do - the SQL is returning the number of records that can be displayed on each page and as you work through the pages it returns the "next" set of records.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900