Results 1 to 6 of 6

Thread: VB 2017 - Retrieving image path into picture box on form from database

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Location
    South Carolina
    Posts
    3

    VB 2017 - Retrieving image path into picture box on form from database

    Hi everyone. I'm new to the forums. I am a programming/computer tech major at a technical college.

    Anyways, is there a possible way I can:
    - retrieve a photo from an Access database field as "Long Text" using an image file path so that it displays the designated image onto a picture box AS the form loads and I navigate each record? I am not using any OleDBAdapter codes in my program (but have Option Infer Off/Strict On/Explicit On and System.IO); I'm simply using a TableAdapter by connecting my program to the database.

    Any help's greatly appreciated.
    - CV

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: VB 2017 - Retrieving image path into picture box on form from database

    You may well be unaware but a table adapter is actually a wrapper for a data adapter so you really are using an OleDbDataAdapter. The table adapter has Fill and Update methods to retrieve and save data and these are mapped onto the Fill and Update methods of that internal data adapter.

    Your table adapter is used to populate a DataTable inside a DataSet via its Fill method. As you have an image file path stored in the database, you can populate a PictureBox via its ImageLocation property in exactly the same way as you would populate a TextBox via its Text property. That can be done directly, by setting the property in code, or via data-binding, which can be set up in the designer or in code.

    How are you loading data at the moment? Did you drag a table from the Data Sources window onto the form and have it generate controls for you? If so, a TextBox was probably generated for that column because it contains text. You can simply add a PictureBox to the form and bind its ImageLocation property in the same way as the Text property of the TextBox is bound. To see and edit that, select a control, open the Properties window, expand the (DataBindings) node, select the (Advanced) node and click the Browse (...) button. Once you've got the PictureBox in place, simply delete the TextBox.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Location
    South Carolina
    Posts
    3

    Re: VB 2017 - Retrieving image path into picture box on form from database

    Quote Originally Posted by jmcilhinney View Post
    Your table adapter is used to populate a DataTable inside a DataSet via its Fill method. As you have an image file path stored in the database, you can populate a PictureBox via its ImageLocation property in exactly the same way as you would populate a TextBox via its Text property. That can be done directly, by setting the property in code, or via data-binding, which can be set up in the designer or in code.

    How are you loading data at the moment? Did you drag a table from the Data Sources window onto the form and have it generate controls for you? If so, a TextBox was probably generated for that column because it contains text. You can simply add a PictureBox to the form and bind its ImageLocation property in the same way as the Text property of the TextBox is bound. To see and edit that, select a control, open the Properties window, expand the (DataBindings) node, select the (Advanced) node and click the Browse (...) button. Once you've got the PictureBox in place, simply delete the TextBox.
    Wow... That saved me a bunch of headaches on its own. As I add a new record with an image onto my program using a Browse button, the image is saved onto the Pic field as a path and the picture box displays that particular image. Thank you so much, man.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Location
    South Carolina
    Posts
    3

    Re: VB 2017 - Retrieving image path into picture box on form from database

    But I found one more problem... on any record I select which has an image path, whenever I update a record and close the program, the record won't save to the physical database, even with the 'Copy to Output Directory' tab set to "Copy if newer". It will save records that don't have image paths though. Any idea of what's going on?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: VB 2017 - Retrieving image path into picture box on form from database

    You're doing something wrong. I don't know what you're doing wrong because I don't know what you're doing. How are you updating a local DataRow? Have you checked the contents of your DataTable just before saving to ensure that it contains what you think it contains? You generally don't fix code issues just by reading the code. You need to execute the code and watch it in action, i.e. debug it. If you don't know how to do that properly, now would be the time to learn.

    https://msdn.microsoft.com/en-us/library/y740d9d3.aspx

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: VB 2017 - Retrieving image path into picture box on form from database

    The way data-binding works is that there is an object between the data source and the control that listens to each side for changes in data and pushes the changed data when such a change is detected. Listening for changes means handling events that are raised when bound properties are changed, e.g. if you bind to the Text property of a TextBox, the Binding object handles the TextChanged event and, when it's raised, pushes the value of the Text property over to the data source.

    In the case of the ImageLocation property of a PictureBox, I don't think that there is any event raised when the property value changes. That would mean that the Binding is never notified of the change and thus never pushes the new value back to the data source. You'll see that if you debug and check what the bound DataRow contains after updating the image. There are two possible solutions:

    1. You can push the data manually from the control to the data source. This could take two forms:

    1A. You can actually get the ImageLocation property of the PictureBox and assign the value to the appropriate column of the bound DataRowView or DataRow.
    1B. You can get the Binding object (probably by name from the DataBindings collection of the control) and call its WriteValue method.

    2. You can create your own custom control and add an ImageLocationChanged event. That would mean shadowing the ImageLocation property and raising an event of your own creation.

    I'd go with option 2 if this is something that you need to do in multiple applications or multiple places in this application. It might look like this:
    vb.net Code:
    1. Public Class PictureBoxEx
    2.     Inherits PictureBox
    3.  
    4.     Public Shadows Property ImageLocation As String
    5.         Get
    6.             Return MyBase.ImageLocation
    7.         End Get
    8.         Set
    9.             Dim oldValue = MyBase.ImageLocation
    10.  
    11.             MyBase.ImageLocation = Value
    12.  
    13.             If Value <> oldValue Then
    14.                 OnImageLocationChanged(EventArgs.Empty)
    15.             End If
    16.         End Set
    17.     End Property
    18.  
    19.     Public Event ImageLocationChanged As EventHandler
    20.  
    21.     Protected Overridable Sub OnImageLocationChanged(e As EventArgs)
    22.         RaiseEvent ImageLocationChanged(Me, e)
    23.     End Sub
    24.  
    25. End Class
    I'd go with option 1B if this was a one-off. It might look like this:
    vb.net Code:
    1. Private Sub SetImageLocation(filePath As String)
    2.     PictureBox1.ImageLocation = filePath
    3.  
    4.     Dim binding = PictureBox1.DataBindings(NameOf(PictureBox1.ImageLocation))
    5.  
    6.     binding.WriteValue()
    7. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width