Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,
Iam using a function to export a crystal report to pdf file.
First it is running fine. but next time iam getting this error:
System.IO.Exception and it is saying the file is being used by another process

HERE IS MY CODING:
VB
Private Sub GetReport()
        Dim strSQL As String
        Dim objReport As New frmReport


        Try
            strSQL = "SP_rpt_SalesRptByMainCategory '" & Now.ToLongDateString & "','" & Now.ToLongDateString & "'," & gBranchCode
DIM p AS STRING
        p = AppDomain.CurrentDomain.BaseDirectory
        p = p.Replace("\bin\", "\Reports\")
            cryRpt.Load(p & "rptMainCat.rpt")
            Dim objds As DataSet = ExexcuteSQLQuery(strSQL)
            cryRpt.SetDataSource(objds.Tables(0))
            objReport.CrystalReportViewer2.ReportSource = cryRpt
            Dim CrExportOptions As ExportOptions
            Dim CrDiskFileDestinationOptions As New  _
            DiskFileDestinationOptions()
            Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
            'Dim FileToDelete As String
            'FileToDelete = Application.StartupPath & "\MainCategoryReport.pdf"
            'If System.IO.File.Exists(FileToDelete) = True Then
            '    System.IO.File.Delete(FileToDelete)
            'End If
            StrFilePath = "\MainCategoryReport" & "_" & Now.ToLongDateString & ".pdf"

            CrDiskFileDestinationOptions.DiskFileName = Application.StartupPath & StrFilePath '' "c:\Report1.pdf"

            CrExportOptions = cryRpt.ExportOptions
            With CrExportOptions
                .ExportDestinationType = ExportDestinationType.DiskFile
                .ExportFormatType = ExportFormatType.PortableDocFormat
                .DestinationOptions = CrDiskFileDestinationOptions
                .FormatOptions = CrFormatTypeOptions
            End With
            cryRpt.Export()
        Catch ex As Exception
            MsgBox(ex.ToString)
            Exit Sub
        End Try     
    End Sub

    Private Shared Function ExexcuteSQLQuery(ByVal StrSql As String) As DataSet
        Dim objDA As SqlDataAdapter = New SqlDataAdapter(StrSql, objForEMailConnection)
        Dim objDS As DataSet = New DataSet
        objDA.Fill(objDS, "myDataset")
        Return objDS
    End Function

Thanks in advance
Nirmala Saravanan
Posted
Updated 22-Apr-13 1:53am
v2
Comments
Richard MacCutchan 22-Apr-13 8:15am    
Get rid of the other process that is holding the file.
ZurdoDev 22-Apr-13 9:55am    
You need to determine why the file is still open. Is it your code? Or do you actually have the file open?
Bernhard Hiller 9-Jul-14 4:21am    
By the way, CrDiskFileDestinationOptions.DiskFileName = Application.StartupPath & StrFilePath is also a problem: a normal (non-administrative) user may not have write access there! Use a sub-folder of e.g. c:\programdata instead.

1 solution

This is easy to investigate.

First of all, the similar question was asked here many times, and from this experience I know: in most cases the blocking process is your own process. You could have forgotten to dispose/close something in the same application. So, first of all, check it up. To explore this possibility, please see my past answer:
Clearing a Handle in C#[^].

In this answer, pay attention for the using of the using statement which helps you to guarantee that appropriate file system object is properly disposed after use, not keeping the file locked.

In some cases, you really need to investigate which process holds which file. For this, I recommend using one utility from the Sysinternals Suite. This set of utilities (formerly from Winternals company, presently at Microsoft) is a must-have for any developer, please see:
http://technet.microsoft.com/en-us/sysinternals/bb842062[^],
http://technet.microsoft.com/en-us/sysinternals/bb545027[^].

The utility you need is "handle.exe", please see:
http://technet.microsoft.com/en-us/sysinternals/bb896655[^].

In your case, you use it with file name parameter:
handle.exe <file_name>


This utility will scan all kinds of handles, not just file handles. For file, it will scan all file handles matching the file name (so it does not have to be a full path name) and return information sufficient to identify each process, including its pid. So, if you need more information on a process in question, you can also use other Sysinternals utilities, in particular, its Process Explorer:
http://technet.microsoft.com/en-us/sysinternals/bb896653[^].

Good luck,
—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900