Click here to Skip to main content
14,699,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good morning everyone.

I am reading an attachment (JPEG file) from an Access database (.accdb).

The user selects a Driver from a combobox, the program then goes to get the drivers license image. The second time that the user selects a given drivers name the routine crashes, with “File is being used by another process” when I try to delete the saved filename. (See Marker in code)

Private Sub GetLicenceImages(ByVal theDriver As String, theLicence As String)
    Dim engine As New Dao.DBEngine
    Dim database As Dao.Database = engine.OpenDatabase(CommonDBPath, False, False, "MS Access;PWD=xxxxxxx")

    Dim rs As Dao.Recordset = database.OpenRecordset("Select HRL_Licence_Image From HR_Licences WHERE HRL_Name_Surname = '" & theDriver & "' AND HRL_Licence_Type = '" & theLicence & "'")

    While Not rs.EOF
        Dim rs1 As Dao.Recordset2 = CType(rs.Fields("HRL_Licence_Image").Value, Recordset2)
        While Not rs1.EOF
            DriversLicencePicPath = LocalTempPath & rs1("FileName").Value.ToString
            Dim fld As Dao.Field2 = CType(rs1("FileData"), Field2)

            System.IO.File.Delete(DriversLicencePicPath)   '<===== CRASH OCCURS HERE

            fld.SaveToFile(DriversLicencePicPath)

            '--------------------------------
            Dim img As Image = Image.FromFile(DriversLicencePicPath)
            but_Driver_Licence.BackgroundImage = img
            but_Driver_Licence.Text = ""
            but_Delete_Drivers_Pic.Visible = True
            '--------------------------------

            rs1.MoveNext()

        End While

        rs.MoveNext()

    End While

    rs.Close()

End Sub


The process that is using the file is this program and I have no idea what to Close or Dispose (or other) in order to show the image the second time.

Any advice or guidance would be much appreciated.

Regards and thanks.

What I have tried:

I have removed the image from the but_Driver_Licence.BackgroundImage button before entering this routine.
Don't know what else to do.
Posted
Updated 11-Sep-16 23:21pm

1 solution

Quote:
I have removed the image from the but_Driver_Licence.BackgroundImage button before entering this routine.

That doesn't mean that the file is freed. If you loaded the image from the file using Image.FromFile, then the documentation is pretty clear: Image.FromFile Method (String, Boolean) (System.Drawing)[^]
Quote:
The file remains locked until the Image is disposed.
Which means as long as your code has not explicitly called Dispose on the Image class instance itself, the file cannot be written, moved or deleted.
When you remove it from the Button, Dispose it - or better when you load teh file into an image, copy it to a new bitmap and Dispose the original so that teh period it is locked is as short as possible.

And don't do database access like that: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
   
Comments
Darrell de Wet 12-Sep-16 4:28am
   
Thank you.
You have helped me a few times before and your advice is indeed appreciated.
OriginalGriff 12-Sep-16 4:40am
   
You're welcome!

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