Run a Crystal Report from .NET using the Crystal Report Viewer

After creating reports using the lite version of Crystal Reports that comes with .NET or the full version you can display them using the CrystalReportViewer control.

Add CrystalDecisions references to your project and drag the ReportViewer control onto your form. The references may vary but typically include:

    CrystalDecisions.CrystalReports.Engine
    CrystalDecisions.Shared
    CrystalDecisions.Windows.Forms
    CrystalDecisions.ReportSource

Loading a report when the report's DataSource is an XML Schema File

This VB.NET snippet assumes your report's DataSource is an XML Schema (XSD) file. For details on using an XML schema as the datasource click here.

Create a DataTable containing the data for the report:

    Imports System.Data.OleDb

    Dim strConnection As String = "..."
    Dim Connection As New OleDbConnection(strConnection)
    Dim strSQL As String = "Select * From Customers"
    Dim DA As New OleDbDataAdapter(strSQL, Connection)
    Dim DS As New DataSet
    '
    ' Create a datatable in your dataset. The datatable's name 
    ' must match that in the schema file used by the report.
    '
    DA.Fill(DS, "Customers")

Verify the path to the Crystal Report's .RPT file:

    Dim strReportPath As String = Application.StartupPath & _
           "\" & strReportName & ".rpt"
    
    If Not IO.File.Exists(strReportPath) Then
        Throw (New Exception("Unable to locate report file:" & _
          vbCrLf & strReportPath))
    End If

Load the Crystal report's .RPT file and pass in the DataTable:

    Dim cr As New ReportDocument

    cr.Load(strReportPath)
    cr.SetDataSource(DS.Tables("Customers"))

Set the CrystalReportViewer's appearance and set the ReportSource:

    CrystalReportViewer.ShowRefreshButton = False
    CrystalReportViewer.ShowCloseButton = False
    CrystalReportViewer.ShowGroupTreeButton = False

    CrystalReportViewer.ReportSource = cr

Loading a report when the report's DataSource is a .NET DLL

This VB.NET code assumes your report's DataSource is a .NET DLL which returns a DataTable. For details on using a DLL as the datasource click here.

Verify the path to the Crystal Report's .RPT file:

    Dim strReportPath As String = Application.StartupPath & "\" & _
        strReportName & ".rpt"
    
    If Not IO.File.Exists(strReportPath) Then
        Throw (New Exception("Unable to locate report file:" & _
          vbCrLf & strReportPath))
    End If

Load the Crystal report's .RPT file:

    Dim cr As New ReportDocument
    cr.Load(strReportPath)

Set the CrystalReportViewer's appearance and set the ReportSource:

    CrystalReportViewer.ShowRefreshButton = False
    CrystalReportViewer.ShowCloseButton = False
    CrystalReportViewer.ShowGroupTreeButton = False

    CrystalReportViewer.ReportSource = cr

To learn how to pass parameter values into your Crystal Report, click here.




About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page