Solved

Retrieve Image from Access Database

Posted on 2009-04-14
3
798 Views
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?
Thanks
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)
 
 
            Try
                dc.Open()
                Dim dr As OleDbDataReader = cmd.ExecuteReader()
                If dr.HasRows Then
                    dr.Read()
 
                    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
                        Try
                            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
                dr.Close()
            Catch ex As Exception
 
                MessageBox.Show(ex.Message)
                Throw ex
 
            Finally
                dc.Close()
 
            End Try
        End If
 
    End Sub

Open in new window

0
Comment
Question by:Sheritlw
  • 2
3 Comments
 
LVL 9

Expert Comment

by:Deathrace
ID: 24135950
Hi,
Here are few articles...which solves your problem...
http://www.mvps.org/access/forms/frm0030.htm
http://www.mvps.org/access/forms/frm0044.htm

0
 

Author Comment

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

Accepted Solution

by:
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

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


            Try
                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)
                FSImage.Close()
                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)
                    cmd.Parameters.Add(Prm)
                    oleCnn.Open()
                    i = cmd.ExecuteNonQuery()
                    oleCnn.Close()
                End Using

                Return i

            Catch ex As Exception

                MessageBox.Show(ex.Message)
                Throw ex

            Finally
                If oleCnn.State = ConnectionState.Open Then
                    oleCnn.Close()
                End If
            End Try
        End If
    End Function
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Duplicating a tab page content to another in TabControl control 11 44
Get Client IP on RDS - VB.NET 15 47
vb.net help 4 31
Modal Popup Extender control 1 20
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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