0

I am barely new to VB.NET and as part of our final project, we are tasked to create a program and use MS Access as our database. Now the problem is whenever we are 'Updating' the picture in our database, the error will always show System.Data.OleDb.OleDbException (0x80004005): Could not save; currently locked by another user. And the funny thing is that when we repeatedly click the Update Button it will now prompt Account updated and proceeds to update the picture in the database. How can I fix this?

Our code is as follow:

 Using modify As New OleDbCommand("Update AccountDetails Set [Picture] = @Picture WHERE ID = " & id_modify & "", connect)
    connect.Close()
    connect.Open()

    Dim arrImage() As Byte
    Dim mstream As New System.IO.MemoryStream()
    picNewAccnt.Image.Save(mstream, Imaging.ImageFormat.Png) 'the picture box that will show the picture inserted
    arrImage = mstream.GetBuffer

    Dim filesize As UInt32
    filesize = mstream.Length
    mstream.Close()
    mstream.Dispose()

    Try
        modify.Parameters.AddWithValue("@Picture", arrImage)
        modify.ExecuteNonQuery() 'the part where the error always occur
        MsgBox("Account updated ", MsgBoxStyle.Information, "Account Update Successful")
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

End Using

I tried checking all connections to the database and making sure that it is enclosed in the Using Statement.

  • 1
    Where are the Using statements you mentioned? -- You should not store the Connection object(s), declare and dispose any of these where they're used. Reusing Connection objects can also slow down read operations quite a lot, especially with an Access database -- This is quite wrong: `arrImage = mstream.GetBuffer()`, see the docs to understand why – Jimi Apr 11 '23 at 12:49
  • There's some code in this [post](https://stackoverflow.com/a/69638011/10024425) that may be helpful. – Tu deschizi eu inchid Apr 12 '23 at 01:02

0 Answers0