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...

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.

This article has been dead for over six months. Start a new discussion instead.