TypeConverter

Posts   
 
    
shekar
User
Posts: 327
Joined: 26-Mar-2010
# Posted on: 24-May-2013 13:39:44   

Reference to my previous thread 21942

LLBLGEN 4.0 Rel May 15th, 2013 .Net Framework 4.0 C# Windows Application SQL Server 2008 LLBLGEN runtime framework self servicing

I am trying to store image in sql server.

Column Image in DB is type Image I am passing Image "Picture" as data type Image. Error: Cannot convert source type System.drawing.image to target type byte[]. Also tried memberimage.Image = Convert.ToByte(Picture); Error: Cannot convert source type byte to target type byte[].

So far, I have created type converter class [code reproduced below], added the dll in LLBLGEN type converter folder. Defined type conversion. Anything else to do ?

But the error memberimage.Image = Picture; //cannot convert source type 'System.Drawing.Image' to target type 'byte[]' still exists


using System;
using System.ComponentModel;
using System.Drawing;


namespace TypeConverterTierMyClubCircle
{
    public class TypeConverterClass : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            switch (sourceType.FullName)
            {
                case "System.Byte[]":
                    return true;
                default:
                    return false;
            }
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            switch (destinationType.FullName)
            {
                case "System.Byte[]":
                    return true;
                default:
                    return false;
            }
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,
                                           object value)
        {
            if (value == null)
            {
                return null;
            }
            var cnv = new ImageConverter();
            return cnv.ConvertFrom(value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,
                                         object value, Type destinationType)
        {
            if (value == null)
            {
                return null;
            }
            var cnv = new ImageConverter();
            return cnv.ConvertTo(value, Type.GetType("System.Byte[]"));
        }

        public override object CreateInstance(ITypeDescriptorContext context,
                                              System.Collections.IDictionary propertyValues)
        {
            return new Bitmap(10, 10);
        }
    }
}

shekar
User
Posts: 327
Joined: 26-Mar-2010
# Posted on: 24-May-2013 18:41:50   

Type conversion screen shot attached.

Attachments
Filename File size Added on Approval
Type Conversion.JPG 76,763 24-May-2013 18:42.10 Approved
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-May-2013 07:51:00   

Although you added the Type conversion definition, maybe it is not applied to the field (on field mappings sub-tab). You need to set "auto assign type converters" to true, and refresh the model, or apply the typeConverter manually. What is the type of the field in the generated code? (it should be Bitmap). Please read this.

David Elizondo | LLBLGen Support Team
shekar
User
Posts: 327
Joined: 26-Mar-2010
# Posted on: 25-May-2013 12:51:40   

daelmo wrote:

Although you added the Type conversion definition, maybe it is not applied to the field (on field mappings sub-tab). You need to set "auto assign type converters" to true, and refresh the model, or apply the typeConverter manually. What is the type of the field in the generated code? (it should be Bitmap). Please read this.

After reading this, I tried setting auto assign type converters to true, refreshing the catalog and rengerating. But the problem remains same.

Generated code in LLBLGEN class is like this


public virtual System.Byte[] Image
        {
            get { return (System.Byte[])GetValue((int)ImagegalleryFieldIndex.Image, true); }
            set { SetValue((int)ImagegalleryFieldIndex.Image, value, true); }
        }

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-May-2013 09:19:21   

The "auto assign type converters" flag works for newly added elements. To make it work, do this:

  1. At Project->Settings, go to "Type Shortcuts" and add a shortcut with name "image" and type "System.Drawing.Bitmap".

  2. Leave your Type Conversion definition as it is.

  3. Set "auto assign type converters" flag to true.

  4. Remove and re-add your entity. Also, the Type Converter will be assigned automatically for any new fields on reverse-engineer or catalog refreshes.

You can also make it manual:

  1. At your LLBLGen project, edit your entity, and select "image" for the field's type.

  2. The TypeConverter will be automatically set. If not, go to Field Mappings, as the types don't match, it will be marked with an error. So for that field mapping, select your type converter.

... then regenerate code. The type of your field at generated code should be System.Drawing.Bitmap.

David Elizondo | LLBLGen Support Team