Solved

Crystal Reports VB.net 2010

Posted on 2015-01-17
20
243 Views
Last Modified: 2015-04-26
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
Comment
Question by:triphen
  • 10
  • 6
  • 4
20 Comments
 
LVL 12

Expert Comment

by:Ammar Gaffar
Comment Utility
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
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
WHere are you trying to do the dispose?

mlmcc
0
 

Author Comment

by:triphen
Comment Utility
I have tried after export and after end try. Both cause the app to freeze.
0
 
LVL 12

Expert Comment

by:Ammar Gaffar
Comment Utility
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
 

Author Comment

by:triphen
Comment Utility
GC.collect did not help either.

Cryrpt.close and cryrpt.dispose just cause the application to freeze....
0
 

Author Comment

by:triphen
Comment Utility
Any other ideas?
0
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
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
 

Author Comment

by:triphen
Comment Utility
Yes exactly, I want to overwrite the first. But since it's "in use" I cant.
0
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
When that happens can you open the PDF in the Acrobat Reader?
I am thinking perhaps the export is not complete.

mlmcc
0
 
LVL 12

Expert Comment

by:Ammar Gaffar
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:triphen
Comment Utility
mlmcc,

Yes I can open it, but I cant delete it (file in use)
0
 

Author Comment

by:triphen
Comment Utility
My next step is to automatically email the PDF file. Which is working just fine.
0
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
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
 

Author Comment

by:triphen
Comment Utility
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
 
LVL 12

Expert Comment

by:Ammar Gaffar
Comment Utility
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
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
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
 

Author Comment

by:triphen
Comment Utility
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
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
Have you checked to see if the document is opened for viewing but behind the current page or even opened minimized?

mlmcc
0
 

Accepted Solution

by:
triphen earned 0 total points
Comment Utility
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
 

Author Closing Comment

by:triphen
Comment Utility
No solution :/ closing case.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

12 Experts available now in Live!

Get 1:1 Help Now