Custom Code in SSRS Report

An expression is a perfect solution for most of the reports that we develop in real-time. But in some cases, we might need complex functionalities or calculations. In these situations, we can use the concept of Custom Code in SSRS.

SSRS Custom Code creates customized functions that incorporate in the report. Or create DLLs (Class libraries), so that we can reuse the same function in multiple reports.

In this article, we will show you the step by step approach to adding the custom code in SSRS report with a practical example

To demonstrate the steps involved in adding the Custom code in SSRS Report, We are going to use the previously generated report that we shown below. Please refer to Formatting Date and Time article to understand the SSRS report data.

Custom Code in SSRS Report 1

Let me show you the report preview. As you can see, it is pretty ordinary Table Report with formatted date and time

Custom Code in SSRS Report 2

How to add Custom Code in SSRS Report

To add the custom Code in SSRS, we have to access the Report properties. And to do so, Right-click on the Empty space within the report designer will open the context menu. Please select the report properties option.

Custom Code in SSRS Report 3

Or go to the Reports menu and select the Report Properties submenu as we showed below.

Custom Code in SSRS Report 4

Either way, it will open the following window.

Custom Code in SSRS Report 5

Please navigate yourself to the Code tab to write the custom code in a report.

Custom Code in SSRS Report 6

Here, we are writing a simple function that will return the message “Welcome to Tutorial Gateway”. And the code is:

Function WelcomeMsg() As String
	return "Welcome to Tutorial Gateway"
End Function
Custom Code in SSRS Report 7

We have completed creating the function. Now let me use that function inside the report. To do so, add a TextBox to the Page Header.

Next, right-click on the Textbox will open the context menu. Please select the Expression option from it.

Custom Code in SSRS Report 8

After you select the Expression option, a new window will open to write an expression. Here, write Code.WelcomeMsg (i.e., Code.FunctionName) and click OK.

Remember, the syntax to add the custom code is Code.Function_Name(Parameter1,….,ParameterN))

Custom Code in SSRS Report 9

Now, let me show you the report preview after adding SSRS custom code. From the below screenshot, you can see the message that we created inside the function.

Custom Code in SSRS Report 10

Next, let me add one more function to the Report properties code.

Function GreetingMsg() As String
        If (DateTime.Now.Hour < 12) Then
            Return "Good Morning Suresh"
        ElseIf (DateTime.Now.Hour < 17) Then
            Return "Good Afternoon Suresh"
        Else
            Return "Good Evening Suresh"
End If
End Function
Custom Code in SSRS Report 11

Next, add the following code to the second TextBox in the page header

Custom Code in SSRS Report 12

As you can see, we formatted the textBox

Custom Code in SSRS Report 13

Let me show you the report preview after adding custom code in SSRS report

Custom Code in SSRS 14