ImageConverter cannot convert from System.DBNull.

Posts   
 
    
shekar
User
Posts: 327
Joined: 26-Mar-2010
# Posted on: 03-Jul-2013 18:13:17   

LLBLGEN 4.0 .Net Framework 4 VB.NET Winforms application LLBLGen Runtime framework SQL Server 2008

Hi, I have an image entity where I am storing the image files and I am using type converter as below.

PROBLEM: I have a Member entity which has data and this had left join on image entity which has no data. I have created typed list consisting of these two entities and fetching it. Since there is no data in image entity [Member entity HAS DATA] I get an error ImageConverter cannot convert from System.DBNull. How do i over come this problem ?


Imports System.ComponentModel
Imports System.Drawing

Public Class TypeConverterClass
    Inherits TypeConverter

    Public Overrides Function CanConvertFrom(context As ITypeDescriptorContext, sourceType As Type) As Boolean
        Select Case sourceType.FullName
            Case "System.Byte[]"
                Return True
            Case Else
                Return False
        End Select
    End Function

    Public Overrides Function CanConvertTo(context As ITypeDescriptorContext, destinationType As Type) As Boolean
        Select Case destinationType.FullName
            Case "System.Byte[]"
                Return True
            Case Else
                Return False
        End Select
    End Function

    Public Overrides Function ConvertFrom(context As ITypeDescriptorContext, culture As Globalization.CultureInfo,
                                          value As Object) As Object
        If value Is Nothing Then
            Return Nothing
        End If
        Dim cnv = New ImageConverter()
        Return cnv.ConvertFrom(value)
    End Function

    Public Overrides Function ConvertTo(context As ITypeDescriptorContext, culture As Globalization.CultureInfo,
                                        value As Object, destinationType As Type) As Object
        If value Is Nothing Then
            Return Nothing
        End If
        Dim cnv = New ImageConverter()
        Return cnv.ConvertTo(value, Type.[GetType]("System.Byte[]"))
    End Function

    Public Overrides Function CreateInstance(context As ITypeDescriptorContext, propertyValues As IDictionary) As Object
        Return New Bitmap(10, 10)
    End Function
End Class


Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Jul-2013 02:33:21   

Do you have a stack trace?

shekar
User
Posts: 327
Joined: 26-Mar-2010
# Posted on: 04-Jul-2013 06:53:51   

Walaa wrote:

Do you have a stack trace?

Here it is


 at System.ComponentModel.TypeConverter.GetConvertFromException(Object value)
   at System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at System.Drawing.ImageConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at System.ComponentModel.TypeConverter.ConvertFrom(Object value)
   at TypeConverterTierMyClubCircle.TypeConverterClass.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) in H:\Clubman Rel 1.0\TypeConverterTierMyClubCircle\TypeConverterClass.vb:line 31
   at System.ComponentModel.TypeConverter.ConvertFrom(Object value)
   at SD.LLBLGen.Pro.ORMSupportClasses.TypeConverterDataValueProjector.ValuePostProcess(Object preliminaryProjectionResult, Object[] sourceValues) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v4.0\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\Projection\DataValueProjector.cs:line 314
   at SD.LLBLGen.Pro.ORMSupportClasses.DataValueProjector.ProjectValue(Object[] sourceValues) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v4.0\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\Projection\DataValueProjector.cs:line 155
   at SD.LLBLGen.Pro.ORMSupportClasses.ProjectionUtils.FetchProjectionFromReader(List`1 valueProjectors, IGeneralDataProjector projector, IDataReader datasource, Int32 rowsToSkip, Int32 rowsToTake, Boolean clientSideLimitation, Boolean clientSideDistinctFiltering, Boolean clientSidePaging, UniqueList`1 stringCache, Dictionary`2 typeConvertersToRun, IRetrievalQuery queryExecuted) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v4.0\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\Projection\ProjectionUtils.cs:line 110
   at SD.LLBLGen.Pro.ORMSupportClasses.DataTableFiller.Fill(IDataReader dataSource, DataTable toFill, IFieldPersistenceInfo[] fieldsPersistenceInfo, IRetrievalQuery queryExecuted, UniqueList`1 stringCache) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v4.0\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\Miscellaneous\DataTableFiller.cs:line 112
   at SD.LLBLGen.Pro.ORMSupportClasses.DaoBase.ExecuteMultiRowDataTableRetrievalQuery(IRetrievalQuery queryToExecute, DbDataAdapter dataAdapterToUse, DataTable tableToFill, IEntityFields fieldsToReturn) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v4.0\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\SelfServicingSpecific\DaoBase.cs:line 1877
   at SD.LLBLGen.Pro.ORMSupportClasses.DaoBase.PerformGetMultiAsDataTableAction(DataTable tableToFill, ITransaction transactionToUse, QueryParameters parameters) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v4.0\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\SelfServicingSpecific\DaoBase.cs:line 1268
   at SD.LLBLGen.Pro.ORMSupportClasses.DaoBase.GetMultiAsDataTable(IEntityFields fieldsToReturn, DataTable tableToFill, Int64 maxNumberOfItemsToReturn, ISortExpression sortClauses, IPredicate selectFilter, IRelationCollection relations, Boolean allowDuplicates, IGroupByCollection groupByClause, ITransaction transactionToUse, Int32 pageNumber, Int32 pageSize) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v4.0\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\SelfServicingSpecific\DaoBase.cs:line 1042
   at SD.LLBLGen.Pro.ORMSupportClasses.TypedListBase`1.Fill(Int64 maxNumberOfItemsToReturn, ISortExpression sortClauses, Boolean allowDuplicates, IPredicate selectFilter, ITransaction transactionToUse, IGroupByCollection groupByClause, Int32 pageNumber, Int32 pageSize) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v4.0\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\SelfServicingSpecific\TypedListBase.cs:line 173
   at SD.LLBLGen.Pro.ORMSupportClasses.TypedListBase`1.Fill(Int64 maxNumberOfItemsToReturn, ISortExpression sortClauses, Boolean allowDuplicates, IPredicate selectFilter) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v4.0\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\SelfServicingSpecific\TypedListBase.cs:line 127
   at LogTierMyClubCircleMembership.MemberClass.View(Decimal memberid) in H:\Clubman Rel 1.0\LogTierMyClubCircleMembership\MemberClass.vb:line 87
   at UITierMyClubCircleMembership.MemberForm.MemberFormLoad(Object sender, EventArgs e) in H:\Clubman Rel 1.0\UITierMyClubCircleMembership\MemberForm.vb:line 6
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
   at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Control.set_Visible(Boolean value)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at UITierMyClubCircleMembership.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 04-Jul-2013 08:32:30   

You have to include the "System.DBNull" in your typeConverter.ConvertFrom/To and CanConvertFrom/To. The question is: What would you return in that case? (An empty byte[]? Ref: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=11970&StartAtMessage=0&#66786

David Elizondo | LLBLGen Support Team
shekar
User
Posts: 327
Joined: 26-Mar-2010
# Posted on: 04-Jul-2013 10:28:49   

daelmo wrote:

You have to include the "System.DBNull" in your typeConverter.ConvertFrom/To and CanConvertFrom/To. The question is: What would you return in that case? (An empty byte[]? Ref: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=11970&StartAtMessage=0&#66786

Yes An empty byte[]

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Jul-2013 19:58:54   

You didn't specify the line where the exception is thrown, but i guess it's here:

If value Is Nothing Then

And I think you should check for value is DBNull

shekar
User
Posts: 327
Joined: 26-Mar-2010
# Posted on: 05-Jul-2013 13:28:57   

Walaa wrote:

You didn't specify the line where the exception is thrown, but i guess it's here:

If value Is Nothing Then

And I think you should check for value is DBNull

Nice this works fine now. for benefit of other users the change i made is as under

You can change

    If value Is Nothing Then
        Return Nothing
    End If

to

    If IsDBNull(value) Then
        Return Nothing
    End If
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Jul-2013 08:53:19   

Good to know you have it working now simple_smile , and thanks for sharing your solution.

David Elizondo | LLBLGen Support Team