Davy Knuysen

SQL Server, .Net, ...
posts - 59, comments - 31, trackbacks - 4

My Links

News



Archives

Post Categories

Blogs I read

SQL Server Reporting Services: Create and Call a Custom Assembly

In one of my previous articles I explained how to use embedded code in SQL Server Reporting Services. The possibility to add embedded code to a report, is a very powerfull feature to add some custom functionality to your report.
But sometimes it might be necessary to add some custom functionality that is too complicated for embedded code to handle efficiently, or you need to access the same function from multiple reports, or you would like to write you code in C#...
In one of these cases you should develop a custom assembly to call from your report.

Let’s start with a simple example and develop a function PercentageToColor() to return a color based on a numeric value representing a percentage.

namespace MyAssembly

{

    public class Colors

    {

        public string PercentageToColor(double percentage)

        {

            string returnValue = string.Empty;

 

            if (percentage < 20)

            {

                returnValue = "red";

            }

            else if (percentage < 80)

            {

                returnValue = "blue";

            }

            else

            {

                returnValue = "green";

            }

 

            return returnValue;

        }

    }

}

Once you’ve compiled your class you need to copy the resulting assembly to the directory in which it is accessible from within your report:

·         To use it in the report designer, you need to copy it to:
 
C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies

·         To make it available for reports that have been deployed to the report server, you need to copy it to:
 C:\Program Files\Microsoft SQL Server\MSSQL.3\Reporting Services\ReportServer\bin

 

 

Before you can access your custom assembly you have to reference the assembly: Open the report properties (Menu: Report – Report Properties) and select the References tab.

Browse to your assembly and define a Class Name and an Instance Name. (The Class Name and the Instance Name or only for non-static methods). Make sure to prefix your class name with the assembly name.

Now you can call the methods in your assembly from your report, using an expression:

·         To call a static method: =<AssemblyName>.<ClassName>.<StaticMethodName>

·         To call an instance method: =Code.<InstanceName>.<PublicMethodName>

So in our example this would be: =Code.TestColor.PercentageToColor(Fields!Percentage.Value)

 That's it! You've just created and called a custom assembly from your report...

 

 In one of my future articles I will explain how to pass parameters to the class constructor and I will also tell something about SSRS and .Net Permissions...

 

 

Davy Knuysen

Print | posted on Monday, March 26, 2007 10:51 PM | Filed Under [ .Net SQL ]

Feedback

Gravatar

# re: SQL Server Reporting Services: Create and Call a Custom Assembly

Thank you
2/13/2008 4:26 PM | siva
Gravatar

# re: SQL Server Reporting Services: Create and Call a Custom Assembly

Hi, great tutorial! I have one problem though. Since SSRS does not provide a localization service, I've developed my own assembly to address that. On my assembly, I have two resource files (.resx), one for English language and one for Portuguese, both are set to Embedded Resource. In this assembly I have a method that given a language and an acronym, I return the translation in the specified language. I have managed to reference the assembly correctly using your tutorial, and my report successfully calls the assembly, but I only get English translation, even if a provide "pt-BR" to my method. I don't know if this is a security problem with SSRS. It's worth saying that when I compile my assembly, it generates a folder named "pt-BR" under the directory of the assembly. I think that maybe the SSRS doesn´t recognizes the "pt-BR" resource. Do you have any clue? Thanks a lot!
12/4/2008 1:32 PM | Emanoel Barreiros
Gravatar

# re: SQL Server Reporting Services: Create and Call a Custom Assembly

Good tutorial man !! keep it going.
1/30/2009 6:07 AM | Jayesh
Gravatar

# re: SQL Server Reporting Services: Create and Call a Custom Assembly

Hi
I have an assembly which has a class and a method that invokes WCF service. I referenced this assembly in my report. What I require is when user clicks on a given column which has say hyperlink in the table, method should be invoked with sends information to WCF service.

Is that achieval?
5/29/2009 6:20 PM | Bhavesh

Post Comment

Title  
Name  
Email
Url
Comment   

Powered by: