Generate a Crystal Reports report without a database






4.68/5 (14 votes)
Nov 21, 2006
2 min read

160308

4476
How to create a Crystal Reports report and fill it without a database.
Introduction
Sometimes we want to do tricks with the Crystal Reports engine, like profiting from the Exporting functions to create Word, Excel, or PDF files. What if we don't read the data from a database? This article shows the details on creating a Crystal Reports sub report, and how to fill it from code, without a database.
Using the code
Create the Project:
- File -> New -> New project
Create a New Crystal Reports Visual Basic Windows Application.
Create the DataSet schema:
-
Right click the Crystal Reports project -> Add -> New Folder
Name it DataSets.
-
Right click the DataSets folder -> Add -> New Item
Create a new
CRDataSet
DataSet. -
Right click the
CRDataSet
Designer Page -> Add -> New ElementName it
MainTable
. -
Add new
Field1
,Field2
,Field3
rows. -
Repeat steps 3 and 4, add a new
DetailTable
element.
You will have the following DataSet
tables schema:
Create the Crystal Reports report:
- Right click the Crystal Reports project -> Add -> New Folder
Name it Reports.
-
Right click the
Reports
folder -> Add -> New ItemOpen a new
MainReport
Crystal Reports report. -
Click the Register Later button to close the annoying register window, if it appears.
-
Select Subreport and click OK. In the Contain Subeport Expert window Data tab, expand Project Data -> ADO.NET DataSet -> CrystalReport.CRDataSet.
-
Select
MainTable
. Click the Insert Table button. -
Click Next.
-
Click the Add All button. Click Next. Click Next. Click Next.
-
Next to the Create a Subreport choice of the Subreport window tab, type DetailReport in the Report Name edit box.
-
Click the Report Expert button. Insert
DetailTable
. Click Next to open the Subreport Generator.-
Add All fields. Click Next. Click Next. Click Next. Click Next.
-
Type "No DB Subreport Crystal Report Sample" as the Subreport Title.
-
Click Finish.
-
- Click Next. Type "No DB Crystal Report Sample." as the Main Report title. Click Finish.
Add the a CrystalReportViewer1
to the form.
-
Open the
Form1
form. Click View -> Toolbox in the menu. -
Drag (from the bottom of the Windows Forms tab in the Toolbox) a
CrystalReportViewer
into the form. -
Right click the
CrystalReportViewer1
CrystalReportViewer in the form -> Properties. - Set the
Dock
property toFill
.
Add the code to fill and show the Crystal Reports report.
Right click the Form -> View Code. Add the following Form1_Activated
method:
Private Sub Form1_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Activated
Dim dsObj As CRDataSet = New CRDataSet
FillDataSet(dsObj)
Dim cr As MainReport = New MainReport
' Set the report DataSet
cr.SetDataSource(dsObj)
CrystalReportViewer1.ReportSource = cr
End Sub
Add the following FillDataSet
method:
Public Sub FillDataSet(ByRef dataSet As CRDataSet)
' Turn off constraint checking before the dataset is filled.
' This allows the adapters to fill the dataset without concern
' for dependencies between the tables.
dataSet.EnforceConstraints = False
Try
' Attempt to fill the dataset MainTable
dataSet.MainTable.AddMainTableRow("Hello!", _
"This is", "my sample data")
' Attempt to fill the dataset DetailTable
dataSet.DetailTable.AddDetailTableRow("This is", _
"the detail", "information")
dataSet.DetailTable.AddDetailTableRow("added", _
"without the", "need of")
dataSet.DetailTable.AddDetailTableRow("actually", _
"access a", "database.")
Catch ex As Exception
' Add your error handling code here.
Throw ex
Finally
' Turn constraint checking back on.
dataSet.EnforceConstraints = True
End Try
End Sub
Build and run the application.
That's it!
History
- 21 Nov 2006 - Posted.