Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ASP.NET Crystal Reports PDF Export

Posted on 2009-05-18
14
Medium Priority
?
697 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

604 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