none
OLE objects (Pictures) from Access DB and Visual Studio 2010

    Question

  • Hello all,

     I have a class assignment and I am having some difficulty with an Access 2010 database and OLE objects in the database. I have the database imported into the project and I am able to view the records from the database that I have in labels and combo boxes.

     I used the database DataSoure wizard to create my connection string and import the Access database. In the DataSouce window I set the DataSet for the "Flag" column that contains my pictures to use a picture box object type and added the data bound object to my form.

     When I run the application all the field from the database show as expected however, the picture box object does not display the images from the database. When I check the picture box object in the designer the properties window shows that the image source is bound to my database with the little database icon and the data bindings are all correct.

    I need some help to explain why the pictures are not showing up in the picture box when I run the application or move through the records. You can download my project file from class below, and any help is greatly appreciated. 

    http://cid-1d8956059722366b.skydrive.live.com/redir.aspx?resid=1D8956059722366B!1337

    Thanks a bunch!

    Richard Ciampa

    • Edited by Richard Ciampa Monday, May 16, 2011 11:12 PM Added hyperlink
    Monday, May 16, 2011 11:05 PM

Answers

  • When you store a file as an OLE Object, Access inserts OLE headers into the data that identifies the app it's associated/stored with. This will prevent you from directly binding to the OLE Object field without using a control or code that understands how to read the OLE header. There is no such control in Visual Studio 2010.

    For some files you can use code to strip the OLE header and then read the file or blob data in it's native format, however, you need to know what type of OLE Object you are working with since the headers differ from app to app.


    Paul ~~~~ Microsoft MVP (Visual Basic)
    Tuesday, May 17, 2011 6:09 PM
  • Hi Richard,

    Welcome to the MSDN Forum.

    I totally agree with Paul's point. When you store a file in the OLE object field, it will show you "Package", As Paul said, it contains the header information.

    Because you want to use a picturebox control traverses all the images, so try to insert the image into database by code, that there will be no header information.

    So I add a new form named "ImportImage" in your project to update your flag field. The following code is the update function:

        Dim openf As New OpenFileDialog
        openf.ShowDialog()
        Dim picName As String = openf.FileName
    
        Dim cn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=""datasource")
        cn.Open()
        Dim bitP() As Byte = System.IO.File.ReadAllBytes(picName)
        Dim Command As New OleDbCommand("update [Population Data] set flag = @img where id = @id", cn)
        Command.Parameters.Add("@img", OleDbType.Binary)
        Command.Parameters("@img").Value = bitP
        Command.Parameters.Add("@id", OleDbType.Integer)
        Command.Parameters("@id").Value = ComboBox1.SelectedValue
    
        Try
          Command.ExecuteNonQuery()
        Catch ex As Exception
        Finally
          cn.Close()
        End Try
        MsgBox("update OK")
    

    Here is the new project: http://cid-2dd4e8a49d9bb418.office.live.com/self.aspx/.Documents/Population%20Analysis.rar

    I hope this will be helpful.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 19, 2011 1:56 AM

All replies

  • When you store a file as an OLE Object, Access inserts OLE headers into the data that identifies the app it's associated/stored with. This will prevent you from directly binding to the OLE Object field without using a control or code that understands how to read the OLE header. There is no such control in Visual Studio 2010.

    For some files you can use code to strip the OLE header and then read the file or blob data in it's native format, however, you need to know what type of OLE Object you are working with since the headers differ from app to app.


    Paul ~~~~ Microsoft MVP (Visual Basic)
    Tuesday, May 17, 2011 6:09 PM
  • When you store a file as an OLE Object, Access inserts OLE headers into the data that identifies the app it's associated/stored with. This will prevent you from directly binding to the OLE Object field without using a control or code that understands how to read the OLE header. There is no such control in Visual Studio 2010.

    For some files you can use code to strip the OLE header and then read the file or blob data in it's native format, however, you need to know what type of OLE Object you are working with since the headers differ from app to app.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    how would you strip the OLE header for image files?
    thanks for any help
    Thursday, May 19, 2011 1:05 AM
  • Hi Richard,

    Welcome to the MSDN Forum.

    I totally agree with Paul's point. When you store a file in the OLE object field, it will show you "Package", As Paul said, it contains the header information.

    Because you want to use a picturebox control traverses all the images, so try to insert the image into database by code, that there will be no header information.

    So I add a new form named "ImportImage" in your project to update your flag field. The following code is the update function:

        Dim openf As New OpenFileDialog
        openf.ShowDialog()
        Dim picName As String = openf.FileName
    
        Dim cn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=""datasource")
        cn.Open()
        Dim bitP() As Byte = System.IO.File.ReadAllBytes(picName)
        Dim Command As New OleDbCommand("update [Population Data] set flag = @img where id = @id", cn)
        Command.Parameters.Add("@img", OleDbType.Binary)
        Command.Parameters("@img").Value = bitP
        Command.Parameters.Add("@id", OleDbType.Integer)
        Command.Parameters("@id").Value = ComboBox1.SelectedValue
    
        Try
          Command.ExecuteNonQuery()
        Catch ex As Exception
        Finally
          cn.Close()
        End Try
        MsgBox("update OK")
    

    Here is the new project: http://cid-2dd4e8a49d9bb418.office.live.com/self.aspx/.Documents/Population%20Analysis.rar

    I hope this will be helpful.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 19, 2011 1:56 AM
  • That would depend upon the type of image and the application that was used to store it.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Thursday, May 19, 2011 2:03 AM
  • Paul,

     I understand what you mean. It just seems odd that that type of functionality is not built-in to VS 2010. The CIS class (intro to VS 2010) I am taking does not even require me to take the application to the level I did (however trivial that level is), I just thought I would exercise my skill set a bit, but it seems to be outside my current knowledge base.

    Thanks for the help it is much appreciated.

    Thursday, May 19, 2011 5:22 AM
  • Mike,

     Wow, that's awesome! As I was telling Paul the class I'm taking is an Intro to VS 2010. Some of the code you gave is beyond me at this point it will take me some time to digest it. The assignment for class is already been turned in just so you know that you aren’t doing my work for me (LOL), besides the instructor would know it from the difference in the rest of my code.

     I can’t thank you enough.

    Thursday, May 19, 2011 5:49 AM
  • Just a little bit of history, OLE (a byproduct of COM)  was one of key technologies of the 90s. Access initially introduced the concept of storing OLE objects in a database during this time. Classic Visual Basic (versions 6.0 and prior) supported this technology with an OLE Object control, that would enable developers to directly access these objects from the database and display or activate them in their associated applications from a VB or Access Form. This was the application hosting feature of document Object Linking and Embedding that we still see in applications such as Word, Excel, Access and other apps that still support it.

    When Microsoft developed Visual Basic .NET (2002) they chose not to carry the OLE Object database binding technology forward, in addition to other features from the prior version. Although COM/OLE technology is still important technology, and supported via interop in .NET, it has been de-emphasized in the managed code environment.

    As Mike mentioned it is preferable to store BLOB data as binary streams, without the OLE information, and then read and write it back out to either a file or PictureBox control in its native format. This method works with any database capable of storing BLOB data and avoids the proprietary method built into Microsoft Access.


    Paul ~~~~ Microsoft MVP (Visual Basic)
    Thursday, May 19, 2011 4:22 PM