The Code Module is used to add custom code or custom assemblies to a report. The Code Module allows you to add references to custom code embedded in a report or generated with custom assemblies.
Click on the outer grey surface of the report area, and then click the Properties icon to open the Report Properties.
The custom code option is listed under the Code
category. Click Code… to open the Code Module dialog.
In the code tab, type the code in the Visual Basic (VB) language.
The following code example demonstrates a custom function that returns the color string based on the given value range.
Public Function GetColor(ByVal TotalSales As Decimal) As String
Dim ColorName As String
If TotalSales > 0 And TotalSales < 1000 Then
ColorName = "Yellow"
ElseIf TotalSales > 1000 And TotalSales < 1500 Then
ColorName = "Blue"
ElseIf TotalSales > 2000 Then
ColorName = "Red"
Else
ColorName = "Green"
End If
Return ColorName
End Function
Click OK.
Use the following expression to call the custom code function in the report. Refer Expression section to learn more about handling expressions.
To view the color changes based on the price range in a report, click Preview
.
To embed custom assemblies in a report, create a simple Visual Studio class library project and define the required custom functions.
Create a Visual Studio class library. Go to Installed > Visual C#, and then select Class Library (.Net Framework) from the listed templates. Change the application name, and then click OK.
Open the class file and create a simple function as shown below.
After defining the required functions, build the project. Now, the assembly (.dll)
will be generated in the bin folder of the respective application.
Note: To embed the custom assembly into the report, the generated assembly must be installed in GAC, or it must be available in the reporting services
bin
location. We prefer manually copying and pasting the generated custom assembly into the following path: C:\Syncfusion\Report Server\ReportServer.Web\ReportService\bin.
Switch to the References tab.
To add an Assembly reference, Click on the Add icon.
Specify the assembly name and click OK.
Switch to the Classes tab.
To add a Class instance reference, Click on the Add icon.
Specify the assembly name, instance name and click OK. Now, the custom code assembly and instance are embedded into the report.
Use the following expression to call the embedded custom code function in the report.
Refer Expression section to learn more about handling expressions.
To view the color changes based on the price range in a report, click Preview
.
Download the above report design from link