Click here to Skip to main content
15,896,439 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When i delete image from folder "Error Message is
(Because it is being used by another process).

What I have tried:

VB
Dim Get_Image_From_Product_Image As New SqlCommand("SELECT Business_Image FROM ImageDB.dbo.Administrator_Account Where Employee_Code = '" & CmbUserCode & "'", sqlcon)

            Dim Get_Image_FromProduct_Image = Get_Image_From_Product_Image.ExecuteScalar().ToString()

 PicturePath = Application.StartupPath & "\Product_Image"

  System.IO.File.Delete(PicturePath & "\" & Get_Image_FromProduct_Image)
Posted
Updated 4-Sep-18 6:56am
v3
Comments
CHill60 4-Sep-18 12:04pm    
The error is quite clear - You have the file open in another process, or if this is on a server someone has the file open.
As an aside your code is vulnerable to SQL Injection Attack - don't concatenate strings to create sql commands - use parameters instead
Richard Deeming 5-Sep-18 13:26pm    
Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

Dim Get_Image_From_Product_Image As New SqlCommand("SELECT Business_Image FROM ImageDB.dbo.Administrator_Account Where Employee_Code = @Employee_Code", sqlcon)
Get_Image_From_Product_Image.Parameters.AddWithValue("@Employee_Code", CmbUserCode.SelectedItem)

1 solution

As CHill60 says, the error message is pretty clear - the file you are trying to write to is in use. Almost certainly, it's in use by your own software - so start by checking everywhere you read or write that file. Unless you Close (and preferably Dispose) all the stream you use when you are finished with them, they remain open and you app cannot use that file again.
The same thing applies if you load an Image from the file: the source file remains locked until the Image has been Disposed or your app closes, whichever comes first.
 
Share this answer
 

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