Tuesday, January 23, 2007

Crystal Reports - Reporting a custom dataset

I've just gone thru the exercise of trying to use Crystal Reports with a custom dataset generated by a complex Oracle database query. I was helped greatly by Chuck Bradley's article at http://aspalliance.com/776. I tried to enter comments but wasn't successful, so here they are:

Many thanks, Mr. Bradley! I've just gotten this to work properly. It took me a couple of days though, as I spent time wading through the Microsoft and Business Objects tutorials and countless web articles. It seems that this is the only way to bind arbitrary dataset data to a Crystal Report. The report designer definitely seems to need an XML schema in order to be able to lay out the report.

I have a data access class that fills a datasource from a complex Oracle SQL query. This class creates the db connection, creates a command object and loads it with the query string, creates an OracleDataAdapter, then creates a dataset and uses adapter.fill to load it. Pretty standard stuff. I'm not explicitly creating any tables.

In this class, after the dataset is filled, I have a line of code to create an XML *Schema* file from my dataset:

ds.WriteXmlSchema("c:\etc...").

Of course I got the UnauthorizedAccessException as mentioned in your article, but after granting write priv to my machines' aspnet account, that went away.

I stepped thru this code so that the XMLSchema file was written then quit and went back to the report design as shown, and the designer showed the data fields from my query!

After designing the report, I went to the code-behind page of the page where I dragged the Crystal Report Viewer, and in the Partial Class I inserted the statement below at the top of the file:

Imports CrystalDecisions.CrystalReprorts.Engine

I then then inserted a Page_Init Sub with the following code:

Dim rptdoc as New ReportDocument
'
'important or you get invalid file path error - thanks clartsonly
'
rptdoc.Load(Server.MapPath("my.rpt"))
rptdoc.SetDataSource(myDataAccessClass.myGetDataset)
crv1.ReportSource = rptdoc 'my crystal reports viewer control is crv1
crv1.DataBind()
crv1.DisplayToolbar = True

When I ran this page, the report displayed properly.

No comments: