Solved

Crystal Reports VB.net 2010

Posted on 2015-01-17
20
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 6
  • 4
20 Comments
 
LVL 12

Expert Comment

by:Ammar Gaffar
ID: 40556145
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
ID: 40556796
WHere are you trying to do the dispose?

mlmcc
0
 

Author Comment

by:triphen
ID: 40556839
I have tried after export and after end try. Both cause the app to freeze.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:Ammar Gaffar
ID: 40557114
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
ID: 40558160
GC.collect did not help either.

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

Author Comment

by:triphen
ID: 40559190
Any other ideas?
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 40560125
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
ID: 40560333
Yes exactly, I want to overwrite the first. But since it's "in use" I cant.
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 40560414
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
ID: 40560423
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
 

Author Comment

by:triphen
ID: 40560541
mlmcc,

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

Author Comment

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

Expert Comment

by:mlmcc
ID: 40560855
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
ID: 40561052
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
ID: 40561422
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
ID: 40561957
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
ID: 40565937
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
ID: 40575731
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
ID: 40603962
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
ID: 40744668
No solution :/ closing case.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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