Search Amazon:

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.

Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Be the first to add a comment! Items to Show:     

     
You must log in to post feedback.
Comment:    
 

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

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms