• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

Crystal Reports VB.net 2010

Hello Experts,

I cant seem to figure out this problem. I am trying to export a Crystal Report to PDF. When I do it, it works fine, but if I try to export it a second time I get this error:

---------------------------
The process cannot access the file 'C:\Path\test.pdf' because it is being used by another process.
---------------------------

When I try to add cryrpt.dispose or cryrpt.close, it freezes the application. What am I doing wrong here?


 'Load the PDF
        Dim cryRpt As New ReportDocument
        Dim crParameterFieldDefinitions As ParameterFieldDefinitions
        Dim crParameterFieldDefinition As ParameterFieldDefinition
        Dim crParameterValues As New ParameterValues
        Dim crParameterDiscreteValue As New ParameterDiscreteValue

        cryRpt.Load(Application.StartupPath & "\Reports\StdRecpt.rpt")

        crParameterDiscreteValue.Value = (txt_TransNum.Text)
        crParameterFieldDefinitions = _
        cryRpt.DataDefinition.ParameterFields
        crParameterFieldDefinition = _
        crParameterFieldDefinitions.Item("Transact")
        crParameterValues = crParameterFieldDefinition.CurrentValues
        crParameterValues.Clear()
        crParameterValues.Add(crParameterDiscreteValue)
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)

        cryRpt.SetDatabaseLogon("user", "password", "sqlbase", "")


        'Export the PDF
        Try
            Dim CrExportOptions As ExportOptions
            Dim CrDiskFileDestinationOptions As New  _
            DiskFileDestinationOptions()
            Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions
            CrDiskFileDestinationOptions.DiskFileName = (Application.StartupPath & "test.pdf")
            CrExportOptions = cryRpt.ExportOptions
            With CrExportOptions
                .ExportDestinationType = ExportDestinationType.DiskFile
                .ExportFormatType = ExportFormatType.PortableDocFormat
                .DestinationOptions = CrDiskFileDestinationOptions
                .FormatOptions = CrFormatTypeOptions
            End With
            cryRpt.Export()


        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error: Exception", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Exit Sub
        End Try

Open in new window

0
triphen
Asked:
triphen
  • 10
  • 6
  • 4
1 Solution
 
Ammar GaffarSoftware EngineerCommented:
This error occurred because crystal object is not released yet, you may try to call garbage collect method to do an immediate garbage collection of all generations after exporting report.
cryRpt.Export()
GC.Collect() 

Open in new window

0
 
mlmccCommented:
WHere are you trying to do the dispose?

mlmcc
0
 
triphenAuthor Commented:
I have tried after export and after end try. Both cause the app to freeze.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Ammar GaffarSoftware EngineerCommented:
Did you try GC.Collect ? if yes and didn't work then you may run export function in a separate thread and kill that thread after confirming export.

You can use Boolean flag (exported) and update this flag to true after your line
cryRpt.Export()
exported = True

Open in new window


Then you may have a timer start ticking after exporting report by 2 seconds, and in this timer kill the thread which is used for export functionality.
0
 
triphenAuthor Commented:
GC.collect did not help either.

Cryrpt.close and cryrpt.dispose just cause the application to freeze....
0
 
triphenAuthor Commented:
Any other ideas?
0
 
mlmccCommented:
If you get around the issue, exporting a second report will overwrite the first. Is that what you want to happen?

If not then you need to add something to the pdf filename to make each export unique.

mlmcc
0
 
triphenAuthor Commented:
Yes exactly, I want to overwrite the first. But since it's "in use" I cant.
0
 
mlmccCommented:
When that happens can you open the PDF in the Acrobat Reader?
I am thinking perhaps the export is not complete.

mlmcc
0
 
Ammar GaffarSoftware EngineerCommented:
What the next step after exporting this report ? I mean your business scenario ?

Now you are trying to find a very specific part, and in some cases approach itself may changed based on your full scenario.

And as I explained in my comment before, you can run your export code in a separate thread and kill it upon completion, but personally I will not use this approach unless I have no other options.

In your scenario, you are exporting this file in the same directory of your executable file, for sure it will be for specific purpose and later you will gonna delete it, correct me if I am wrong.

If yes, I can suggest different approach, but before that, please tell me what is your next step after exporting this report (assume you manage to overwrite it)
0
 
triphenAuthor Commented:
mlmcc,

Yes I can open it, but I cant delete it (file in use)
0
 
triphenAuthor Commented:
My next step is to automatically email the PDF file. Which is working just fine.
0
 
mlmccCommented:
I believe you can email it directly as an attachment and a file is not really created.

I have the code in VB6 which you should be able to translate.

mlmcc
0
 
triphenAuthor Commented:
I have this in VB6 as well, and it works just fine as a file and attachment. I need to use our GMail account and SMTP.
0
 
Ammar GaffarSoftware EngineerCommented:
So why you don't take  mlmcc  comment by saving this file every time with unique name (GUID as an example) and remove all exported files in your application start-up or End (a lot of events option to clear this temp files).

Your business scenario is to automatically email the exported report, and normally you need to remove this exported file after sending for security reasons from business point of view.

Now you are looping in overwrite point, which is not easy, because you don't know how crystal reports object works.

Good Luck
0
 
mlmccCommented:
Is this an interactive application or does it just loop on its own?
If interactive have you tried waiting say 5 minutes before trying the next export?

I believe the export starts and then returns control to the application before the report is done.  SOme reports may take several minutes to build and export and that time can be increased if there is a load on the network or the database.

mlmcc
0
 
triphenAuthor Commented:
The report is very short and is less than a page long. It is definitely loading in less than a few seconds. I have tried to wait, I dont think this is the issue here.

For some reason the .Export is only exporting and leaving the document open and not closing it.
0
 
mlmccCommented:
Have you checked to see if the document is opened for viewing but behind the current page or even opened minimized?

mlmcc
0
 
triphenAuthor Commented:
Yes I have looked at that too. Nothing works!

I am going to just leave it as is for now, maybe find a solution later.
0
 
triphenAuthor Commented:
No solution :/ closing case.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 10
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now