Wednesday, April 7, 2010

How to Run Crystal Report with Visual Basic 2008

Add CrystalDecisions references to your project and drag the ReportViewer control onto your form. 


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


'----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 Employee"
    Dim DA As New OleDbDataAdapter(strSQL, Connection)
    Dim DS As New DataSet


    DA.Fill(DS, "Employee")
    '----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 find crystal 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("Employee"))
    '-----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


    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

1 comments:

Buy Contact Lenses said...

Hey keep posting such smart and important articles.

Post a Comment

 
Powered by Blogger