Thursday 17 January 2008

Crystal Reports - PUSH Method Example

Whether you want to or not as a professional developer at some point you are going to have to bang out some Crystal Reports, after a few false-starts I managed to get my head around the basics. Here's an example of producing a Crystal Report using the PUSH method.

ASPX PAGE:

Add the following just below the Page Directive declarations:

<%@ Register Assembly="CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

Add a CrystalReportViewer control to the form:

<CR:CrystalReportViewer ID="crv" runat="server" AutoDataBind="true" />


CODE BEHIND:

Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

' SET UP THE DOCUMENT OBJECT
Dim doc As CrystalDecisions.CrystalReports.Engine.ReportDocument
doc = New CrystalDecisions.CrystalReports.Engine.ReportDocument
doc.Load(Server.MapPath("MyReport.rpt"))
doc.SetDatabaseLogon("username", "password")

' RETRIEVE DATA USING TABLE ADAPTERS FROM THE STRONGLY TYPED DATASET 'YOU HAVE DESIGNED IN THE DATASET DESIGNER
Dim myTable as DataTable = myDataAdapter.GetRecords

'ADD THE DATATABLE TO THE DOCUMENT OBJECTS
'DATABASE.TABLES COLLECTION AND SET THIS
'AS THE DATASOURCE
doc.Database.Tables("myTable ").SetDataSource(myTable )
Me.crv.ReportSource = doc
'BIND THE DATA TO THE REPORTVIEWER
crv.DataBind()

End Sub

NB . When deploying anything with Crystal Reports in it you need to make sure that
the Crystal Reports Engine is installed on the server. If it isn't you will need this file:

CRRedist2005_x86.msi aka The Crystal Reports BootStrapper.

It might sound obvious but you also need to make sure that there is a PDF reader installed on the server as well.




No comments: