StrataFrame Forum
Back
Login
Login
Home
»
StrataFrame Application Framework - V1
»
Business Objects and Data Access (How do I?)
»
Error using Custom Field Property for PictureBox
Error using Custom Field Property for PictureBox
Post Reply
Like
1
Error using Custom Field Property for PictureBox
View
Flat Ascending
Flat Descending
Threaded
Options
Subscribe to topic
Print This Topic
RSS Feed
Goto Topics Forum
Author
Message
Chris Diesel
Chris Diesel
posted 7 Years Ago
ANSWER
Topic Details
Share Topic
Group: StrataFrame Users
Posts: 74,
Visits: 300
I get this error I believe during binding of a Custom Field Property to your PictureBox control. If I break execution right before the field value is returned, it looks correct.
Error:
An error occurred while refreshing the data from field 'IwbProduct.PrimaryImage' to property 'Image' on control '.'
Inner Exception:
Cannot format the value to the desired type.
#region Custom Field Properties
[Description("Primary Image"), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public byte[] PrimaryImage
{
get
{
byte[] primaryImage = new byte[0];
if (this.Count > 0 && this.CurrentRow != null)
{
using (IwbProductImages iwbProductImages = new IwbProductImages())
{
primaryImage = iwbProductImages.GetPrimaryImage(this.Guid);
}
}
return primaryImage;
}
}
#endregion
/// <summary>
/// Provider property descriptors for the custom bindable fields
/// </summary>
protected override MicroFour.StrataFrame.Business.FieldPropertyDescriptor[] GetCustomBindablePropertyDescriptors()
{
//-- Return the array of property descriptors
return new MicroFour.StrataFrame.Business.FieldPropertyDescriptor[] { new ReflectionPropertyDescriptor("PrimaryImage", typeof(IwbProduct)) };
}
Thanks,
Chris
Tags
CFP PictureBox Binding
Reply
Like
1
StrataFrame Team
S
StrataFrame Team
posted 7 Years Ago
ANSWER
Post Details
Share Post
S
Group: StrataFrame Developers
Posts: 3K,
Visits: 2.5K
The Image property on the PictureBox is a System.Drawing.Bitmap, not a byte[]. You'll need to change the property to return a System.Drawing.Bitmap.
You'll then need to convert your byte[] to a Bitmap using by wrapping it in a MemoryStream.
public System.Drawing.Bitmap PrimaryImage
{
get
{
if (this.Count > 0 && this.CurrentRow != null)
{
using (IwbProductImages iwbProductImages = new IwbProductImages())
{
using(MemoryStream ms = new MemoryStream(
iwbProductImages.GetPrimaryImage(this.Guid)
))
{
return new System.Drawing.Bitmap(ms);
}
}
}
return null;
}
}
Reply
Like
2
Chris Diesel
Chris Diesel
posted 7 Years Ago
ANSWER
Post Details
Share Post
Group: StrataFrame Users
Posts: 74,
Visits: 300
Thank you so much!!!!
Reply
Like
2
Chris Diesel
Chris Diesel
posted 7 Years Ago
ANSWER
Post Details
Share Post
Group: StrataFrame Users
Posts: 74,
Visits: 300
That worked great when there is an image to display. Having problems dealing with no image though. I get the same error listed above when it returns null. Any ideas on that?
Thanks!!!
Reply
Like
1
StrataFrame Team
S
StrataFrame Team
posted 7 Years Ago
ANSWER
Post Details
Share Post
S
Group: StrataFrame Developers
Posts: 3K,
Visits: 2.5K
Any time the property get throws an error during binding, it wraps it in the binding exception that you're getting.
You'll need to store off the data into a byte[] and check it's length to make sure there's actually data. While you're at it, you might as well catch any exceptions and return a placeholder if you encounter one.
public System.Drawing.Bitmap PrimaryImage
{
get
{
if (this.Count > 0 && this.CurrentRow != null)
{
try
{
using (IwbProductImages iwbProductImages = new IwbProductImages())
{
byte[] imageData = iwbProductImages.GetPrimaryImage(this.Guid);
if ((imageData?.Length ?? 0) > 0)
{
using(MemoryStream ms = new MemoryStream(imageData))
{
return new System.Drawing.Bitmap(ms);
}
}
else
{
//-- There is no image, so return either nothing or a placeholder
return null;
//return My.Resources.NoImagePlaceholder;
}
}
catch
{
//-- There was some error loading the image, so return either nothing or a placeholder
return My.Resources.ErrorImagePlaceholder;
//return null;
}
}
//-- No current row in the BO
return null;
}
}
Reply
Like
2
GO
Merge Selected
Merge into selected topic...
Merge into merge target...
Merge into a specific topic ID...
Open Merge
Post Reply
Like
1
Similar Topics
Post Quoted Reply
Reading This Topic
Login
Login
Remember Me
Reset Password
Resend Validation Email
Login
Explore
Messages
Mentions
Search