Search code examples
.netvb.netcrystal-reports

How to programatically export crystal report as PDF in VB.NET


Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports CrystalDecisions.Web
Imports CrystalDecisions.ReportSource
Imports CrystalDecisions.CrystalReports

Imports System.IO
Imports System.Net
Imports System.Net.Mail

Public Class Form1

Dim cryRpt As New ReportDocument

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    cryRpt.Load("C:\Documents and Settings\Prs1\My Documents\Visual Studio 2008\Projects\myCR\myCR\cr.rpt")
    CrystalReportViewer1.ReportSource = cryRpt

    CrystalReportViewer1.Refresh()

    Try
        Dim CrExportOptions As ExportOptions
        Dim CrDiskFileDestinationOptions As New  _
        DiskFileDestinationOptions()
        Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
        CrDiskFileDestinationOptions.DiskFileName = _
                                    "C:\crystalExport.pdf"
        CrExportOptions = cryRpt.ExportOptions
        With CrExportOptions
            .ExportDestinationType = ExportDestinationType.DiskFile
            .ExportFormatType = ExportFormatType.PortableDocFormat
            .DestinationOptions = CrDiskFileDestinationOptions
            .FormatOptions = CrFormatTypeOptions
        End With
        cryRpt.Export()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

End Sub

End Class

I tried the above code. I have one Windows Form and a single Crystal report file (cr.rpt) in my project folder. I'm using Oracle 10G as the database. But I'm getting an error saying that "Logon Failed". ([Error Description]Image: http://pho.to/Zv6t)

Pls do help.


Solution

  • Finally found the solution from here

    The only thing is, I need to set Database credentials at runtime for the Crystal Report to work fine.

    cryRpt.SetDatabaseLogon("user", "password", "server", "database")