Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Crystal Reports VB.net 2010

Posted on 2015-01-17
20
Medium Priority
?
256 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
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 101

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 101

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 101

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 101

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 101

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 101

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Hot fix for .Net Crystal Reports 10.2.3600.0 to fix problems with sub reports running on 64 bit operating systems ISSUE: Reports which contain subreports fail with error "Missing Parameter Value" DEPLOYMENT SERVER OS: Windows 2008 with 64 bi…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

876 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