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

About these ads

7 Responses to “How to use Custom Assemblies with Reports”

  1. puromtec Says:

    Thanks a million. Too bad people like yourself aren’t writing documentation for Microsoft. I constantly give them 1 star for documentation and leave nasty feedback about thier non-informative documentation.

  2. puromtec Says:

    I didn’t get mine to work until I added the dll to the GAC, in addition. Wierd.

  3. Kamozz Says:

    You saved me man,
    a big thank you.

  4. Ghassan Kahool Says:

    I really so happy to find the resolve of that problem

    Thans

  5. Rick Knoebel Says:

    Excellent! This showed me what I needed to know about trusted assemblies where other descriptions had failed. Thank you!

  6. Mj. Logan Says:

    Your walkthrough was very useful, thanks a lot!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 106 other followers

%d bloggers like this: