Blogs

Trevor Dubinsky

Crystal Reports and PONOs, POCOs and Classes(OH MY!)
Trevor Dubinsky SAP Employee Active Contributor Bronze: 250-499 points
Business Card
Company: SAP
Posted on Sep. 03, 2010 02:21 PM in Business Intelligence (BusinessObjects), SAP Crystal Reports

Subscribe.Subscribe
Print. Print
Permalink Permalink
Share

A question that keeps popping up from our .NET developer community is "How do I report off of my .NET classes or objects?" with Crystal Reports. Now recently it has changed to "How do I report off of PONO/POCO objects?". 

 

In the support team we have not had the opportunity to explore this fully, so would give the less-then-satisfactory answer "Build the report off of an XSD file and put your data in a .NET DataTable object" because we knew that would work.

 

Since POCO/PONO has been a buzz word flying around our forums lately I took some time to investigate this and see how or if we work with non-dataset objects inside of Visual Studio .NET. Spoiler: We DO!

 

Before we go further I should explain my definition of what a POCO or PONO is. The acronym stands for Plain Old C#/CLR/NET Objects.  I found there are varying definitions of these objects and I am settling on this: POCOs are classes that contain no functionality required of a Common Language Runtime (CLR) or Framework. For example the code for the definition of a POCO in VB .NET should allow you to copy and paste it into VB 6 and it would still be valid. It should be easily represented in another language like Java as a POJO. As I said this is simplified and if you have any comments concerning this please provide feedback. For the rest of this blog I will just refer to this type of data as their basic type, class. 

 

Crystal Reports is a reporting tool that expect to be reporting off of a table of indeterminate number of rows but with fixed amount of columns. So to report off of a class, the class should represent a row of data. What this means is that any properties that are not simple data types, such as numbers, dates and strings will not be available to Crystal. This means you couldn't report off of sub-classes. 

 

Let's take the following simple classes:

class owner
{
public string Name;
public int Age;
class car
{
public string Make;
public string Model;
public int Year;
Owner TheOwner;

These are simple classes and Crystal can report off of both of them. The class Car would work but the property "TheOwner" would not be able to be read by Crystal.

 

How would you create a report off of this in the first place? The answer depends if you are in in the standalone Crystal designer or the VS .NET designer:

  • Crystal Designer: You would need to have a table like and Access, SQL Server or XSD file that matches the structure of your class. The report can be built on this then at runtime pass the active data to the report.
  • VS .NET Designer: You can follow the Crystal Designer above or even easier when you are in the Database Expert expand Project Data->.NET Objects and you will get a list of your available classes. Select your class and added it to Selected Tables box. You will then be able to add the properties/members of the class to your report just like they are database fields.

Once you have your report designed then the next step is passing this to the ReportDocument and ReportClientDoc objects. This is the step where most people got stuck. The methods to use would be ReportDocument.SetDataSource for Crystal Reports or ReportClientDoc.DatabaseController.SetDataSource for RAS. As these methods do not take a class object directly we need to make use of an intermediary Collection object. Just create a collection object to handle the same type as your class, add your class variable to it and then call SetDataSource to this collection. The collection has to have at least one item in it to work. Here is a sample based on the Car class above:

//Create three "car" objects from form input by user

car Car1 = new car ();
Car1.Make = "Ford";
Car1.Model = "Windstar";
Car1.Year = 2000;

car Car2 = new car();
Car2.Make = "Ford";
Car2.Model = "Pinto";
Car2.Year = 1976;

car Car3 = new car();
Car3.Make = "Dodge";
Car3.Model = "Colt";
Car3.Year = 1996;

// To make available to Crystal Reports place these in a Collection containerCollection<car> cars = new Collection<car>();
cars.Add(Car1);
cars.Add(Car2);
cars.Add(Car3);

//Pas the data to the report and view it

CarReport1 myCarReport = new CarReport1();
myCarReport.SetDataSource(cars);
crystalReportViewer1.ReportSource = myCarReport;

 


 

 

 

Trevor Dubinsky  Active Contributor Bronze: 250-499 points I'm a Senior Engineer with SAP Business Objects.


Comment on this articleI will be expanding this by creating some sample applications for C#, VB and Delphi Prism. In the mean time let me know what you think or if this was helpful.
Comment on this weblog
Showing messages 1 through 5 of 5.

Titles Only Main Topics Oldest First

  • Silverlight
    2010-09-09 11:57:07 Doyle Johnson Business Card [Reply]

    Since 1. Silverlight must receive its data from a WCF layer. 2. MS says that datasets absolutely should not be sent through WCF. 3. Reporting services supports only datasets (no collection binding). THEN CR is an obvious choice for silverlight reporting given its POCO support. A great project would be to show us how to incorporate CR reporting into a silverlight application !
    • Silverlight
      2010-09-16 15:30:27 Doyle Johnson Business Card [Reply]

      I happened to read in Lerman's new Entity Framwork Book that object support for SSRS is pending at Microsoft for the next release.
    • Silverlight
      2010-09-16 09:32:34 Trevor Dubinsky SAP Employee Business Card [Reply]

      Thank you for the feed back! At this point I think our official support for Silverlight is none, however that doesn't mean it will not work.


      Here is a blog concerning Crystal 2008 and Silverlight: http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/20795


      And I will investigate Silverlight with our Crystal for VS 2010 WCF control to see what can be done and will blog my results!

      • Silverlight
        2010-10-22 14:22:23 Trevor Dubinsky SAP Employee Business Card [Reply]

        Hi,


        Some good news, go to https://ideas.sap.com/, this is our idea market place which allows our customers to enter enhancement requests for our products, and up-vote the ones they like, and this is looked at by our developers to incorporate into our products.


        Trevor

      • Silverlight
        2010-10-19 15:59:22 Trevor Dubinsky SAP Employee Business Card [Reply]

        My testing has not been able to get our WPF control to work inside of Silverlight... however I will speak to the powers that be to let them know there is an interest in this!

Showing messages 1 through 5 of 5.