Wednesday, 16 February 2011

Visual Studio 2010 Report Viewer - Object Datasource

I have had no end of issues trying to get the ReportViewer control in Visual Studio to work with business objects in a Web Application (not website)

When I tried to create a new report the datasources window would never pick up my business objects. Here is a solution that I finally got working.

Create a new class library project to host reports. This library can contain the object datasources added by using the Data menu, then Add New Datasource. The reason I could not do this in the Web Application is because this menu item is missing for Web Applications!

Anyway, once you have added the datasources, add a new report to the reporting class library, here it will have access to all of the datasources (my data sources are actually business objects in another class library) This report needs to be set as an embedded resource.

Once you have the report and datasources ready its time to add the ReportViewer to an aspx page. I ran into a problem here when setting the ReportViewer.LocalReport.ReportEmbeddedResource. It looks like the report viewer looks for the resource in the current assembly. Luckily this can be resolved by adding a helper class to the reporting library to set the embedded resource from within the library.

Here is the code for the helper function in the reporting library


and here is how you would use it in the aspx page
Update for .net 4.0 I had an issue where the report would not load after being compiled in .net 4.0. The message read: 'The report definition for report 'xxx.rdlc' has not been specified' even though it was clearly in the assembly. Changing the way the Embeded resource was loaded seemed to fix the problem with the following simple change to the SetReportEmbeddedResource method in the reporting assembly.

9 comments:

  1. I have spent a few days with my head bashing against this problem.

    If this works I want to have your babies :)

    ReplyDelete
  2. ha ha, let me know how you get on! hope it works for you! ;)

    ReplyDelete
  3. Thanks a lot. I had the same problem

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. great post! thanks!

    ReplyDelete
  6. If you're having this issue because the project type is MVC, you can try adding an ASPX page anywhere to the solution to trick Visual Studio to pulling in the right design time references.

    See this Stack Overflow question http://stackoverflow.com/a/27696119/1366033

    ReplyDelete
  7. Hey, can you package up a.sln of this. I'm having problems. :

    Parser Error Message: The base class includes the field 'rptViewer', but its type (Microsoft.Reporting.WebForms.ReportViewer) is not compatible with the type of control (Microsoft.Reporting.WebForms.ReportViewer).

    ReplyDelete
  8. When I suffered with this error I tried the solution explained below and it also gives some explanation for reason of the error -

    Fix For This Error
    http://geeksprogrammings.blogspot.com/2015/05/parse-error-base-class-includes-field.html

    ReplyDelete