Chapter 3 - Embedding Code in Reports
Chapter 3 - Embedding Code in Reports
3.1Our Example
To illustrate how embedding code works (and why you might do it), consider this report:
The easiest way to understand this expression (which gives a person’s true age) is by example. If “today” is 21 June 2012, here is how the 3 ages shown boxed above were calculated by SSRS:
Actor / Date of birth / Age / WhyBen Affleck / 15/08/1972 / 39 / The difference in years between 1972 and 2012 is 40, but because month 8(August) comes after today’s month 6 (June), we subtract 1.
Selma Blair / 23/06/1972 / 39 / The difference in years between 1972 and 2012 is 40, but because Selma Blair’s month of birth (6) equals the current month and her birthday number (23) is greater than today’s day number (21), we subtract 1.
Karl Urban / 07/06/1972 / 40 / The difference in years between 1972 and 2012 is 40. Because Karl Urban’s birth month (6) is the same as the current month number, and his birthday number (7) is less than the current day number (21), no further adjustment is needed.
The expression to calculate someone’s age is hard to read and difficult to reuse somewhere else within the same report; because of this it makes sense to embed it as a code function.
3.2Creating Embedded Code
This page shows how to create and embed code for a report.
Step 1 - Designing the Function Outline
Every function takes in zero, one or more inputs (called the arguments), and spits out a single value. Here’s what our function will do:
Here’s how you would write this in Visual Basic:
Step 2 – Coding the Function
Here’s what our proposed function could look like (this chapter assumes that the reader has some familiarity with basic programming concepts)::
Digression: An Overview of Data Types
When declaring data types for arguments or functions, you can use the following data types (there are many more, but they are nearly all just variant of the ones listed below):
What to use / What it denotes / ExampleBoolean / Anything which is either True or False / Whether an actor is still alive
Integer / Any whole number / The length of a film in minutes
Double / Any floating point number (such as 3.14) / The budget for a film in dollars
Date / Any date or time / The date a film was released
String / Any string of text characters / The name of a film
Step 3 - Embedding the Code in a Report
Having created a function, it’s time to embed it in a report:
If you’ve created a class (see later in this chapter for how to do this) and pasted in the code from there, it will look pretty initially, but soon lose its colouring and formatting:
Step 4 –Referring to your Code
Now that you’ve created your code, you can refer to it anywhere within your report:
Step 5 – Adding More Functions
You can have lots of functions in the Code window, although you may find them hard to read:
3.3Writing Code within RDL
Writing code within the Report Properties dialog box isn’t easy. An alternative is to write it directly into the Report Definition Language (RDL) of your report:
3.4Writing Code within Classes
You really don’t want to be writing code in the Report Properties dialog box! A better alternative is to create a class, and write code within that.
Creating a Class Library
The first thing to do is to create a separate project for writing code. To do this:
Typing in Code
You should now have a default class created for you:
You can now type in any code you like, making full use of Visual Studio’s excellent Intellisense:
When you’ve finished, copy and paste your code into the Report Properties dialog box:
© Copyright 2019 Page 1