Question : "Invalid Argument provided." Error when Generating PDFs in VB.NET Application via Crystal Reports Viewer

I have developed a simple application to generate PDF documents. It works just fine, except it seems that approximately every 8th generated report it gives me a "Invalid Argument provided. Failed to retrieve data from the database. Error in File C:\DOCUME~1\XXXXX\LOCALS~1\Temp\PDF_Report {EDB21020-D860-4FEE-B5FA-55AD61847E3F}.rpt: Invalid argument for database." error. The application is intended to read a set of variables from a text file, generate the report, then export that file to the appropriate folder. Here is my code:

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.IO
Imports System.Threading.Thread

Public Class Form1

    Private Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click

        ' Create new StreamReader instance with Using block.
        Using reader As StreamReader = New StreamReader("file.txt")

            Dim counter As Integer = 0

            While (Not reader.EndOfStream)
                Dim line As String
                Dim AssessmentID As String
                Dim VisitID As String
                Dim rptPath As String

                line = reader.ReadLine

                AssessmentID = line.Substring(0, 7)
                VisitID = line.Substring(8, 7)

                rptPath = "C:\PDF_Report.rpt"

                Dim CR As New ReportDocument

                CR.Load(rptPath)
                CR.SetDatabaseLogon("user", "password", "server", "db")

                Dim crParameterDiscreteValue As New ParameterDiscreteValue
                Dim crParameterFieldDefinitions As ParameterFieldDefinitions
                Dim crParameterFieldLocation As ParameterFieldDefinition
                Dim crParameterValues As New ParameterValues
                '
                ' Get the report's parameters collection.
                '
                crParameterFieldDefinitions = CR.DataDefinition.ParameterFields
                '
                crParameterFieldLocation = crParameterFieldDefinitions.Item("@AssessmentID")
                crParameterValues = crParameterFieldLocation.CurrentValues
                crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
                crParameterDiscreteValue.Value = Int(AssessmentID)
                crParameterValues.Add(crParameterDiscreteValue)
                crParameterFieldLocation.ApplyCurrentValues(crParameterValues)

                crParameterFieldLocation = crParameterFieldDefinitions.Item("@Visit_OID")
                crParameterValues = crParameterFieldLocation.CurrentValues
                'crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
                crParameterDiscreteValue.Value = VisitID
                crParameterValues.Add(crParameterDiscreteValue)
                crParameterFieldLocation.ApplyCurrentValues(crParameterValues)

                crParameterFieldDefinitions = CR.DataDefinition.ParameterFields

                CrystalReportViewer1.ReportSource = CR

                txtAssessmentVisit.Text = AssessmentID & "/" & VisitID

                Dim myDiskFileDestinationOptions As CrystalDecisions.Shared.DiskFileDestinationOptions
                myDiskFileDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions()
                Dim myExportOptions As CrystalDecisions.Shared.ExportOptions
                Dim myExportFile As String
                myExportFile = "S:\Missed Assessments\MIASA" & AssessmentID & ".pdf"
                myDiskFileDestinationOptions.DiskFileName = myExportFile
                myExportOptions = CR.ExportOptions
                myExportOptions.DestinationOptions = myDiskFileDestinationOptions
                myExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
                myExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat

                CR.Export()
                GC.Collect()

                counter = counter + 1
                txtNumberProcessed.Text = counter.ToString()

            End While
        End Using

        MessageBox.Show("Export Complete")

    End Sub
End Class

Thank you for your assistance.

Answer : "Invalid Argument provided." Error when Generating PDFs in VB.NET Application via Crystal Reports Viewer

Try doing garbabe collection twice:
Clean up memory .
GC.Collect();
GC.WaitForPendingFinalizers();

// The GC needs to be called twice in order to get the
// Finalizers called - the first time in, it simply makes
// a list of what is to be finalized, the second time in,
// it actually the finalizing. Only then will the
// object do its automatic ReleaseComObject.
GC.Collect();
GC.WaitForPendingFinalizers();
Random Solutions  
 
programming4us programming4us