Search results
Suggest a FeaturePDF

Code Module

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.

Add Custom Code to a report

  1. Click on the outer grey surface of the report area, and then click the Properties icon to open the Report Properties. open-properies

  2. The custom code option is listed under the Code category. Click Code… to open the Code Module dialog. report-properies

  3. In the code tab, type the code in the Visual Basic (VB) language. Open code module

  4. 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

    Enter custom code

  5. Click OK.

  6. Use the following expression to call the custom code function in the report. Call custom code function Refer Expression section to learn more about handling expressions.

  7. To view the color changes based on the price range in a report, click Preview. Preview custom code

Add Assembly References

To embed custom assemblies in a report, create a simple Visual Studio class library project and define the required custom functions.

Create a custom assembly

  1. 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. To create c sharp class library

  2. Open the class file and create a simple function as shown below. To create a simple function

  3. 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.

Adding References to Custom Assembly

  1. Switch to the References tab. Code module assembly references

  2. To add an Assembly reference, Click on the Add icon. Add assemby references

  3. Specify the assembly name and click OK. Given assemby references name

Add Class Instances

  1. Switch to the Classes tab. Switch to class instance tab

  2. To add a Class instance reference, Click on the Add icon. Code module class instance

  3. Specify the assembly name, instance name and click OK. Now, the custom code assembly and instance are embedded into the report. Added class instance name

  4. Use the following expression to call the embedded custom code function in the report.

    Call the method using expression Refer Expression section to learn more about handling expressions.

  5. To view the color changes based on the price range in a report, click Preview. Preview the record

Download the above report design from link