Jan 31, 2010

ASP.NET MVC and Crystal Reports

Nearly every application nowadays needs to visualize some data presented in convenient way, with good look-and-feel, export capabilities and printing functionality.

Reports can be developed under various set of technologies and 3rd party libraries. Today we’ll take a look how to incorporate Crystal Reports with an ASP.NET MVC application.

First of all, I’d like to mention that a MVC application doesn’t support the well known Crystal Reports controls that we use in one ASP.NET web form application like – CrystalReportViewer, CrystalReportSource, Report Document, etc. That means, you cannot open a MVC view and drag these controls on it.

Fortunately we can easily find a workaround and bypass this small “limitation”.
I will review two possible solutions for integrating crystal reports as part of MVC application and will outline some pros and cons while reviewing the approaches.


A)Starting from standalone web application that incorporates crystal reports and moving it into an ASP.MVC application without creating sub application.

Creating ASP.NET forms application that incorporates crystal reports is a kid stuff and it is widely discussed over the net, so I will skip the explanations related to this small step.

The screenshot below shows a sample report that displays the access rights of our users:


Once we have the application, we may plug it into our MVC application that I have created in the one of my previous posts named MVC Custom authorization. Following this approach i can use different technologies and assemblies in my crystal report application like MS Ajax and the only one I need to do is to refer their assemblies in MVC references. And in the same time I have my Crystal Report application separately.
Technically there is only one application – the MVC one, we just take what we need from the report application and add it in MVC solution.

Next screen-shot shows the files we need in our MVC project.


Since there is only one application, in the context of the MVC application we can get rid of the web.config file. It is not really needed. To make the application compile able, exclude the folders displayed on the screen-shot.

It is very important to change the Build Action of the report file from Embedded to Content. The file will not compiled, but will be included in the Content output group.



Otherwise after deploy you will end up with no rpt file to be loaded and you will see the yellow screen of death:



And if doing so is inevitable to change the build action of the *.rpt file from embedded resource to content, when it is part of your MVC project (section B) it can easily keep his default build behavior. And I warmly advise you not to keep it and always to change it to “Content”. Otherwise you will end up doing new deploy for the sake of small change in one of your *.rpt files. And imagine what mess it could be if you have a product line in many production environments….

After compiling and running we can see that our report is displayed only when user is authenticated.





You can run your crystal project out of the context of the MVC project. Imagine that your build is broken for the next few hours? What are you going to do, waiting rather opening the solution of your report project and keep on coding?

At last, what will happen if I want to separate for some reasons (let’s say make it available to dedicated group of users) my reports as new application. In this case I will just exclude the files from my solution and touch a bit the code to adopt it for my new needs. Not a biggy…indeed it is already separate one.

B)Integrating crystal reports in MVC application via ASP.NET ASPX pages

The first thing we need to do is to add catch-all ignore route for the aspx pages, just to make sure that they won’t be handled by the MVC engine.



We can add a new folder (let’s say Reports) and define our aspx pages which host crystal reports inside. There is no really difference in doing this with creating aspx page in standalone crystal report application.



In this scenario we are not facing the problems of excluding/including new files from our inner application, but definitely our reporting part is less scalable.

You can download the Crystal Reports 2008 runtime package for .net Framework from the links, and then install it on the target machine. Don’t forget to read very carefully the license agreement.

"CRRedist2008_x86.msi" (for 32bit)
"CRRedist2008_x64.msi" (for 64bit)

http://resources.businessobjects.com/support/downloads/redistributables/vs_2008/redist/x64/CRRedist2008_x64.msi

http://resources.businessobjects.com/support/downloads/redistributables/vs_2008/redist/x86/CRRedist2008_x86.msi

17 comments:

  1. Hi, I'm trying to do your Solution A. How do i plug in the crystal reports standalone web application into the mvc project?

    I tried to copy and paste directly into the MVC project's folder in visual studio folder, but when I open the project in visual studio, it wont show the crystal report folder.

    Please help explaining more detailed. Thank you !

    ReplyDelete
  2. Hi Friend,
    I would like to contact for to get help about MVC and your great article, how can contact?. please contact me

    ReplyDelete
  3. sorry for the late response, i am a bit lazy about blogging lately so i haven't check this in a while...you can contact me on hurbata@gmail.com.

    ReplyDelete
  4. I am trying to do your solution B.
    I have installed the CR and in the Views, I have added a Report Folder and when I add view, I choose Reporting/CR, and it creates a .rpt file and a .cs file. It does not create a .aspx file.
    How do I define a aspx page to host crystal reports inside. Even better... can I create a .ascx file with the report and use RenderPartial in a .aspx file.

    ReplyDelete
  5. you should add a asp.net WebForm page, rather than a view. Afterwards you should be able to host CR controls the way you use them in ASP.NET forms application (just google it, tons of examples). Something like that:
    <CR:CrystalReportViewer id="crViewerAccessRights" runat="server" AutoDataBind="true" DisplayGroupTree="false" HasRefreshButton="true" ReportSourceID = "crsAccessRights" />

    <CR:CrystalReportSource ID="crsAccessRights" runat="server">
    <Report FileName="Reports\AccessRightsReport.rpt" >
    </Report>
    </CR:CrystalReportSource>
    on the code behind:
    1) instantiate AccessRightsReport report = new AccessRightsReport();
    2) fill your DataSet
    3) bind the content to the report
    report.SetDataSource(ds.Tables["AccessRightsReport"]);

    crViewerAccessRights.ReportSource = report;

    It should be something like that. Bear in mind that this code is not copy pasted but manually typed, so i might mistype it somewhere

    ReplyDelete
  6. Looks interesting will check now. Please try to explain with more details. Nice post.

    ReplyDelete
  7. can you please upload source code for this. what about the controller

    ReplyDelete
  8. Thanks for contribution such helpful information. The information provided is very nice and this information is not available so easily. Therefore I thank the writer for the useful input.
    web services

    ReplyDelete
  9. Thіs is a topic ωhіch is near to my
    heaгt... Βеst wisheѕ! Exactly where
    аrе your contact dеtаіls though?
    Also see my web page :: Tiger’s Eye Crystal

    ReplyDelete
  10. For those that click on all the links to get to here.

    Go here for actual code example.
    http://code.msdn.microsoft.com/Using-Crystal-Report-in-bb0e6229

    ReplyDelete
  11. I followed Set B, but after aspx page loads nothing is displayed, why?

    ReplyDelete
  12. Chinu Dey - because you have to set up the reportdatasource in code, otherwise its just a blank report. Here's a great video that i followed

    http://www.youtube.com/watch?v=63TJAlLoIOg

    ReplyDelete
  13. How can I do that in Visual Studio 2012, I install de SAP Crystal Reports from here: http://scn.sap.com/docs/DOC-35074, but in visual studio doesn't appear the option to add the crystal report in Add - New Item...

    ReplyDelete
  14. hi can u post the same with detailed explanation

    ReplyDelete
  15. hi can please post the same with rendering in the same page or view

    ReplyDelete
  16. hi can please post the same with rendering in the same page or view

    ReplyDelete