Home
Help
Register
Log in

Search

 
   Active Threads  

You are here: Home > LLBLGen Pro > LLBLGen Pro Runtime Framework> Binding a PictureBox to an Access OLEObject database field
 

Pages: 1
LLBLGen Pro Runtime Framework
Binding a PictureBox to an Access OLEObject database field
Page:1/1 

  Print all messages in this thread  
Poster Message
like2175
User



Location:
Camberley, United Kingdom
Joined on:
27-Mar-2006 16:16:06
Posted:
83 posts
# Posted on: 04-Nov-2009 11:38:35.  
I've been messing with this for hours, so I thought I'd ask the experts.

I have a pictureBox bound to an OLE Object database field. I can update and display images correctly, they get read from and written to the database. However I have 2 issues:

1. As I scroll through my records If there is no Image stored in the database then the pictureBox continues to show the image from the previous record unless I clear it manually with:

Code:
    
If currPerson.Photo.Length = 0 Then
        pbPreviewArea.Image = Nothing
End If


This works... but is there a better solution?

2. If I want to remove the Image from the database I do:

Code:

     pbPreviewArea.Image = Nothing


but this doesn't trigger an update to database. Stepping through the code I can see that {collection}.ContainsDirtyContents = False after I have set "pbPreviewArea.Image = Nothing".

Any helpful hints greatfully received.
VB / Winforms/ VS2008 / Access 2003 / LLBLGenPro 2.6
  Top
like2175
User



Location:
Camberley, United Kingdom
Joined on:
27-Mar-2006 16:16:06
Posted:
83 posts
# Posted on: 04-Nov-2009 11:41:30.  
like2175 wrote:
I've been messing with this for hours, so I thought I'd ask the experts.

I have a pictureBox bound to an OLE Object database field. I can update and display images correctly, they get read from and written to the database. However I have 2 issues:

1. As I scroll through my records If there is no Image stored in the database then the pictureBox continues to show the image from the previous record unless I clear it manually with:

Code:
    
If currPerson.Photo.Length = 0 Then
        pbPreviewArea.Image = Nothing
End If


This works... but is there a better solution?

2. If I want to remove the Image from the database I do:

Code:

     pbPreviewArea.Image = Nothing


but this doesn't trigger an update to database. Stepping through the code I can see that {collection}.ContainsDirtyContents = False after I have set "pbPreviewArea.Image = Nothing".

My Picture box is declared as follows:
Code:

'pbPreviewArea
    '
    Me.pbPreviewArea.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.pbPreviewArea.BackColor = System.Drawing.Color.Yellow
    Me.pbPreviewArea.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.pbPreviewArea.DataBindings.Add(New System.Windows.Forms.Binding("Image", Me.bsPersons, "Photo", True, System.Windows.Forms.DataSourceUpdateMode.OnValidation, """"""))
    Me.pbPreviewArea.ErrorImage = CType(resources.GetObject("pbPreviewArea.ErrorImage"), System.Drawing.Image)
    Me.pbPreviewArea.InitialImage = CType(resources.GetObject("pbPreviewArea.InitialImage"), System.Drawing.Image)
    Me.pbPreviewArea.Location = New System.Drawing.Point(217, 177)
    Me.pbPreviewArea.Name = "pbPreviewArea"
    Me.pbPreviewArea.Size = New System.Drawing.Size(45, 39)
    Me.pbPreviewArea.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
    Me.pbPreviewArea.TabIndex = 24
    Me.pbPreviewArea.TabStop = False


Any helpful hints greatfully received.
VB / Winforms/ VS2008 / Access 2003 / LLBLGenPro 2.6



  Top
Walaa
Support Team



Location:

Joined on:
21-Aug-2005 16:03:48
Posted:
14569 posts
# Posted on: 04-Nov-2009 11:48:45.  
Quote:
pbPreviewArea.Image = Nothing

Is pbPreviewArea an entity?
Is Image an entity?
  Top
like2175
User



Location:
Camberley, United Kingdom
Joined on:
27-Mar-2006 16:16:06
Posted:
83 posts
# Posted on: 04-Nov-2009 12:00:00.  
Walaa wrote:
Quote:
pbPreviewArea.Image = Nothing

Is pbPreviewArea an entity?
Is Image an entity?


pbPreviewArea is the PictureBox control.
Image is used to get or set the image
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image.aspx


  Top
Walaa
Support Team



Location:

Joined on:
21-Aug-2005 16:03:48
Posted:
14569 posts
# Posted on: 04-Nov-2009 15:23:45.  
Would you please post the binding code of pbPreviewArea.Image.
  Top
like2175
User



Location:
Camberley, United Kingdom
Joined on:
27-Mar-2006 16:16:06
Posted:
83 posts
# Posted on: 04-Nov-2009 15:32:42.  
Walaa wrote:
Would you please post the binding code of pbPreviewArea.Image.


In the .Designer.vb I have:

Code:

Me.pbPreviewArea.DataBindings.Add(New System.Windows.Forms.Binding("Image", Me.bsPersons, "Photo", True, System.Windows.Forms.DataSourceUpdateMode.OnValidation, """"""))



I've attached the IDE from VS2008 showing the PictureBox in place. I have bound it through '(DataBindings) - Image' on the Properties pane shown.




  Top
Walaa
Support Team



Location:

Joined on:
21-Aug-2005 16:03:48
Posted:
14569 posts
# Posted on: 04-Nov-2009 17:08:35.  
Please change the update mode from OnValidation to OnPropertyChanged.
Quote:
Me.pbPreviewArea.DataBindings.Add(new System.Windows.Forms.Binding("Image", Me.bsPersons, "Photo", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))
  Top
like2175
User



Location:
Camberley, United Kingdom
Joined on:
27-Mar-2006 16:16:06
Posted:
83 posts
# Posted on: 10-Nov-2009 15:01:16.  
Walaa wrote:
Please change the update mode from OnValidation to OnPropertyChanged.
Quote:
Me.pbPreviewArea.DataBindings.Add(new System.Windows.Forms.Binding("Image", Me.bsPersons, "Photo", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))


I'm afraid that hasn't helped.


  Top
MTrinder
User



Location:
London by day, Milton Keynes by night.
Joined on:
08-Oct-2008 17:55:47
Posted:
1461 posts
# Posted on: 10-Nov-2009 22:23:11.  
Do you get the same behaviour if you bind directly to a datatable rather than an LLBLGen entity ?

Just trying to determing if the issue is with our code...

Matt
  Top
like2175
User



Location:
Camberley, United Kingdom
Joined on:
27-Mar-2006 16:16:06
Posted:
83 posts
# Posted on: 11-Nov-2009 12:26:16.  
MTrinder wrote:
Do you get the same behaviour if you bind directly to a datatable rather than an LLBLGen entity ?

Just trying to determing if the issue is with our code...

Matt


Binding to a DataSet rather than an LLBLGenCollection it works.


  Top
Otis
LLBLGen Pro Team



Location:
The Hague, The Netherlands
Joined on:
17-Aug-2003 18:00:36
Posted:
37803 posts
# Posted on: 11-Nov-2009 13:36:30.  
in the datatable, if there's no value, it's likely the value is DBNull.Value, while in the entity the image field is returning Nothing (null).

I think this is your problem, not sure if a picture box can help with this in the sense that it can trap these situations and let you display a different picture or clear it ?
Frans Bouma
LLBLGen Pro / ORM Profiler Lead Developer | Blog | Twitter
 
Top
like2175
User



Location:
Camberley, United Kingdom
Joined on:
27-Mar-2006 16:16:06
Posted:
83 posts
# Posted on: 11-Nov-2009 16:47:34.  
Otis wrote:
in the datatable, if there's no value, it's likely the value is DBNull.Value, while in the entity the image field is returning Nothing (null).

I think this is your problem, not sure if a picture box can help with this in the sense that it can trap these situations and let you display a different picture or clear it ?



The application creates visitors badges which I construct in a PrintPreviewControl. I've just realised that the bound PictureBox is redundant so I've removed it! I have a dialog that now returns a byte array, ready to populate the .Photo attribute, which maps to the OLE Object field in the Access database.

I include the following code extracts for others' reference.

Code:


Private Sub btnImportPhoto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportPhoto.Click

    Dim importImageForm As New ImportImage(ImportImage.ImageFromByteArray(CType(bsPersons.Current, PersonEntity).Photo))

    Try
     importImageForm.ShowDialog()
     If importImageForm.DialogResult = Windows.Forms.DialogResult.OK Then
        'pbPreviewArea.Image = importImageForm.ImportedBitmap
        CType(bsPersons.Current, PersonEntity).Photo = importImageForm.ImportedImageAsByteArray
        PrintPreviewControl1.InvalidatePreview()
     End If

    Catch ex As Exception
     HandleError(ex)
    End Try

End Sub


'''<summary>
''' Get Image from a Byte Array (the format it is stored in database) to Image. If Byte Array is zero length return nothing.
'''</summary>
Public Shared Function ImageFromByteArray(ByVal imageAsByteArray As System.Byte()) As Image
    If imageAsByteArray.Length <> 0 Then
     Return Image.FromStream(New System.IO.MemoryStream(imageAsByteArray))
    Else
     Return Nothing
    End If
End Function


'''<summary>
''' Convert ByteArray (the format that images are stored in the database) to an Image
'''</summary>
''' Convert Image to ByteArray for storing in BLOB.
Public Shared Function ByteArrayFromImage(ByVal img As Image) As Byte()
    If Not img Is Nothing Then
     Dim ms As New System.IO.MemoryStream
     img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
     Return ms.ToArray
    Else
     Return Nothing
    End If

End Function


Thanks to everyone for their suggestions.

Graham


  Top
Pages: 1  


Powered by HnD ©2002-2007 Solutions Design
HnD uses LLBLGen Pro

Version: 2.1.12172008 Final.