I was doing some testing the other day with a ReportViewer control hosted in a WinForms application to do local mode report processing. As some of my reports had a drillthrough / navigation option set, I had setup a set of DrillthroughEventHandler to ensure that the right datasources are bound to the report when the drillthrough reports are invoked.
The problem in my case was that on specific reports being invoked, the drillthrough would cause an exception inside ReportViewer and it would display:
An error occurred during rendering of the report.
Object reference not set to an instance of an object.
If this issue was random I would have suspected issues like the one in http://support.microsoft.com/kb/959595 but I was able to get this error consistently. On later troubleshooting it was clear that:
- I had two DrillthroughEventHandler setup, but only one for each report should have been ‘active’.
- Unfortunately the way you setup these handlers is you use the following construct:
reportViewer1.Drillthrough += new DrillthroughEventHandler(CorrectDrillthroughEventHandler)
- Due to the way my code was structured, it turned out I would add the 2nd DrillthroughEventHandler to a report which never really needed it. So I ended up making sure that the unwanted handlers were registered first, just before the call to the registration of the correct one.
reportViewer1.Drillthrough -= new DrillthroughEventHandler(UnwantedDrillthroughEventHandler);
Doing this got rid of the exception and things worked fine for me. Of course, be sure that this is not the ONLY reason for the above exception. This was a specific case and I hope it might prove useful for someone who is using Drillthrough and multiple DrillthroughEventHandler.
If you found this useful, please do leave a comment! And if you are logged in, please do rate the post as well.