Embedded Code in SQL Server Reporting Services

Sometimes it might be necessary to use the same expression on different places throughout a report. You can of course just copy and paste the expression every time you need it, but if you’ll ever need to change this expression, you will have to change it for every field you’re using it for. Therefore it’s good practice to write this expression just once and refer to it where necessary. This can be done by implementing embedded code, that can be entered through the report’s property dialog on the Code tab.

 

 

(Click “Report”, “Report Properties” and select the Code tab)

 

                

 

Assume you have some calculated percentage fields and you need to set the background color depending on it’s value:

-          when the value is less then 20% : background must be red

-          when the value is between 20 and 80%:  background must be orange

-          when the value is greater then 80%: background must be green

 

Add the following function to the custom code textbox:

Function GetColor(ByVal percentage As Double) As String

        Dim returnValue As String

 

        Select Case percentage

            Case Is < 20

                returnValue = “red”

            Case Is < 80

                returnValue = “orange”

            Case Is > 80

                returnValue = “green”

        End Select

 

        Return returnValue

End Function

 

Now you can access this function as a member of the class called “Code”:

=Code.GetColor(Fields!CalculatedPercentage.Value)

 

The Embedded Code Window is a very handy tool to quickly add some easy functions to your report.

But at this time, Embedded Code only supports Visual Basic and the Embedded Code Window is nothing more then a large textbox, without Intellisense or any debugging info.

Embedded Code can also only be reached from within the same report.  

Therefore if you want multiple reports to access the same code, or you would like to write your code in C#,  or do some really advanced things, you should consider accessing a .NET assembly from the embedded code, which I will explain in one of my future articles…

 


  • Share/Bookmark
No comments yet.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>