Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3300
  • Last Modified:

Export to PDF fails using ASP.NET

I made a report based on an SP and it works great (VS 2003). Using code, I run the query and then export it to a PDF. I then made a second report, copying the code from the working page, but now I get an error on the export.

I am pulling my hair out (and I don't have that much to spare. I don't know where to begin, or what code to post.

Please help me!
0
jawhitmoyer
Asked:
jawhitmoyer
1 Solution
 
mlmccCommented:
What error are you getting?

Post the code for the new report.

mlmcc
0
 
frodomanCommented:
What is the error message?
0
 
jawhitmoyerAuthor Commented:
Here's the code:

    Sub BindReport()

        Dim myConnection As New SqlClient.SqlConnection

        myConnection.ConnectionString = "server=sql01;" _
                                            & "Initial Catalog=forensicsSQL;" _
                                            & "User Id=sa;" _
                                            & "Password=token!;"

        Dim MyCommand As New SqlClient.SqlCommand

        MyCommand.Connection = myConnection
        MyCommand.CommandType = CommandType.Text
        MyCommand.CommandText = "select * from user_photoReportTemp"

        Dim MyDA As New SqlClient.SqlDataAdapter

        MyDA.SelectCommand = MyCommand

        Dim myDS As New Dataset4

        MyDA.Fill(myDS, "user_rptCaseDocList")

        Dim oRpt As New PhotoReport2

        oRpt.SetDataSource(myDS)

        Dim DiskOpts As CrystalDecisions.Shared.DiskFileDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions

        oRpt.ExportOptions.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile

        oRpt.ExportOptions.ExportFormatType = CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat

        DiskOpts.DiskFileName = "C:\Inetpub\wwwroot\SQLaspx\PhotoReport.pdf"

        oRpt.ExportOptions.DestinationOptions = DiskOpts

        oRpt.Export()

        myDS = Nothing
        myConnection.Close()
        Response.Redirect("./PhotoReport.pdf")
    End Sub

The error:

Server Error in '/SQLASPX' Application.
--------------------------------------------------------------------------------

Error in File C:\DOCUME~1\JAWHIT~1\ASPNET\LOCALS~1\Temp\temp_d34488ef-ebdf-417d-9ce7-07ab9608c65f.rpt: Error detected by export DLL:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: CrystalDecisions.CrystalReports.Engine.ExportException: Error in File C:\DOCUME~1\JAWHIT~1\ASPNET\LOCALS~1\Temp\temp_d34488ef-ebdf-417d-9ce7-07ab9608c65f.rpt: Error detected by export DLL:

Source Error:


Line 173:        oRpt.ExportOptions.DestinationOptions = DiskOpts
Line 174:
Line 175:        oRpt.Export()
Line 176:
Line 177:        myDS = Nothing
 

Source File: C:\Inetpub\wwwroot\SQLaspx\photoReport.aspx.vb    Line: 175
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
jawhitmoyerAuthor Commented:
Oh yeah, the report is bound to the Dataset4 I created off the table.
0
 
jawhitmoyerAuthor Commented:
I am continuing to test, so please consider these points.

There is am image in the table, which I need to display. I just tried deleting the image field, and it did the export, but there was not other data...just the report form.
0
 
mlmccCommented:
As a guess, i don't have CR.Net so I can't verify.  Does CR.Net support loading pictures from a datasource?  I think that is one of the features not in the .Net versions.

Not sure about the ASP but I have seen other code where instead of a new they used the one for the report
Try changing
Dim DiskOpts As CrystalDecisions.Shared.DiskFileDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions

to
Dim DiskOpts As CrystalDecisions.Shared.DiskFileDestinationOptions
DiskOpts = oRpt.ExportOptions.DestinationOptions

mlmcc
0
 
kssaranCommented:
You can Try Exporting to PDF File as mentioned Bellow


   'Variable initialised at the class level
    Dim strInvoiceNo As String
    Dim strSelectFormula As String
    Dim ServerName As String, UserId As String, Password As String

    Private Sub cmdShowReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShowReport.Click
        ExportReport("servername", "username", "password")
    End Sub



    Private Sub ExportReport(ByVal ServerName As String, ByVal UserId As String, ByVal Password As String)
    'To Export the Result into a PDF File and display the Same in the window
        Dim myReport As New SampleReport()
        strInvoiceNo = Request.Form("txtInvoiceNo")
        strSelectFormula = "{invoice_header.inh_invoiceno} = '" + strInvoiceNo + "'"
        myReport.RecordSelectionFormula = strSelectFormula
        'Set the logon credentials of the main report
        LogonToDatabase(myReport.Database.Tables, ServerName, UserId, Password)

        'Modify the sub-report properties

        Dim mySection As CrystalDecisions.CrystalReports.Engine.Section
        Dim myReportObject As CrystalDecisions.CrystalReports.Engine.ReportObject
        Dim mySubReport As CrystalDecisions.CrystalReports.Engine.SubreportObject
        Dim mySubReportDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument

        'Loop through each section of the main report

        For Each mySection In myReport.ReportDefinition.Sections
            'Drill down into the section to see if there are any sub-reports
            For Each myReportObject In mySection.ReportObjects
                If myReportObject.Kind = _
                    CrystalDecisions.[Shared].ReportObjectKind.SubreportObject Then
                    mySubReport = CType(myReportObject, _
                        CrystalDecisions.CrystalReports.Engine.SubreportObject)

                    'Get a reference to the sub-report object
                    'so we can change its properties
                    mySubReportDoc = mySubReport.OpenSubreport(mySubReport.SubreportName)
                    'Set the logon credentials
                    LogonToDatabase(mySubReportDoc.Database.Tables, ServerName, UserId, Password)
                End If
            Next
        Next
        Dim myExportExt As String, myExportType As String, myExportFile As String
        Dim strExportType As String
        strExportType = "PDF"

        If strExportType = UCase("Excel") Then
            myExportExt = ".xls"
            myExportType = "application/vnd.ms-excel"
        ElseIf strExportType = UCase("HTML") Then
            myExportExt = ".html"
            myExportType = "application/html"
        ElseIf strExportType = UCase("PDF") Then
            myExportExt = ".pdf"
            myExportType = "application/pdf"
        ElseIf strExportType = UCase("RichText") Then
            myExportExt = ".rtf"
            myExportType = "application/rtf"
        ElseIf strExportType = UCase("Word") Then
            myExportExt = ".doc"
            myExportType = "application/msword"
        End If

        Dim myExportOptions As CrystalDecisions.Shared.ExportOptions
        Dim myDiskFileDestinationOptions As CrystalDecisions.Shared.DiskFileDestinationOptions

        'myExportFile = Server.MapPath(".") & "\" & Session.SessionID.ToString & myExportExt
        myExportFile = "c:\winnt\temp\" & Session.SessionID.ToString & myExportExt

        myDiskFileDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions()
        myDiskFileDestinationOptions.DiskFileName = myExportFile
        myExportOptions = myReport.ExportOptions

        With myExportOptions

            .DestinationOptions = myDiskFileDestinationOptions
            .ExportDestinationType = .ExportDestinationType.DiskFile

            If strExportType = UCase("Excel") Then
                .ExportFormatType = .ExportFormatType.Excel
            ElseIf strExportType = UCase("HTML") Then
                .ExportFormatType = .ExportFormatType.HTML40
            ElseIf strExportType = UCase("PDF") Then
                .ExportFormatType = .ExportFormatType.PortableDocFormat
            ElseIf strExportType = UCase("RichText") Then
                .ExportFormatType = .ExportFormatType.RichText
            ElseIf strExportType = UCase("Word") Then
                .ExportFormatType = .ExportFormatType.WordForWindows
            End If
        End With

        'Exports the report to PDF format
        myReport.Export()
        Response.ClearContent()
        Response.ClearHeaders()
        Response.ContentType = myExportType
        Response.WriteFile(myExportFile)
        Response.Flush()
        Response.Close()

        System.IO.File.Delete(myExportFile)
    End Sub

    Private Sub LogonToDatabase(ByVal ReportTables As CrystalDecisions.CrystalReports.Engine.Tables, ByVal ServerName As String, ByVal UserId As String, ByVal Password As String)
    ' To Supply Logon Information to each and every Tables used in the Report
        Dim myTable As CrystalDecisions.CrystalReports.Engine.Table
        Dim myConnectionInfo As New CrystalDecisions.Shared.ConnectionInfo()
        Dim myLogonInfo As New CrystalDecisions.Shared.TableLogOnInfo()
        myConnectionInfo.UserID = UserId
        myConnectionInfo.Password = Password
        myConnectionInfo.ServerName = ServerName
        myLogonInfo.ConnectionInfo = myConnectionInfo
        For Each myTable In ReportTables
            myTable.ApplyLogOnInfo(myLogonInfo)
        Next
    End Sub
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now