Different methods of loading Crystal Report in Crystal Report Viewer in VB.NET

This article explains different methods of loading Crystal Report in the Windows Forms from VB.NET.

1. By Report Name - This method of loading a report simply specifies the  location of the report and sets it as the reportsource for the viewer.

Dim filePath As String
filePath = Application.StartupPath + "\ReportName.rpt"
CrystalReportViewer1.ReportSource = filePath

2. By Report Object - This method of loading a report creates a new instance of the ReportDocument object from the CrystalDecisions.CrystalReports.Engine namespace and loads a report into the object by specifying the physical path to the report.

Dim filePath As String
filePath = Application.StartupPath + "\ReportName.rpt"
'Create a new instance of the reportdocument object and load the report
Dim objReportDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
objReportDocument.Load(filePath)
'Bind the report to the viewer
CrystalReportViewer1.ReportSource = objReportDocument

3. By Untyped Report Component - This method of loading a report uses the ReportDocument component from the Components tab of the ToolBox.  When this component is added to the form, a dialogue box appears and prompts the Developer as to which type of Component to add to the form.  The options selected were 'Untyped ReportDocument' and a component called ReportDocument1 was added to the form.  This component acts as a proxy to the ReportDocument class.

Dim filePath As String
filePath = Application.StartupPath + "\ReportName.rpt"
Dim reportdocument1 As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
'load the report into the report document component and set the component as the reportsource for the viewer
reportdocument1.Load(filePath)
CrystalReportViewer1.ReportSource = reportdocument1

4. By Report Object - This scenario uses a report object that has been added to the current Project.  When a report is added to a project, a class file is generated for the report making the report a strongly-typed object, in this case 'ReportName.rpt'

'Bind a new instance of the strongly-typed report object to the viewer
CrystalReportViewer1.ReportSource = New ReportName()

5. By Strongly-Typed Report Component - This method of loading a report uses the ReportDocument component from the Components tab of the ToolBox.  When this component is added to the form, a dialogue box appears and prompts the Developer as to which type of Component to add to the form.  The 'Name' option selected was the strongly-typed report that was added to the project (SolutionName.ReportName).  The option to generate a Cached Strongly-Typed Report was deselected and a report component called 'ReportName1' was created and added to the form.

' Friend WithEvents ReportName1 As SolutionName.ReportName1 -This piece of code will be there under 'Windows forms designer generated code section.
'Bind the strongly typed report component to the viewer
CrystalReportViewer1.ReportSource = ReportName1


Similar Articles