Opening a Crystal Report as a PDF
I'm trying to open a Crystal Report as a PDF in ASP.NET. Some times when the page loads it works, other times I have to hit the refresh button to make the report open. It seems pretty sporadic. Anybody have any idea what might cause this? Here is the Page_Load code. Thanks.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
'Put user code to initialize the page here
' Define Crystal Reports variables
Dim crReportDocument As ReportDocument
Dim crExportOptions As ExportOptions
Dim crDiskFileDestinationOptions As DiskFileDestinationOptions
Dim Fname As String ' Temp PDF File Name
Dim SQLConn As SqlClient.SqlConnection ' SQL Connection
Dim SQLDA As SqlClient.SqlDataAdapter ' SQL Data Adapter
Dim dsDataSet As New DataSet ' Dataset
Dim strSQL As String ' SQL Statement
' Instantiate the SQL Connection
SQLConn = New SqlClient.SqlConnection("Persist Security Info=False;server=SomeServer;database=SomeDB;User ID=sa;Password=password")
' SQL SELECT Statement
strSQL = "SELECT * FROM Tasks ORDER BY DueDate"
' Instantiate the SQL DataAdapter
SQLDA = New SqlClient.SqlDataAdapter(strSQL, SQLConn)
' Populate the DataSet
SQLDA.Fill(dsDataSet, "Tasks")
' Instantiate the Crystal Report Document (crTasks.rpt)
crReportDocument = New crTasks
Try
' Set the DataSource for the Report as the DataSet
crReportDocument.SetDataSource(dsDataSet)
Catch ex As Exception
End Try
' Set the name of the temporary file to create for the PDF
Fname = "c:\temp\" & Session.SessionID.ToString & ".pdf"
' Instatiate the Crystal Reports DiskFileDestinationOptions
crDiskFileDestinationOptions = New DiskFileDestinationOptions
crDiskFileDestinationOptions.DiskFileName = Fname
' Set reference to the Crystal Report Export Options
crExportOptions = crReportDocument.ExportOptions
' Specify those options
With crExportOptions
.DestinationOptions = crDiskFileDestinationOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.PortableDocFormat
End With
' Export the Crystal Reports as a PDF
crReportDocument.Export()
' The following code writes the pdf file to the Client’s browser.
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.WriteFile(Fname)
Response.Flush()
Response.Close()
' Delete the temporary exported file from disk
System.IO.File.Delete(Fname)
End If
End Sub