I am getting an error
System.IO.IOException: 'The process cannot access the file 'C:\Users\james\Desktop\chem\d70e1e925d85464e259cb8a7488aec78.png' because it is being used by another process.'
I am trying to delete a picture after uploading it to my database.
Here is my code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Files As New OpenFileDialog
Files.Filter = "Image(*.JPG;*.PNG;*.GIF)|*.jpg;*.png;*.gif"
' Allow the user to select multiple images.
Files.Multiselect = True
Files.Title = "Select an image"
Files.ShowDialog()
PictureBox1.Image = Image.FromFile(Files.FileName)
Try
Dim sqlcon As New SqlConnection("constring")
Dim sqladapt = New SqlDataAdapter("Select * from [Table]", sqlcon)
sqlcon.Open()
Dim cmd As SqlClient.SqlCommand
Dim sql As String = "insert into [Table] values(@ID,@Question,@Answers,@Howtowork,@Chapter,@img)"
cmd = New SqlClient.SqlCommand(sql, sqlcon)
Using ms As MemoryStream = New MemoryStream()
Dim bm As Bitmap = New Bitmap(PictureBox1.Image)
bm.Save(ms, PictureBox1.Image.RawFormat)
Dim arrPic() As Byte = ms.GetBuffer()
' Make sure that all tables have the same type of information that can be entered, if not you will recieve an error.
cmd.Parameters.AddWithValue("@ID", DataGridView1.Rows.Count)
cmd.Parameters.AddWithValue("@Question", TextBox2.Text)
cmd.Parameters.AddWithValue("@Answers", TextBox3.Text)
cmd.Parameters.AddWithValue("@Howtowork", TextBox4.Text)
cmd.Parameters.AddWithValue("@Chapter", ComboBox2.Text)
cmd.Parameters.AddWithValue("@img", arrPic)
cmd.ExecuteNonQuery()
ms.Close()
End Using
sqlcon.Close()
MessageBox.Show("New Record Added")
Catch ex As Exception
End Try
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
refreshtable()
System.IO.File.Delete(Files.FileName)
End Sub
What I have tried:
I have tried an ms.dispose, bm.dispose and a picturebox1.dispose. I figured that one of those would disconnect from the file before so I could delete. However, I am still getting the same error