Retrieve Image from Access Database

Posted on 2009-04-14
Last Modified: 2012-05-06
I have a form where users can import an image of their vehicle. This image gets placed as a panels background.
I then save the image to an Ole Object field in an Access Table.
I have everything working except the retrieving of the image.  I can see in the database that it is stored as a long binary data.
I have added the whole procedure I use to retrieve this data.
What am I doing wrong?
Public Sub FillCars(Optional ByVal Pan As Panel = Nothing)

        Dim ds As New DataSet

        If DatabaseToUse() = BizObjs.DBType.Access Then

            'Access Database

            Dim dc As New OleDb.OleDbConnection(ConnectionString)

            Dim cmd As New OleDb.OleDbCommand(CreateSQLSelect, dc)



                Dim dr As OleDbDataReader = cmd.ExecuteReader()

                If dr.HasRows Then


                    CarID = dr("CarID")

                    CarNumb = Field2Str(dr("CarNumb"))

                    CarName = Field2Str(dr("CarName"))

                    CarColor = Field2Integer(dr("CarColor"))

                    ChassisID = Field2Integer(dr("ChassisID"))

                    CompetitionClassID = Field2Integer(dr("CompetitionClassID"))

                    EngineID = Field2Integer(dr("EngineID"))

                    CarJunctionID = Field2Integer(dr("CarJunctionID"))

                    CarColorText = Field2Str(dr("CarColorText"))

                    CarClassID = Field2Integer(dr("CarClassID"))

                    CarPicPath = Field2Str(dr("CarPicPath"))

##### I get the error on this next statement ##############

                    Dim cImage() As Byte = dr("CarPic")

                    If Not cImage Is Nothing Then


                            Dim MSImage As New MemoryStream(cImage)

                            'This is the Picture Box in which we will display the image

                            Pan.BackgroundImage = Image.FromStream(MSImage)

                        Catch ex As Exception

                            Exit Try

                        End Try

                    End If

                End If


            Catch ex As Exception


                Throw ex



            End Try

        End If

    End Sub

Open in new window

Question by:Sheritlw
  • 2

Expert Comment

ID: 24135950
Here are few articles...which solves your problem...


Author Comment

ID: 24139958
I have read a lot of articles like you posted.  I need to know what is wrong with my code.

Accepted Solution

Sheritlw earned 0 total points
ID: 24141403
I found the solution.
Here's the code if anyone else is looking for it.
 Public Function UpdatePicture() As Integer
        Dim i As Integer
        Dim str As String

        If msCarPicPath = "" Then

            i = RemovePicture()
            Return i

            str = "Update Cars Set CarPic = @ImageData where " _
                & " carid = " & miCarID & ""

                Dim FSImage As New FileStream(msCarPicPath, FileMode.Open, FileAccess.Read)
                'Store it in Bytes
                Dim BTImage(FSImage.Length() - 1) As Byte
                FSImage.Read(BTImage, 0, BTImage.Length)
                Dim Prm As New OleDbParameter("@ImageData", OleDbType.VarBinary, BTImage.Length, ParameterDirection.Input, False, 0, 0, Nothing, DataRowVersion.Current, BTImage)

                Using cmd As New OleDbCommand(str, oleCnn)
                    i = cmd.ExecuteNonQuery()
                End Using

                Return i

            Catch ex As Exception

                Throw ex

                If oleCnn.State = ConnectionState.Open Then
                End If
            End Try
        End If
    End Function

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Make a border less form movable 2 23
Showdialog 8 29
Want to create a userform that looks like the image provided 10 46
Allow user to hide and show datagridview columns 4 17
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now