I have pieced this together to take a datatable created from a SQL query, it works great until i have a null field. Now I'm stuck any help would be great

I realize this is complex but the end game is to use Interfaces and Ienumerables to build classes data objects from SQL. I feel that generics are the correct way to go so i can pass any object structure and fill it with data.


Fails on this line

Code:
propertyInfo.SetValue(ReturnType, Convert.ChangeType(propertyVal, targetType))
Thanks

Madaxe

Code:
Imports System.Reflection

Namespace Oracle
    Public Class OracleToClass
        Public ReadOnly Property Parent As Object = Nothing

        Sub New(ByRef iParent As Object)
            _Parent = iParent
        End Sub
        Public Function ConvertDataTable(Of T)(ByVal dt As DataTable) As List(Of T)
            Dim ReturnType As List(Of T) = New List(Of T)()
            For Each row As DataRow In dt.Rows
                Dim item As T = GetItem(Of T)(row)
                ReturnType.Add(item)
            Next
            Return ReturnType
        End Function
        Private Function GetItem(Of T)(ByVal dr As DataRow) As T
            Dim temp As Type = GetType(T)
            Dim ReturnType As T = Activator.CreateInstance(Of T)()
            For Each column As DataColumn In dr.Table.Columns
                For Each propertyInfo As PropertyInfo In temp.GetProperties()

                    Dim propertyVal As Object = dr(column.ColumnName)
                    Dim propertyType As Type = propertyInfo.PropertyType
                    Dim targetType = propertyInfo.PropertyType

                    If Convert.IsDBNull(propertyVal) Then
                        propertyVal = DBNullToValue(Of T)(propertyVal)
                    End If

                    If propertyInfo.Name = column.ColumnName Then
                        'propertyInfo.SetValue(ReturnType, Convert.ChangeType(dr(column.ColumnName), propertyInfo.PropertyType))
                        propertyInfo.SetValue(ReturnType, Convert.ChangeType(propertyVal, targetType))
                    Else
                        Continue For
                    End If
                Next
            Next
            Return ReturnType
        End Function

        Private Function DBNullToValue(Of T)(ByVal propertyVal As Object) As T
            Dim GetOnjectType As Type = GetType(T)
            'If TypeOf propertyVal() Is System.DBNull Then Return CType((CObj(0)), T)

            If GetOnjectType = GetType(Integer) Then
                propertyVal = Convert.ToInt32(propertyVal)
            ElseIf GetOnjectType = GetType(Double) Then
                propertyVal = Convert.ToDouble(propertyVal)
            ElseIf GetOnjectType = GetType(Short) Then
                propertyVal = Convert.ToInt16(propertyVal)
            ElseIf GetOnjectType = GetType(Single) Then
                propertyVal = Convert.ToSingle(propertyVal)
            End If

            Return CType((CObj(propertyVal())), T)
        End Function

    End Class
End Namespace