dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [RESOLVED] Handling NULL database fields when converting to classes

  1. #1
    Join Date
    Jun 2018
    Posts
    2

    [RESOLVED] Handling NULL database fields when converting to classes

    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

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,845

    Re: Handling NULL database fields when converting to classes


  3. #3
    Join Date
    Jun 2018
    Posts
    2

    Re: Handling NULL database fields when converting to classes

    works great now

    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 propertyInfo.Name = column.ColumnName Then
    
                            Dim Writeable As Object = Nothing
                            If Convert.IsDBNull(propertyVal) Or propertyVal.ToString = String.Empty Then
                                Writeable = CreateEmptyBaseType(targetType)
                            Else
                                Writeable = propertyVal
                            End If
                            propertyInfo.SetValue(ReturnType, Convert.ChangeType(Writeable, targetType))
                        Else
                            Continue For
                        End If
                    Next
                Next
                Return ReturnType
            End Function
    
            Private Function CreateEmptyBaseType(iTargetType As Type) As Object
                Dim ReturnObject As Object = Nothing
    
                If iTargetType = GetType(Double) Then
                    ReturnObject = CDbl(ReturnObject)
                ElseIf iTargetType = GetType(String) Then
                    ReturnObject = CStr(ReturnObject)
                ElseIf iTargetType = GetType(Int64) Then
                    ReturnObject = CInt(ReturnObject)
                End If
    
                Return ReturnObject
            End Function
    
        End Class
    End Namespace

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)