Using Embedded Code in SQL Server Reporting services (SQL Server 2008)

Sometimes we may require using the same expression at many places in a report. Off course se can use the same expression at every place. But imagine the pain if we need to change the expression? In such situations it will be a good practice to use embedded code.

In this article we will see a step by step implementation of Embedded Code

In the report I am using a count field and will change the font color depending upon the value for the field

In your project, Go to Report, Report Properties and select Code tab

clip_image002

The count is 1: font color is red

The count is from 2 till 10: font color is yellow

The count more than 10: font color is green

Add following code to Custom Code

Function ReturnColor(ByVal count As Integer) As String
Dim returnValue As String
Select Case count
Case Is
=1
returnValue = "Red"
Case Is < 10
returnValue = "Yellow"
Case Is > 10
returnValue = "Green"
End Select
Return
returnValue
End Function

In order to access this function in code, we need to enter following in font expression

=Code.ReturnColor(Fields!<Field with count>.Value)

The limitation of using embedded code is it can only be used in the report where you have declared it. In next article we will see how to add custom assembly, which can be used for multiple reports.


No comments: