Hi everyone. I want to ask a question on how to show pictures (jpg files) from access database to a form

I have a database called DataBase, i have some fields there. One is called picture, and the data type is attachment.

and i have the following code

    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim inc As Integer
    Dim MaxRows As Integer  

        sql = "SELECT * FROM Staff"
        da = New OleDb.OleDbDataAdapter(Sql, con)
        da.Fill(ds, "DataBase")

        txtFname.Text = ds.Tables("DataBase").Rows(inc).Item(1)
        txtLastName.Text = ds.Tables("DataBase").Rows(inc).Item(2)
        txtJob.Text = ds.Tables("DataBase").Rows(inc).Item(3)
        picture.? = ds.Tables("DataBase").Rows(inc).Item(4) ??? 'How do i display picutures from my database which is Item(4)??? 

        MaxRows = ds.Tables("DataBase").Rows.Count
        inc = -1

I have a PictureBox named picture. So my question is, How do i display the picture from my access database which is Item(4) following the above pattern of displaying all other data in textbox?

Thank You.

Recommended Answers

All 3 Replies

The PictureBox has a ImageLocation property. You set that to be the URL of the image. Are you storing the URL as text in the db or the image itself?

Hi Hericles, Thanks for your reply.

i saved the images itself in the database.

here is how I would retrieve an image from an Access database. First I would create a function that turned the data from a byte format to a Bitmap format using the MemoryStream Class and pass it a byte array containing my image data

Public Function GetImageFromDB(ByRef imageName As String) As Bitmap
        Dim conn As New OleDb.OleDbConnection
        Dim cmd As OleDb.OleDbCommand
        Dim reader As OleDb.OleDbDataReader

        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\YourDBFile.mdb;User Id=YourUsername;Password=YourPassword;"

        cmd = conn.CreateCommand()
        cmd.CommandText = "SELECT YourColumnName FROM YourTable WHERE ColumnName = '" & imageName & "'"
        reader = cmd.ExecuteReader

        If reader.Read Then
            Dim imgByteArray() As Byte
                imgByteArray = CType(reader(0), Byte())
                Dim stream As New System.IO.MemoryStream(imgByteArray)
                Dim bmp As New Bitmap(stream)
            Catch ex As Exception
                Return Nothing                    
            End Try
        End If

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.