Solved

ASP.NET Crystal Reports PDF Export

Posted on 2009-05-18
14
686 Views
Last Modified: 2013-11-26
Is it possible to export a crystal report from asp.net 2008 into a PDF, with parameters without using a crviewer?

The following code work fine if I remove the parameter from the report and the parameter reference from the code.  I am getting a missing parameter error when attempting to execute the code below.

The crystal report only has one parameter.

Thanks!

Dim rptDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument
rptDoc.Load(Server.MapPath("/") & "crReports/ImportException.rpt")
rptDoc.SetParameterValue(0, "Test")
rptDoc.SetDataSource(dt)
ExportData(rptDoc)
 
    Public Sub ExportData(ByRef oRpt As CrystalDecisions.CrystalReports.Engine.ReportDocument)
        Dim fs As IO.FileStream
        Dim FileSize As Long
        Dim oDest As New CrystalDecisions.Shared.DiskFileDestinationOptions
        Dim ExportFileName As String = Server.MapPath("/") & TempProcessFile & "/" & Session.SessionID & ".pdf"
        'Dim ExportFileName As String = "c:\data\" & Session.SessionID & ".pdf"
        Try
            oRpt.ExportOptions.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile
            oRpt.ExportOptions.ExportFormatType = CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat
            oDest.DiskFileName = ExportFileName
            oRpt.ExportOptions.DestinationOptions = oDest
            oRpt.Export()
 
            'Build Target Filename
            'Send the file to the user that made the request
            Response.Clear()
            Response.Buffer = True
            Response.AddHeader("Content-Type", "application/pdf")
            fs = New IO.FileStream(ExportFileName, IO.FileMode.Open)
            FileSize = fs.Length
            Dim bBuffer(CInt(FileSize)) As Byte
            fs.Read(bBuffer, 0, CInt(FileSize))
            fs.Close()
            Response.BinaryWrite(bBuffer)
            Response.Flush()
            Response.Close()
        Catch e As Exception
        End Try
    End Sub

Open in new window

0
Comment
Question by:dpinchot
  • 7
  • 5
  • 2
14 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24419415
YES. Is the above code not working?
0
 

Author Comment

by:dpinchot
ID: 24421428
No.. It fails at the oRpt.Export() line.  If I remove the rptDoc.SetParameterValue(0, "Test")
 line, and remove the parameter from the report, it runs just fine.

So, it seems to be in my parameter object declaration, or in how I create the parameter in my Crystal Report.  I am moving from VB6 to .net, and I have an application that contains 50 or so Crystal reports.  They are all unbound using active data, so I assign the DataTable at run-time.  I also am a heavy user of parameters and formulas to populate the page header content etc.  

I have added a more complete code sample below and attached a sample crystal report.  The Crystal report is unbound using a ttx (active data) file.  Just rename the CR with a rpt ext,.
im dt As New DataTable
 
dt.Columns.Add("RecNo", Type.GetType("System.String"))
dt.Columns.Add("ID", Type.GetType("System.String"))
dt.Columns.Add("Name", Type.GetType("System.String"))
dt.Columns.Add("Exception", Type.GetType("System.String"))
dt.Columns.Add("Imported", Type.GetType("System.String"))
dt.Columns.Add("Database", Type.GetType("System.String"))
dt.Columns.Add("ImportFile", Type.GetType("System.String"))
 
 
dt.Rows.Add(New String() {"1", "ID 1", "Name", "Exception 1", "Y", "wpTest", "c:\data\test.dat"})
dt.Rows.Add(New String() {"2", "ID 2", "Name", "Exception 2", "Y", "wpTest", "c:\data\test.dat"})
dt.Rows.Add(New String() {"3", "ID 3", "Name", "Exception 3", "Y", "wpTest", "c:\data\test.dat"})
 
 
Dim rptDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument
rptDoc.Load(Server.MapPath("/") & "crReports/ImportException.rpt")
rptDoc.SetParameterValue(0, "Test")
rptDoc.SetDataSource(dt)
ExportData(rptDoc)
 
 
Public Sub ExportData(ByRef oRpt As CrystalDecisions.CrystalReports.Engine.ReportDocument)
        Dim fs As IO.FileStream
        Dim FileSize As Long
        Dim oDest As New CrystalDecisions.Shared.DiskFileDestinationOptions
        Dim ExportFileName As String = Server.MapPath("/") & TempProcessFile & "/" & Session.SessionID & ".pdf"
 
        Try
            oRpt.ExportOptions.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile
            oRpt.ExportOptions.ExportFormatType = CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat
            oDest.DiskFileName = ExportFileName
            oRpt.ExportOptions.DestinationOptions = oDest
 
	    'This is the line that fails. 
            oRpt.Export()
 
            'Build Target Filename
            'Send the file to the user that made the request
            Response.Clear()
            Response.Buffer = True
            Response.AddHeader("Content-Type", "application/pdf")
            fs = New IO.FileStream(ExportFileName, IO.FileMode.Open)
            FileSize = fs.Length
            Dim bBuffer(CInt(FileSize)) As Byte
            fs.Read(bBuffer, 0, CInt(FileSize))
            fs.Close()
            Response.BinaryWrite(bBuffer)
            Response.Flush()
            Response.Close()
        Catch e As Exception
        End Try
    End Sub

Open in new window

ImportException.txt
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24422091
The SetParameterValue function is used to set values in following syntax
SetParameterValue(ParameterName, ParameterValue)

Are you sure 0 is name of the parameter?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 100

Expert Comment

by:mlmcc
ID: 24422301
Do you need to 0 parameter?

Try it this way

rptDoc.SetParameterValue("0", "Test")

mlmcc
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24422328
I believe 0 is the value being set and Test is the name of the parameter. If its of numeric type then try this
rptDoc.SetParameterValue("Test", 0)
0
 

Author Comment

by:dpinchot
ID: 24422444
After looking at the syntax, the SetParameter()  is overloaded to accept the following:

 SetParameter(index as integer, val as object)
 SetParameter(name as string, val as object)
 SetParameter(index as integer, val as object,subreport as string)

I was trying to do the first one, which should work if 0 is the index value of the first (and only) parameter.  I will try the second method,using name as string, val as object.  

I'll let you guys know ASAP.
 
0
 

Author Comment

by:dpinchot
ID: 24422795
After changing the code to:

rptDoc.SetParameterValue("ImportType", "test val")

Where "ImportType" is the name of the Crystal Report parameter, I am still getting the following error on the oRpt.Export() line:

Missing parameter values.

The .rpt file was created in CR8.5 and there is only one parameter on the report.



0
 
LVL 100

Expert Comment

by:mlmcc
ID: 24422835
Crystal arrays are all indexed from 1 not 0.

mlmcc
0
 

Author Comment

by:dpinchot
ID: 24422977
Useing rptDoc.SetParameterValue(1,"Test Val") yields an invalid index error.  

This is a look at the rptDoc.parameterfields object


-            CrystalDecisions.Shared.ParameterField      {CrystalDecisions.Shared.ParameterField}      CrystalDecisions.Shared.ParameterField
            _DefaultValueDisplayType      1      Integer
            AllowCustomValues      True      Boolean
+            CurrentValues      Count = 0      CrystalDecisions.Shared.ParameterValues
            DefaultValueDisplayType      DescriptionAndValue {1}      CrystalDecisions.Shared.DefaultValueDisplayType
+            DefaultValues      Count = 0      CrystalDecisions.Shared.ParameterValues
            DefaultValueSortMethod      BasedOnValue {0}      CrystalDecisions.Shared.DefaultValueSortMethod
            DefaultValueSortOrder      NoSort {0}      CrystalDecisions.Shared.DefaultValueSortOrder
            DiscreteOrRangeKind      DiscreteValue {0}      CrystalDecisions.Shared.DiscreteOrRangeKind
            EditMask      Nothing      String
            EnableAllowEditingDefaultValue      True      Boolean
            EnableAllowMultipleValue      False      Boolean
            EnableNullValue      False      Boolean
            HasCurrentValue      False      Boolean
            MaximumValue      Nothing      Object
            MinimumValue      Nothing      Object
            Name      "ImportType"      String
            ParameterFieldName      "ImportType"      String
            ParameterType      ReportParameter {0}      CrystalDecisions.Shared.ParameterType
            ParameterValueKind      StringParameter {4}      CrystalDecisions.Shared.ParameterValueKind
            ParameterValueType      StringParameter {4}      CrystalDecisions.Shared.ParameterValueKind
            PromptingType      DiscreteValue {0}      CrystalDecisions.Shared.DiscreteOrRangeKind
            PromptText      "ImportType"      String
            ReportName      ""      String
            ReportParameterType      ReportParameter {0}      CrystalDecisions.Shared.ParameterType
            UDefaultValueSortMethod      0      Integer
            UDefaultValueSortOrder      0      Integer
            UDiscreteOrRangeKind      0      Integer
            UParameterType      0      Integer
            UParameterValueKind      4      Integer
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 24423112
In my code, I set the databaselogon information and then set the parametervalue and it works. The only difference is that my report uses database directly. Trying setting the datasource first and then the parameter value.
0
 

Author Comment

by:dpinchot
ID: 24423194
CodeCruiser,

That was the ticket!  Placing the rptDoc.SetDataSource(dt) before the rptDoc.SetParameterValue did the trick.  It works both with he index value of 0, or the name "ImportType".

Thanks everyone, for you input, and to CodeCruiser for the solution!
0
 

Author Closing Comment

by:dpinchot
ID: 31582727
Thanks for you solution CodeCruiser!!
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24423360
See! Sometimes something so small and negligible holds the key!
0
 

Author Comment

by:dpinchot
ID: 24423653
You are so right.  Looking back at some of the sample code that I found on populating the cr at runtime, I can see that the data was always set first.   In my VB6 code, I always set my parameters first, and the last thing that I did before viewing the report was to set the data source.

I'm sure this will be just one in a long series of small changes in the way I have been developing, in my move into the .net world!

Thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 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