Adding Embedded Code


To get a better understanding of how embedded custom code is used in a report, report developers can assume all of the code placed in the code window gets compiled into a make-believe class called Code . You can add properties and methods to the make-believe Code class, and call class members from expressions in the report, which has the class defined.

Embedded code is compiled into a report's intermediate format and gets executed as needed when the report is rendered. Report Designer provides a simple text area to allow editing of the embedded code. To access the code editor, complete the following steps:

  • Use the Report, Report Properties menu (if the Report menu is not available, click within the report design area). Alternatively, right-click on the area surrounding the report's body and select the Properties menu.

  • Click the Code tab in the Report Properties dialog box.

You should see a window similar to Figure 22.1 and can type the code in the Custom code area.

Figure 22.1. Code editor


The code editor is basically a multiline text box, and it does not provide any productivity assistance, such as Intellisense or debugging. You have to be extremely careful while using this code editor. For example, only one level of "undo" action is allowed, as compared to Visual Studio's infinite undo.

For all of its difficulties, embedded code provides several key benefits for a developer, including the following:

  • A more elegant approach (as compared to expressions) to medium-complexity coding scenarios

  • A single repository for functions and methods that can be reused by multiple items within a single report, as opposed to complex copy-and-paste expressions

  • Direct access to the exception handling functionality of VB.NET

Every developer knows function reuse is beneficial over copy-and-paste programming. Take the following fictitious example. Suppose two developers were assigned the parallel development of a single report. One developer used embedded code, whereas the other one used simple expressions. One of the initial goals was to highlight negative values in red. Later, this goal changed to a more complex requirement, such as color -coding numeric ranges. The developer who used embedded code could adapt to the change in requirements quickly, and would make his manager happy, probably getting a bonus in the process.

Embedded functions must be written in VB.NET. If you prefer C#, you would have to develop a custom assembly. This topic is covered in Chapter 23, "How to Create and Call a Custom Assembly from a Report."

The following steps add the function used in the preceding narrative:

1.
Open the report properties by either clicking the Report, Report Properties menu, or right-clicking on the area surrounding the report's body and selecting Properties.

2.
In the Report Properties dialog box, click the Code tab and enter the following-function:

 Function Highlight(value As Integer) As String             If value < 0             return "Red"          Else             return "Black"          End If       End Function 

3.
Drag a Textbox from the Toolbox to the report and place the following code in the Background Color property:

 =Code.Highlight(me.value) 

4.
Place -1 (minus one) in the Value property.

5.
Open the report in Preview mode to see the text box with a red background.



Microsoft SQL Server 2005 Reporting Services
Microsoft SQL Server 2005 Reporting Services
ISBN: 0672327996
EAN: 2147483647
Year: 2004
Pages: 254

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net