Skip to Content
author's profile photo Former Member
Former Member

Export crystal report to pdf

Hi,

I want to export my crystal report to pdf and send a mail programically. Crystal report has 4 parameters and how can i pass these parameters in code...

My code is as follows...

Dim crParameterFieldDefinitions As ParameterFieldDefinitions

Dim crParameterFieldDefinition As ParameterFieldDefinition

Dim crParameterValues As New ParameterValues

Dim crParameterDiscreteValue As New ParameterDiscreteValue

Dim crParamenter_array As New ParamArrayAttribute

Dim cryRpt As New ReportDocument

Dim DefaultPrinterName As String

Dim oPS As New System.Drawing.Printing.PrinterSettings

Dim sFilePath As String

Dim sPath As String

Dim sFileLoc As String

Dim sFileName As String

Try

sPath = Path.GetTempPath()

DefaultPrinterName = oPS.PrinterName

sFilePath = GetCrystalReport(_REPRT_CODE)

sFileLoc = sPath + "Reports\" + _REPRT_CODE + ".rpt"

If sFilePath = String.Empty OrElse sFilePath = Nothing Then

TNotification.StatusBarError("File Not Found.")

Return False

Else

cryRpt.Load(sFileLoc)

End If

If Not SetDbLogonInfo(cryRpt) Then

TNotification.StatusBarError("Database Login Failed.")

Return False

Else

crParameterDiscreteValue.Value = _empid

crParameterDiscreteValue.Value = _month

crParameterDiscreteValue.Value = _parea

crParameterDiscreteValue.Value = _year

crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields

crParameterFieldDefinition = crParameterFieldDefinitions.Item("empid")

crParameterFieldDefinition = crParameterFieldDefinitions.Item("month")

crParameterFieldDefinition = crParameterFieldDefinitions.Item("parea")

crParameterFieldDefinition = crParameterFieldDefinitions.Item("year")

crParameterValues = crParameterFieldDefinition.CurrentValues

' crParameterValues.Clear()

crParameterValues.Add(crParameterDiscreteValue)

crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)

cryRpt.PrintOptions.PrinterName = DefaultPrinterName

Dim rep As CrystalDecisions.CrystalReports.Engine.FieldObject = Nothing

'sending report to the printer

'cryRpt.PrintToPrinter(_noOfCpys, False, 1, 100)

Dim CrExportOptions As ExportOptions

Dim CrDiskFileDestinationOptions As New _

DiskFileDestinationOptions()

Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()

' CrDiskFileDestinationOptions.DiskFileName = "c:\" + _rptCode + "_" + _docEntry + ".pdf"

'sFileName = "\\B1devsrv\b1_shr\Attachments\" + _rptCode + "_" + _docEntry.ToString + ".pdf"

sFileName = TSQL.GetSingleRecord("Select AttachPath from OADP").ToString.Trim + _empid + "_payslip.pdf"

CrDiskFileDestinationOptions.DiskFileName = sFileName

CrExportOptions = cryRpt.ExportOptions

With CrExportOptions

.ExportDestinationType = ExportDestinationType.DiskFile

.ExportFormatType = ExportFormatType.PortableDocFormat

.DestinationOptions = CrDiskFileDestinationOptions

.FormatOptions = CrFormatTypeOptions

End With

cryRpt.Export()

If SendMail(_empid.ToString, sFileName) Then

TNotification.MessageBox("Email Sent")

End If

If File.Exists(sFileLoc) Then

' File.Delete(strFileLoc)

End If

End If

Return True

Catch ex As Exception

Throw ex

End Try

I got invalid index as error

Add a comment
10|10000 characters needed characters exceeded

Related questions

2 Answers

  • Posted on Dec 13, 2012 at 05:16 AM

    Hi,

    Below is an example of setting the parameter name dockey@ in crystal report.

    Just repeat this block of code for each of your parameter.

    If Not CRRpt.ParameterFields("dockey@") Is Nothing Then    CRRpt.ParameterFields("dockey@").CurrentValues.Clear()    Dim ParamValue As New CrystalDecisions.Shared.ParameterDiscreteValue()    ParamValue.Value = DocEntry    CRRpt.ParameterFields("dockey@").CurrentValues.Add(ParamValue)End If

    Do note that the ParamValue.Value accepts an object type variable.

    You have to supply the correct type for each of your CR params.

    Regards
    Edy

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 10, 2013 at 06:43 PM

    hi, Former Member

    i try to past your code in my project, but need add reference... .and i dont know the reference that i need to put in the project...

    could you say me?

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.