Thursday, May 28, 2009

Programmatically/Dynamically building an RDLC

I was designing a Local Report when I stumbled upon a shortcoming of the Report Rendering Engine. A Rectangle has a Hidden property. When you set the Hidden property to True, the content of the Rectangle doesn't render, but the space of the content is preserved. In my case, this caused some white pages. So I looked for a way to build a RDLC programmatically. An RDLC is just an XML, so how hard could it be :).

I first searched for an example on MSDN, but I didn't like the way described there. In this article on MSDN they use a XmlTextWriter, which makes the code hard to read and not very maintainable in my opinion. You can find that MSDN article here.

After some further investigation I found the RDLC XSD on MSDN. You can find the XSD here.

I used the xsd.exe Tool to generate a Class based on the XSD. This way you can create an RDLC in a more Object Oriented way, which leads to more maintanable code.

It would be awesome if someone had the courage to add comments, so Intellisense would be able to help creating RDLCs programmatically.

After adding the Report.cs to your solution, you can start making your own RDLCs programmatically. Note that the ReportViewer needs a Stream to read the RDLC.

I chose not to show my whole implementation of my own ReportGenerator, because it's not really a good example. These steps might get you started though.

1. Create a new Report object.
2. Do whatever you want with the object.. Add textboxes, rectangles..
3. Before reading the Report object into the ReportViewer you need to serialize the object to a Stream. I chose to use a MemoryStream. (Don't forget to set the position of the Stream to 0!)

4. Load the Local Report in the ReportViewer using the LoadReportDefinition method.

And that should be it!

Tuesday, May 12, 2009

The report definition for report 'path' has not been specified

The full errormessage is:
The report definition for report 'path' has not been specified. Could not find file 'path'.

If you use an installer to install your webapplication, this error might occur when you try to run a local report (rdlc).

The solution is to set the property "Build Action" of the rdlc to "Content". Now your installer will copy the physical rdlc.

Thanks to my colleague Kris S.