ASP.NET Crystal Reports PDF Export

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

dpinchotAsked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
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
 
CodeCruiserCommented:
YES. Is the above code not working?
0
 
dpinchotAuthor Commented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
CodeCruiserCommented:
The SetParameterValue function is used to set values in following syntax
SetParameterValue(ParameterName, ParameterValue)

Are you sure 0 is name of the parameter?
0
 
mlmccCommented:
Do you need to 0 parameter?

Try it this way

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

mlmcc
0
 
CodeCruiserCommented:
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
 
dpinchotAuthor Commented:
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
 
dpinchotAuthor Commented:
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
 
mlmccCommented:
Crystal arrays are all indexed from 1 not 0.

mlmcc
0
 
dpinchotAuthor Commented:
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
 
dpinchotAuthor Commented:
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
 
dpinchotAuthor Commented:
Thanks for you solution CodeCruiser!!
0
 
CodeCruiserCommented:
See! Sometimes something so small and negligible holds the key!
0
 
dpinchotAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.