Pro SQL Server 2012 Reporting Services

(sharon) #1

CHAPTER 7  USING CUSTOM .NET CODE WITH REPORTS


a task in an SSRS report, you probably shouldn’t take on the added overhead of in report code or a
custom assembly.

Using Embedded Code in Your Report

Using embedded code is by far the easiest way to implement custom .NET code in your reports, for two
main reasons. First, you simply add the code directly to the report using the Report Designer’s user
interface (UI) in either BIDS or Visual Studio. Second, this code becomes a segment within the report’s
RDL file, making its deployment simple because it is a part of your report and will be deployed with it in
a single step.
Although embedded code is easier to use, there are a few considerations that you should take into
account:

Embedded code must be written in VB .NET. If you are a C# programmer or use
some other .NET–compatible language as your primary development language,
this may force you to use the custom assembly for all but the simplest of
functions. Chances are that if you are developing complicated code, you will
want to go with the custom assembly route anyway for the debugging and
source control options.

All methods must be instance based. This means the methods will belong to an
instantiated instance of the code object and you cannot have static members.

OnlY basic operations are available. This is because, by default, code access
security will prevent your embedded code from calling external assemblies and
protected resources. You could change this through SSRS security policies, but
it would require granting FullTrust to the report expression host, which would
grant full access to the CLR and is definitely not recommended. If you need
these capabilities, use custom assemblies so you can implement security
policies to grant each assembly only the security it needs. You will look at
custom assemblies and how to set security for them in the “Deploying a
Custom Assembly” section.

Before you run the examples for this chapter, make sure to read the ReadMe.htm file included with
the sample code, available for download from the Source/Download page at http://www.apress.com. It is
located in a file in this chapter’s samples root folder. If you have the code open in Visual Studio 2010, it
will be under the Solution Items folder. It contains setup and configuration steps that are required
before running the examples.
Let’s take a look at how this feature of SSRS works by adding some embedded code to one of the
reports you have already created. In this case, start with the sample Employee Service Cost report
included with this chapter. It is a slightly modified version of the Employee Service Cost report you
created in Chapter 6. We will show you how to use the embedded code feature to add a function that will
determine whether you have exceeded a certain cost per visit for a type of treatment in a given time
period. You will then use that function to determine the color of one of the text fields in the report to
help draw attention to those specific treatment types.
Free download pdf