Private Sub btnsave_Click(sender As Object, e As EventArgs) Handles btnsave.Click

       Dim com As New OleDbCommand()
        ' Try
        Dim fs As New FileStream(str, FileMode.Open)
        Dim data() As Byte = New [Byte](fs.Length) {}
        fs.Read(data, 0, fs.Length)
        fs.Close()
        'readed image
        Dim name As New OleDbParameter("namee", OleDbType.VarChar, 50)
        name.Value = TextBox7.Text

        Dim address As New OleDbParameter("address", OleDbType.VarChar, 50)
        address.Value = TextBox8.Text

        Dim contact As New OleDbParameter("contact", OleDbType.VarChar, 50)
        contact.Value = TextBox9.Text


        Dim dob As New OleDbParameter("dob", OleDbType.Date)
        Dim s As String = DateTimePicker4.Text.Trim
        Dim pattern As String = "MM/dd/yyyy"
        Dim parsedDate As Date = Date.ParseExact(s, pattern, Nothing)
        dob.Value = parsedDate


        Dim entry_date As New OleDbParameter("entry_date", OleDbType.Date)
        Dim a As String = DateTimePicker3.Text.Trim
        Dim entry As Date = Date.ParseExact(a, pattern, Nothing)
        entry_date.Value = entry


        Dim salary As New OleDbParameter("salary", OleDbType.Integer)
        salary.Value = Val(TextBox10.Text)

        Dim img As New OleDbParameter("picture", OleDbType.Binary)
        img.Value = data

        'Adding parameters
        com.Parameters.Add(name)
        com.Parameters.Add(address)
        com.Parameters.Add(contact)
        com.Parameters.Add(dob)
        com.Parameters.Add(entry_date)
        com.Parameters.Add(salary)
        com.Parameters.Add(img)

        com.Connection = conn
        com.CommandText = "insert into staff values(@name,@address,@contact,@dob,@entry_date,@salary,@img)"
        com.ExecuteNonQuery()
        MsgBox("Saved")
        Me.StaffTableAdapter.Fill(Me.Database1DataSet.staff)
        'Catch ex As Exception
        'MsgBox(ex.Message)
        'End Try
        end sub



        i am using this code to insert an images into access database. but when i click the button it gives me error that io exception unhandle...
        Dim fs As New FileStream(str, FileMode.Open)
        this is where i get erorr saying :
        The process cannot access the file 'C:\Users\itechstore\Documents\sunil camera photo\2011-01-02 05.48.08.jpg' because it is being used by another process.
        even those process is not been opened and i tried changing the directory but same error occured... i cant figure out the problem.. plz help me...

Dani AI

Generated

This exception means the file is held open by some handle when the insert runs. Common causes: another app (image viewer, Explorer thumbnailer, antivirus), a previous debug run left the process alive, or the same program opened the image earlier and never released it (Image.FromFile is a frequent culprit because it keeps the file locked). ’s reboot / check-for-stuck-process suggestion and ’s advice to use Sysinternals tools are both on point for finding the locker.

Practical, low-risk fixes to try in this order:

  • Close any viewer/editor and, if convenient, reboot or check Task Manager for leftover processes (Visual Studio processes sometimes remain).
  • Use a handle/monitor tool as suggested to identify the process that holds the file.
  • Replace manual FileStream handling with an API that opens, reads and closes immediately (or use an explicit Using block and FileShare). That both prevents leaks and avoids locks caused by preview code.

Example approaches (VB):

' simplest — opens, reads and closes in one call
Dim data() As Byte = File.ReadAllBytes(path)
' explicit Using with FileShare and correct array sizing
Using fs As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)
    Dim data(CInt(fs.Length) - 1) As Byte
    fs.Read(data, 0, data.Length)
End Using

If an image preview was created with Image.FromFile earlier, that keeps the file locked. Load the bytes and create the image from a MemoryStream, then clone the Bitmap so the stream can be closed:

Dim b() As Byte = File.ReadAllBytes(path)
Using ms As New MemoryStream(b)
    Dim img = Image.FromStream(ms)
    pictureBox.Image = New Bitmap(img) ' clone so ms/img can be disposed
    img.Dispose()
End Using

Extra tips: avoid the VB off-by-one when creating a byte array from fs.Length (use CInt(fs.Length) - 1 or File.ReadAllBytes), and if a locker cannot be removed, copy the file to a temp folder and read the copy.

Recommended Answers

All 2 Replies

Hi,

It could be any process that has the file open - tell me, by any chance did you run through a debug on this process before that opened the file but exited the process before it had a chance to close it? In which case it could be your orginal debug run which has locked the file.

If you are not going to modify the file in anyway try opening it in read only mode.

Also, try rebooting the machine and see if it will let you access it then - if so, whatever process locked the file has ended. I think in later versions of Windows there is away to see who/what has a file open, but I can't remember exactly how you do it (under the right mouse click?)

Note that the process which has the file open could be your process, if you've opened it before and failed to close it or try to open it again before disposal completes.

I'd strongly recommend downloading and learning how to use Process Monitor to troubleshoot these types of errors. That tool has saved me so many times in both development and IT support.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.