How to use Custom Assemblies with Reports

Last days I tried to write a sql server report. In the beginning everything was fine. Until – I wanted to call a custom method in a custom assembly of my project.

My first thought was adding the referenced assembly within the sql report and than calling the method in one of my textboxes.

Report Properties

As a result, I got many error messages that told me, that the method name does not have been declared. But the first error message was following:

Error while loading code module: ‘TpUtils, Version=1.90.0.0, Culture=neutral, PublicKeyToken=6d1c15bd239054aa’. Details: Could not load file or assembly 'TpUtils, Version=1.90.0.0, Culture=neutral, PublicKeyToken=6d1c15bd239054aa' or one of its dependencies. Das System kann die angegebene Datei nicht finden.

Ok, got it. It’s not enough to reference the custom assembly.
But where the hell is .NET searching for it? After raking up the internet I found the missing piece. The build process is searching for the custom assembly in a folder called “PrivateAssemblies”. The solution was to establish a post build event that copies the custom assembly to that folder.

Post Build Event

Build succeeded! Now I can run the report – I thought. But the report viewer only showed the following message: The report references the code module … which is not a trusted assembly.

No trusted assembly

Ok. So raking up the internet again. The solution was to tell the report viewer, that our assembly is a trusted assembly. To do this, I had to call the method AddTrustedCodeModuleInCurrentAppDomain.

reportViewer.LocalReport.AddTrustedCodeModuleInCurrentAppDomain
("TpUtils, Version=1.90.0.0, Culture=neutral, PublicKeyToken=6d1c15bd239054aa");

I compiled again and wow – the report came up. But instead of calling the method, the output console showed a message like that:

Warning: The BackgroundColor expression for the textbox ‘LogonName’ contains an error: Die Assembly lässt keine Aufrufer zu, die nicht voll vertrauenswürdig sind. (rsRuntimeErrorInExpression)

But what the hell … ? My Assemblies are fully trusted and signed, so go ahead! But .NET was stolid like a donkey. After searching the internet again, the solution I found was to put the following assembly attribute to my “signed and fully trusted” custom assembly.

[assembly: System.Security.AllowPartiallyTrustedCallers]

And now … Yeah. The report is rendering correctly.

Reporting result

But you may ask: “What is the method I wrote good for?” The simple answer. I have done this for colouring the rows by using a property of the bound objects. Which is a functionality that is not build in.

If you are interessted, here’s the custom method I wrote:

public static string ToKnownColor(int color)
{
return string.Concat("#" , Color.FromArgb(color).Name.Substring(2));
}

Crazy – how many problems can arise until this small piece of code could work.

Wish you a pieceful and bug free christmas.
Cheers

- Gerhard

kick it on DotNetKicks.com