Reporting Services allows you to write functions in C# or Visual Basic, then reference these as custom assemblies. This blog shows how - and why - you might do this.
- Creating Custom Assemblies in Reporting Services (this blog)
- Creating a Project, Class and Function in VB or C#
- Create and Deploy the Code DLL
- Referencing and Using Custom Assemblies in Reports
- Post-Build Events to Help Deployment
- Debugging Custom Assemblies
Posted by Andy Brown on 03 August 2012 | 6 comments
Creating Custom Assemblies in Reporting Services
In SQL Server Reporting Services (SSRS) you have access to a whole range of built-in functions. However, to release the full power of SSRS you'll need to create custom assemblies. This rather long blog will explain how - and why - you might do this!
Our Example - a Proper Case Text Function
Suppose that you frequently want to capitalise a word. SSRS includes functions to convert text to upper and lower case, but nothing to capitalise a word:
The LCase and UCase functions convert text to lower and upper case, but neither capitalises a word.
You could get round this by embedding a VB function in a report, looking something like this:
Embedded code is hard to read, limited to a single report and VB-specific.
However, the best solution is to create a function in VB (or C#), compile this into something called a DLL file and then reference this within your report. This is called a custom assembly.
Advantages of Custom Assemblies
There are a fair few hoops to jump through to create a custom assembly; why would you need one? Here are some reasons:
|Portability||Code that you embed within one report is obviously not available for other reports to use, whereas you can reference a custom assembly from any report.|
|Error-checking||When you type code into the Report Properties dialog box there's no way of knowing whether you've made any syntactical mistakes until you try to run it, whereas custom assemblies provide full Intellisense and code formatting.|
|Languages||You can write custom assemblies in either VB or C#.|
|Speed||It's not usually a big issue, but code in a custom assembly can be pre-compiled, and so will run more quickly.|
|Debugging||You can debug code using a custom assembly.|
Following the Rest of this Blog
To create a custom assembly you will need to do the following:
- Create a project, class and function.
- Build, deploy and reference your DLL.
- Use the custom assembly within a report.
- Deploy your project (perhaps automatically, using post-build events).
- Learn how to debug a custom assembly.
Let's begin by creating a project, class and function in Visual Studio.
This blog assumes that you already know basic programming in either VB or C# (examples are given for both languages).
This blog has 6 comments
CAN you handle events like printing? If you add a comment giving a summary of what you mean, or a link to a relevant website, I'll certainly consider this. There's always more to learn about SSRS!
If anyone can improve on this, please add your penn'orth!
You could create a true/false (Boolean) report variable whose expression would be set to the value of a custom assembly function (which could look at the Oracle database). The value of this variable would be calculated once only, when the report first loads. You could then set the visibility of all relevant parts of the report to be the value of this variable.
Not for the faint-hearted - you'll need to learn about custom assemblies and report variables - but it should work OK!
Great question - rubbish answer coming up! When I'm training on SQL or SSRS, I always emphasise that there is one area I can't help with: security. I too tear my hair out with problems like this. The only thing I can suggest is copying your question to StackExchange or a similar site. Unless any other readers can help?
First and foremost, a great big THANK YOU to you and your team for your blogs and youtube videos...they are truly appreciated!! i've learned a great deal from them and your teaching methods are straightforward and thorough and easy to understand. i am actually considering coming to UK to take one of your courses in person!
I have a report uses a de-normalized table for the data. i created the table using data from 6 different business sections within our company, and because of this not all sections have data in all fields. for example, fields 'category' and 'type' have data for Membership, but for Certification there is data in 'category' only. my objective is to write custom code that allows for 'optional' columns on the report if data exists in the 'category' and 'type' fields. is this possible? i know i'll need custom code to accomplish this, but first i need to know if it's even possible.
By the way, i'm only 9 months into my SQL/SSRS career, and i am primarily self-taught (with your help, of course!!) - the fact that i'm even thinking of tackling something complex like this is a tribute to your teaching...the possibilities are endless!! :)
I'll be honest and admit I don't fully understand what you're trying to achieve, but I would have thought you could do it without any custom code. Couldn't you right-click on a column of your report and set the column visibility to be an expression depending on the underlying data fields? If you do have to use custom code, I am convinced that ANYTHING is possible!
"Error while loading code module: 'SSRSFunctions, Version=188.8.131.52, Culture=neutral, PublicKeyToken=null'. Details: could not load file or assembly 'SSRSFunctions, Version=184.108.40.206, Culture=neutral, PublicKeyToken=null' or one of it's dependencies. The system could not find the file specified"
Obviously the above error indicates that SSRS cannot find my assembly, however I followed your directions to the letter. I have my SSRSFunction.dll in the C:\Program Files\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Reporting Services\ReportServer\bin folder and am referencing this file in my .rdl file, so I'm at a loss here.
Any ideas what else might be causing this?
A full-blown discussion forum is being built for this site, which will allow you once more to add comments and discussion threads.