Binding on Image field

Posts   
 
    
KelMan
User
Posts: 11
Joined: 25-Oct-2006
# Posted on: 25-Oct-2006 23:22:57   

Hi,

I’m new to LLBLGen Pro, currently evaluating it. I’m using the October 17th Final 2.0.0.0 version on .NET 2.0 Framework and SQL Server 2005 Database. I’m trying to databind an entity collection to a DataGridView via a BindingSource. One of the fields is an image, System.Byte array .NET data type mapped in a varabinary(max) column. The code is pretty simple:

        Dim adapter As New DataAccessAdapter()

        Dim chapter As New ChapterEntity(chapterEx.ChapterId)
        adapter.FetchEntity(chapter)

        adapter.FetchEntityCollection(chapter.ClusterCollectionViaChapterCode, chapter.GetRelationInfoClusterCollectionViaChapterCode)

        ClustersBindingSource.DataSource = chapter.ClusterCollectionViaChapterCode

The problem is that as soon as I set the DataSource property, I get exceptions from the DataGridView while trying to bind each image. By handling the DataError event I captured the following Message of the exceptions:

Parameter is not valid.
   at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
   at System.Drawing.ImageConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at System.Windows.Forms.Formatter.FormatObjectInternal(Object value, Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, String formatString, IFormatProvider formatInfo, Object formattedNullValue)
   at System.Windows.Forms.Formatter.FormatObject(Object value, Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, String formatString, IFormatProvider formatInfo, Object formattedNullValue, Object dataSourceNullValue)
   at System.Windows.Forms.DataGridViewCell.GetFormattedValue(Object value, Int32 rowIndex, DataGridViewCellStyle& cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)

So, do I need to provide a custom implementation of a TypeConverter for the image field or am I doing something wrong?

Thanx, Manos

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 26-Oct-2006 09:44:37   

Do you use auto generate columns or do you define them manually "DataGridView.Columns.Add(...)"?

KelMan
User
Posts: 11
Joined: 25-Oct-2006
# Posted on: 26-Oct-2006 10:40:04   

I'm using AutoGenerated columns. During the design time, I drag the LLBLGen components from the ToolBox and bind them on the BindingSource and then I bind the BindingSource on the DataGridView. Thus, the DataGridView autogenerates the appropriate fields. During runtime, I rebind the DataSource of the BindingSource, as I posted earlier.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 26-Oct-2006 11:52:50   

I can reproduce this, but I'm not sure where the error is, probably in the datagridview.

the thing is this: I bind a collection of northwind employees to a datagridview. These entities have a photo property (which is an image). When I scroll the rows to the left so the image column is visible, I get the error. What's strange is that the FIRST row's field for Photo is empty, however the rest is visible!. When I scroll the row a little further (I can see 10% or so per attempt) I again get the error, and see another 10% of the photo's, etc.

So I thought: what causes this problem? I have actually 2 rows with the error: the first (employee 1) and the last (the new row).

Checking in the DB, I see what's the problem: the photo field for employee 1 is NULL. A NULL in a blob field results in an EMPTY byte[] array. So not DBNull.Value, but simply no data.

This 'default value' is produced by the TypeDefaultValue class in HelperClasses in the dbgeneric project. The class is generated using an include template you could change if you want to (typeDefaultValueInclude.template)

When I comment out the line for byte[], I'll get null back, and then the grid will show without the error but with the errorous images.

Frans Bouma | Lead developer LLBLGen Pro
KelMan
User
Posts: 11
Joined: 25-Oct-2006
# Posted on: 26-Oct-2006 23:48:41   

Otis wrote:

This 'default value' is produced by the TypeDefaultValue class in HelperClasses in the dbgeneric project. The class is generated using an include template you could change if you want to (typeDefaultValueInclude.template)

OK, it seems that the problem was really the null values. I changed the

Case "System.Byte[]"
    valueToReturn = New Byte(-1) {}

to

Case "System.Byte[]"
    valueToReturn = New Byte(0) {0}

and it works now (it just shows the white page with the red X mark).

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 27-Oct-2006 10:37:58   

Yes, that's actually creating a 1byte array now, instead of an empty array simple_smile . Still odd this .net byte[] to image typeconverter fails with an empty array but succeeds with a 1-byte array...

Frans Bouma | Lead developer LLBLGen Pro