Hi. I am new to VS.NET and Crystal Reports. I have figured out how to pass parameters to a Crystal Report Viewer on a web page but it didn't print very well. So I have successfully opened my Crystal Report in Adobe Reader (prints much better). I have attempted to pass the PDF file a parameter (actually I need to pass 2 parameters but I will start with one) but failed. I use the following code....
Public Sub ExportToAdobe(ByVal strReportPath As String, ByVal strAdobeFileName As String)
' This procedure exports data to Adobe file
Dim objCrystalReportDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
Dim objCrystalFileDestinationOptions As New CrystalDecisions.Shared.DiskFileDestinationOptions()
objCrystalReportDocument.Load(strReportPath)
' Set the datasource appropriately for your application, then export as follows:
objCrystalFileDestinationOptions.DiskFileName = strAdobeFileName
objCrystalReportDocument.ExportOptions.DestinationOptions = objCrystalFileDestinationOptions
objCrystalReportDocument.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
objCrystalReportDocument.PrintOptions.PaperOrientation = PaperOrientation.Landscape
objCrystalReportDocument.PrintOptions.PaperSize = PaperSize.PaperLegal
Dim mypara As New CrystalDecisions.Shared.ParameterDiscreteValue()
mypara.Value = "John Do"
objCrystalReportDocument.DataDefinition.ParameterFields("txtOccurrence".ApplyCurrentValues(mypara.Value)
objCrystalReportDocument.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat
objCrystalReportDocument.Export()
End Sub
Public Sub SendPdfToClient(ByVal strAdobeFileName As String)
' This procedure sends new Adobe file to client
HttpContext.Current.Response.ClearHeaders()
HttpContext.Current.Response.ClearContent()
HttpContext.Current.Response.ContentType = "application/pdf"
HttpContext.Current.Response.WriteFile(strAdobeFileName)
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.Close()
End Sub
I get the following error:
Specified cast is not valid
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Specified cast is not valid.
It marks this line in red:
objCrystalReportDocument.DataDefinition.ParameterFields("txtOccurrence".ApplyCurrentValues(mypara.Value)
The parameter (txtOccurrence) is of type string on the rpt file. What am I doing wrong? Am I missing a line of code that will cast the parameter?
Any help is greatly appreciated.
Mich
Public Sub ExportToAdobe(ByVal strReportPath As String, ByVal strAdobeFileName As String)
' This procedure exports data to Adobe file
Dim objCrystalReportDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
Dim objCrystalFileDestinationOptions As New CrystalDecisions.Shared.DiskFileDestinationOptions()
objCrystalReportDocument.Load(strReportPath)
' Set the datasource appropriately for your application, then export as follows:
objCrystalFileDestinationOptions.DiskFileName = strAdobeFileName
objCrystalReportDocument.ExportOptions.DestinationOptions = objCrystalFileDestinationOptions
objCrystalReportDocument.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
objCrystalReportDocument.PrintOptions.PaperOrientation = PaperOrientation.Landscape
objCrystalReportDocument.PrintOptions.PaperSize = PaperSize.PaperLegal
Dim mypara As New CrystalDecisions.Shared.ParameterDiscreteValue()
mypara.Value = "John Do"
objCrystalReportDocument.DataDefinition.ParameterFields("txtOccurrence".ApplyCurrentValues(mypara.Value)
objCrystalReportDocument.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat
objCrystalReportDocument.Export()
End Sub
Public Sub SendPdfToClient(ByVal strAdobeFileName As String)
' This procedure sends new Adobe file to client
HttpContext.Current.Response.ClearHeaders()
HttpContext.Current.Response.ClearContent()
HttpContext.Current.Response.ContentType = "application/pdf"
HttpContext.Current.Response.WriteFile(strAdobeFileName)
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.Close()
End Sub
I get the following error:
Specified cast is not valid
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Specified cast is not valid.
It marks this line in red:
objCrystalReportDocument.DataDefinition.ParameterFields("txtOccurrence".ApplyCurrentValues(mypara.Value)
The parameter (txtOccurrence) is of type string on the rpt file. What am I doing wrong? Am I missing a line of code that will cast the parameter?
Any help is greatly appreciated.
Mich