12

I want to use Globals!PageNumber in Report body part. How can I access inside Report body?

I am using SQL Server Reporting Service 2008 R2.

Pedram
  • 6,256
  • 10
  • 65
  • 87
Mike
  • 317
  • 3
  • 7
  • 17

4 Answers4

19

Create functions in the code under the report properties:

Page Number:

Function PageNumber() As String    
    Return Me.Report.Globals!PageNumber    
End Function

Total Pages:

Function TotalPages() As String   
    Return Me.Report.Globals!TotalPages    
End Function

Access it in the body via an expression:

=code.PageNumber & " of " & code.TotalPages

Check out Sample Usage of the Concat Function

mmdemirbas
  • 9,060
  • 5
  • 45
  • 53
Struan
  • 254
  • 2
  • 4
  • You are correct. we need to implement custom code to access value of body part on header. Thank You. – Mike Aug 01 '12 at 07:08
  • 2
    hi i'm writing both the functions in report properties->code area and writing "=code.PageNumber & " of " & code.TotalPages" in textbox in report body part. it is showing "1 of 1" when total pages are 6.. what might be wrong? please suggest... – hemanth Jan 04 '13 at 14:37
  • 10
    Why is this answer accepted? It doesn't work, since page number can't be used in the report body. – pabrams Jan 13 '14 at 17:05
  • 12
    Doesn't work for me, always gives page 1 of 1 no matter how many pages. – Kevin Jul 25 '14 at 18:19
  • I'm using this method within an expression to hide a textbox on page 1 of a report that has the potential to go to multiple pages and it works. The expression I'm using is:- =IIF(code.TotalPages = 1 OR (code.TotalPages > 1 AND code.PageNumber <> code.TotalPages), true, false) – Neil Nov 21 '19 at 10:28
10

Unfortunately in Reporting Services (up to RS2008), this will produce "Page 1 of 1" on every page. The problem is that the body is rendered before the header and footer, therefore the code cannot access the correct pagination, since it is determined AFTER all elements in the body.

If your report is basically a large table with predefined number of rows on each table, try using row_number in your SQL as a workaround to manually calculate page numbers: http://social.msdn.microsoft.com/Forums/en-US/sqlreportingservices/thread/c2047eee-41a8-4d79-ae58-dbf60f6e7554/

Tingo
  • 472
  • 7
  • 11
6

you can't use page number in body. use it only in report footer or header.

masoud ramezani
  • 22,228
  • 29
  • 98
  • 151
  • See also the [corresponding MS Connect item](http://connect.microsoft.com/SQLServer/feedback/details/425893/global-page-number-value-to-be-accessed-from-body-section-in-ssrs-2008). – Jeroen Jul 04 '12 at 20:27
3

For that you need to use Report variables:

Go to Report Menu from main Menu in Visual Studio, > Click on Report Properties > Add new variable - named as PageCount (Default value to 0)

Then inside header of footer create one textbox and set below expression,

=Variables!PageCount.SetValue(Variables!PageCount.Value+1)

It will automatically increase for each page.

NOTE: Do not hide it from header or footer, the SetValue will not work if you hide the box, so change the font of textbox to white color. (Do whatever you want but just do not hide it. Then you can use below expression to fetch pagenumber value inside report body.

=Variables!PageCount.Value

I have taken reference from this answer.

Community
  • 1
  • 1
Pedram
  • 6,256
  • 10
  • 65
  • 87