Search code examples
c#.netvisual-studio-2008rdlreport-designer

How to use custom utility classes in RDL?


how do you use your own utility classes (those you use in application code) in Microsoft reporting in local mode? Is it even possible?

I have read some specification and there are some elements that indicate it can be done but it is not really clear on what to do.

Simply calling the code does not work even if I call it with namespace prefix.


Solution

  • Let's say you have an assembly fully qualified as ReportUtils, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null with your utility methods.

    It has 2 classes:

    • public static class ReportUtils.Foo with a public method string GetValue()
    • public class Bar with a parameterless constructor and a public property Id.

    Visual Studio 2008

    Report -> Report Properties -> References - References section.

    RDL xml

    add

    <CodeModules>
        <CodeModule>ReportUtils, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</CodeModule>
    </CodeModules>
    

    under the Report element.

    You can also add instances of your classes that will get auto-constructed when you report is loaded:

    Visual Studio 2008

    same menu as before - Classes section.

    RDL xml

    add

    <Classes>
        <Class>
            <ClassName>ReportUtils.Bar</ClassName>
            <InstanceName>barInstance</InstanceName>
        </Class>
    </Classes>
    

    under the Report element.

    You can use your static utility method like this:

    <Value>=ReportUtils.Foo.GetValue()</Value>
    

    You can use your class instance like this:

    <Value>=Code.barInstance.Id</Value>
    

    You have to add your assembly as trusted to the LocalReport instance you are using:

    localReport.AddTrustedCodeModuleInCurrentAppDomain("ReportUtils, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
    

    You may have to copy your ReportUtils assembly to your VS2008\Common7\IDE\PrivateAssemblies directory so you can design your reports in Visual Studio's report designer without errors.

    I have used this recently so this should be all the necessary steps to achieve your goal.