0

I'm trying to get an image stored in my Access database (via an SQL statement) to display in a picture box on a windows form application.

The code:

Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;DataSource=C:\Users\finla\Documents\userDB.accdb")
Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM Users WHERE ID =" & LoginSystem.currentUSERID, con)
con.Open()
Dim sdr As OleDbDataReader = cmd.ExecuteReader()
sdr.Read()
nicknameBox.Text = sdr.Item("Nickname")
imageBox.Image = sdr.Item("ProfileImage")
con.Close()

However, when using this code, I get an error saying: System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'System.Drawing.Image'.' - Anyone able to help?

Finlay
  • 5
  • 3
  • What's the datatype of the ProfileImage field in the Users table? Is it an image/blob type or string with the file path? – Hursey Apr 07 '22 at 22:12
  • It's stored as an attachment in Access. – Finlay Apr 07 '22 at 22:47
  • Have to extract the attachment to an external folder location and reference that file for loading to Windows form. – June7 Apr 07 '22 at 22:52
  • You might find this of some help https://stackoverflow.com/questions/63421364/how-to-work-with-access-attachment-field-from-windows-forms – Hursey Apr 07 '22 at 22:52
  • 2
    Does this answer your question? [How to work with Access attachment field from Windows Forms?](https://stackoverflow.com/questions/63421364/how-to-work-with-access-attachment-field-from-windows-forms) – June7 Apr 07 '22 at 22:58
  • Access uses `OleDbType.LongVarBinary` to store byte arrays. Use that as the Column's data type. – Jimi Apr 07 '22 at 23:02

0 Answers0