Solved

ASP.NET Crystal Reports PDF Export

Posted on 2009-05-18
14
674 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

17 Experts available now in Live!

Get 1:1 Help Now