Thursday, August 07, 2008

Creating a C# library and use it as custom function in Crystal Report

You can write your own functions in any .NET language like C# , vb.net and use it in crystal report as user defined fuction.
Here are the steps you need to follow:-

  1. Create a User Function Library .TheUser Function Libraries must be prefixed with CRUFL to be recognized by the embedded Crystal Reports Designer
  2. In the project property Build/Compile window select Register for COM Interop.Register for COM Interop" causes the assembly to be automatically registered by the regasm.exe utility, when the project is compiled. So, you do not need to run regasm.exe manually from the command prompt.
  3. Create an interface class with COM attributes.Above the interface declaration, create an attribute with three values: ComVisible, InterfaceType, and Guid (in C#).Create all the method signature.This method signature will become the name of the function that is exposed in the embedded Crystal Reports Designer.
  4. Create a Class and implement this interface.Above the class declaration, create an attribute with three values: ComVisible, ClassInterface and Guid (in C#).
  5. Create a strong name and attach it to the class library projcet to make this assembly as shared assembly.
  6. Install this assembly in GAC
  7. Now you can go to the Formula Workshop window and in side function -> Additional Functions ->Visual Basic UFLs (u2lcomm.dll) , you will find the functions defined in the assmbly.

Namastee !