Solved

ASP.NET Crystal Reports PDF Export

Posted on 2009-05-18
14
691 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 101

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 101

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

717 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