Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing parameters to PDF

Status
Not open for further replies.

mlang11

Programmer
Nov 27, 2002
5
CA
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
 
Please help me if you got any solution to this problem.
I am facing the same exact problem
 
Try to pass in a simple string variable rather than mypara.Value

Cheers,
- Ido CUT, Visual CUT, and DataLink Viewer:
view, e-mail, export, burst, distribute, and schedule Crystal Reports.
 
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

'''''''''''''''''''''''''''''''''''''''''''''''''''''
' Set Parameters for the report
Dim param As New ParameterDiscreteValue()
Dim values As New ParameterValues()

param.Value = mtxtOccur
values.Add(param)

'the index number is for the first parameter, specified by the 0
objCrystalReportDocument.DataDefinition.ParameterFields(0).ApplyCurrentValues(values)

param = New ParameterDiscreteValue()
param.Value = mtxtAcc
values.Add(param)

'the second param in the report, specified by the 1
objCrystalReportDocument.DataDefinition.ParameterFields(1).ApplyCurrentValues(values)

Dim txtConn As String = MyConnectionString

' Substitute Stored Proc Name for SQL string
Dim txtSQL As String = "usp_ReportOffSched"
Dim dbCtyCrtSch As New System.Data.SqlClient.SqlDataAdapter()
Dim dsCtyCrtSch As New DataSet()

dbCtyCrtSch = New System.Data.SqlClient.SqlDataAdapter(txtSQL, txtConn)
dbCtyCrtSch.SelectCommand.CommandType = CommandType.StoredProcedure

dsCtyCrtSch.Clear()
dbCtyCrtSch.Fill(dsCtyCrtSch)

objCrystalReportDocument.SetDataSource(dsCtyCrtSch)
CrystalReportViewer1.ReportSource = objCrystalReportDocument
CrystalReportViewer1.DataBind()

objCrystalReportDocument.ExportOptions.DestinationOptions = objCrystalFileDestinationOptions
objCrystalReportDocument.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
objCrystalReportDocument.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat

' Trap any errors that occur on export
Try
objCrystalReportDocument.Export()
Catch err As Exception
System.Windows.Forms.MessageBox.Show(err.Message & " " & err.HelpLink & " " & err.Source, "Export to PDF Error", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Exclamation, Windows.Forms.MessageBoxDefaultButton.Button1, Windows.Forms.MessageBoxOptions.DefaultDesktopOnly)
End Try

dsCtyCrtSch.Dispose()
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"

' catch any error when sending Adobe to the client
Try
HttpContext.Current.Response.WriteFile(strAdobeFileName)
Catch err As Exception
System.Windows.Forms.MessageBox.Show(err.Message, "Error sending new Adobe file to client.", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Exclamation, Windows.Forms.MessageBoxDefaultButton.Button1, Windows.Forms.MessageBoxOptions.DefaultDesktopOnly)
End Try

HttpContext.Current.Response.Flush()
HttpContext.Current.Response.Close()
End Sub

Hope this helps
Mich
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top