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. We can use this concept to create custom functions which are incorporated in the report, or create a custom dlls (Class libraries) to reuse the same function in multiple reports.
In this article we will show you the step by step approach to add the custom code to the SSRS report with 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 Formatting Date and Time article to understand the report data.
Let me show you the report preview. As you can see, it is pretty ordinary Table Report with formatted date and time
Add Custom Code in SSRS Report
In order to add the custom code, 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 as we shown below.
Or go to the Reports menu, and select the Report Properties sub menu as we shown below.
Either ways, it will open the following window.
Please navigate yourself to the Code tab to write the code.
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
We are done with the creating function. Now let me use that function inside the report. To do so, add a Text Box to the Page Header.
Next, right-click on the Text box will open the context menu. Please select the Expression option from the context menu as we shown below.
After you select the Expression option, a new window will be opened 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))
Now let me show you the report preview. From the below screenshot you can see the message that we created inside the function.
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
Next, add the following code to the second Text Box in the page header
As you can see, we formatted the text Box
Let me show you the report preview
Thank You for Visiting Our Blog