Passing parameters in VB.Net that converts Crystal Report to pdf

Posted on 2004-11-03
Last Modified: 2008-02-01
I have a VB.NET windows app, that has a button goes out and converts CR to PDF, and I don't view it.  This works fine.  I am wanting now to have a parameter passed into this CR pulling out of a table, passed to my report then generated into a pdf.  Say that the field in the table is strVariable.

Here is my code that does the conversion, could someone please help me and show me where I would put in this passing of a parameter.  Like where to put it in my VB.NET code?  Thanks so much...

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Place the following name spaces at the top of the code page
        Dim fileList() As String = System.IO.Directory.GetFiles("c:\inetpub\wwwroot\MCNETWebSite\IT_Work\")

        For Each fileSource As String In fileList
            If Strings.Right(fileSource, 4) = ".rpt" Then
                'Crystal Export Code Here
                Dim CrReport As New ReportDocument
                CrReport.Load(fileSource, OpenReportMethod.OpenReportByDefault)

                Dim CrExportOptions As ExportOptions
                Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions
                Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions

                ' Set the destination path and file name
                ' 41 is the spot where I need to get file name 41 (34 old)
                Dim mid As String
                mid = Strings.Mid(fileSource, 41)

                ' Gets the length of the filename
                Dim LengthObjName As String
                LengthObjName = Strings.Len(mid)

                ' Gets charactor to start at for taking out of the .rpt
                Dim LengthFrom_rpt As String
                LengthObjName = CInt(LengthObjName - 4)

                ' Gets the actual file name minus the .rpt extension
                Dim strVarName As String
                strVarName = Strings.Left(mid, LengthObjName)

                'Getobj = Microsoft.VisualBasic.GetObject(fileSource)
                Dim pdf_path As String
                pdf_path = "c:\CR_Convert_to_PDF\" & strVarName & ".pdf"
                CrDiskFileDestinationOptions.DiskFileName = pdf_path

                'Specify a page range (optional)
                CrFormatTypeOptions.FirstPageNumber = 1
                CrFormatTypeOptions.LastPageNumber = 3
                CrFormatTypeOptions.UsePageRange = True
                'Set export options
                CrExportOptions = CrReport.ExportOptions

                With CrExportOptions
                    'Set the destination to a disk file
                    .ExportDestinationType = ExportDestinationType.DiskFile
                    'Set the format to PDF
                    .ExportFormatType = ExportFormatType.PortableDocFormat
                    'Set the destination options to DiskFileDestinationOptions object
                    .DestinationOptions = CrDiskFileDestinationOptions
                    .FormatOptions = CrFormatTypeOptions
                End With

                ' Trap any errors that occur on export
                    'Export the report
                    MessageBox.Show("PDF File Generated from " & fileSource & " into location: " & pdf_path)
                Catch err As Exception
                End Try

            End If
        MessageBox.Show("All pdf files created successfully.")

    End Sub
Question by:vikingg97
    LVL 100

    Expert Comment

    You put the code somewhere before you call the export routine.

    I would put them after you set the export options.

    LVL 1

    Author Comment

    I forgot to put in my question how to to get this variable out of a table and to put it into a parameter within the CR?  

    Say if the field in the table is strVariable?

    I have looked around but haven't found any definitive answers.
    LVL 100

    Expert Comment

    LVL 10

    Expert Comment

    So you want to set  aparameter in runtime and then export the final diocument to pdf right? I have c3 code for you. Can you convert it to
    LVL 1

    Author Comment

    I can always try to convert it I suppose ebolek.  Remember this is not a web application.  This is a VB.NET windows application.  
    LVL 10

    Accepted Solution

    That is fine. Workd for windows.

    Here is the code.

    internal static void CreateParameter(ref ReportDocument report, string ParameterName, object value)
                      ParameterDiscreteValue  ParameterDiscreteValue;
                      ParameterValues  ParameterValues;
                      ParameterFieldDefinition param = report.DataDefinition.ParameterFields[ParameterName];
                      ParameterValues = new CrystalDecisions.Shared.ParameterValues();
                      ParameterDiscreteValue = new CrystalDecisions.Shared.ParameterDiscreteValue();      
                      ParameterDiscreteValue.Value = value;      
    LVL 10

    Expert Comment

    Glad I could help


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Crystal Reports: 5 Tests for Top Performance It is complete, your masterpiece report.  Not only does it meet your customer’s expectations, it blows them out the water, all they want is beautifully summarised and displayed in a myriad of ways. …
    I hate sub reports and always consider them the last resort in any reporting solution.  The negative effect on performance and maintainability is just not worth the easy ride they give the report writer.  Nine times out of ten reporting requirements…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now