Solved

Export to PDF fails using ASP.NET

Posted on 2004-04-01
7
3,179 Views
Last Modified: 2007-12-19
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
Comment
Question by:jawhitmoyer
7 Comments
 
LVL 100

Expert Comment

by:mlmcc
ID: 10734360
What error are you getting?

Post the code for the new report.

mlmcc
0
 
LVL 42

Expert Comment

by:frodoman
ID: 10734378
What is the error message?
0
 

Author Comment

by:jawhitmoyer
ID: 10734510
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:jawhitmoyer
ID: 10734542
Oh yeah, the report is bound to the Dataset4 I created off the table.
0
 

Author Comment

by:jawhitmoyer
ID: 10734577
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
 
LVL 100

Expert Comment

by:mlmcc
ID: 10737690
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
 
LVL 4

Accepted Solution

by:
kssaran earned 500 total points
ID: 10738452
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Crystal Reports: 5 Tests for Top Performance It is complete, your masterpiece report.  Not only does it meet your customer’s expectations, it blows them out the water, all they want is beautifully summarised and displayed in a myriad of ways. …
There have always been a lot of questions related to when Crystal Reports evaluates report components (such as formulas, summaries, cross-tabs, charts, to name a few examples). Crystal Reports uses a two-pass reporting process to provide greater …
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now