Adding Custom Code to a Report (Visual Studio Report Designer)
You can add custom functions to a report or add references to functions in external assemblies. When the project is compiled, the following assemblies are referenced automatically: Microsoft.VisualBasic, AllMembers.T:System.Convert, and AllMembers.T:System.Math. Additional assemblies may be referenced using the Report Properties dialog box or the CodeModules element in the report definition. The Report Properties dialog box can also be used to define custom functions. To define custom functions in the report definition, use the Code element. Note that functions cannot be passed sets of data values (specifically, custom aggregates are not supported).
You can write custom code that is used in expressions throughout a report. You can do this in two ways: embedding code within a report, or by referring to methods within a custom assembly. Use embedded code for complex functions or functions that are used multiple times in a single report. Use custom assemblies to maintain code in a single place and share it across multiple reports.
Embedded Code
To use code within a report, you add a code block to the report. This code block can contain multiple methods. Methods in embedded code must be written in Microsoft Visual Basic and must be instance-based.
To add code to a report
On the Report menu, click Report Properties.
Note
If the Report menu is not available, click within the report design area.
On the Code tab, in Custom code, type the code.
Methods in embedded code are available through a globally defined Code member. You access these by referring to the Code member and method name. The following example calls the method ToUSD which converts the StandardCost field value to a dollar value:
=Code.ToUSD(Fields!StandardCost.Value)
Custom Assemblies
To use custom assemblies in a report, you must first create the assembly, make it available to your project, add a reference to the assembly in the report, and then use an expression within the report to refer to the methods within that assembly. When the report is deployed to the report server, you must also deploy the custom assembly to the report server.
To add an assembly reference to a report
On the Report menu, click Report Properties.
Note
If the Report menu is not available, click within the report design area.
On the References tab, do the following:
In References, click the add (...) button and then select or browse to the assembly from the Add Reference dialog box.
In Classes, type name of the class and provide an instance name to use within the report.
Note
Specify a class and instance name only for instance-based members. Do not specify static members in the Classes list.
To refer to custom code in an expression, you must call the member of a class within the assembly. How you do this depends on whether the method is static or instance-based. Static methods within a custom assembly are available globally within the report. You can access static methods in expressions by namespace, class, and method name. The following example calls the method ToGBP, which converts the StandardCost field value from dollar to pounds sterling:
=CurrencyConversion.DollarCurrencyConversion.ToGBP(Fields!StandardCost.Value)
Instance-based methods are available through a globally defined Code member. You access these by referring to the Code member, and then the instance and method name. The following example calls the instance method ToEUR which converts the StandardCost field value from dollar to euro:
=Code.m_myDollarCoversion.ToEUR(Fields!StandardCost.Value)
Note
In Report Designer, a custom assembly is loaded once and is not unloaded until you close Visual Studio. If you preview a report, make changes to a custom assembly used in the report, and then preview the report again, the changes will not appear in the second preview. To reload the assembly, close and reopen Visual Studio and the preview the report.
To learn more about using custom assemblies, you can search for "Using Custom Assemblies with Reports" in SQL Server 2005 Books Online. Books Online is available in most editions of Visual Studio 2008. You can also view it on MSDN at www.microsoft.com. The information about custom assemblies in Books Online applies to reports that you deploy with the ReportViewer controls.
See Also
Reference
Edit References Dialog Box (Visual Studio Report Designer)
Edit Classes Dialog Box (Visual Studio Report Designer)
Concepts
Using Expressions in a Report (Visual Studio Report Designer)