Checking NULL value in VB.NET

 从数据库中获取datatable型的数据后在页面上显示时,报出如下错误:
Conversion from type 'DBNull' to type 'Date' is not valid.

原因就是对于DBNull的数据要通过程序处理,处理方法如下:
 Public Enum enumObjectType
            StrType = 0
            IntType = 1
            DblType = 2
            DateType = 3
        End Enum


        ''' <summary>
        ''' 判断是否为数据库NULL类型的数据,是,则转成数据类型
        ''' </summary>
        ''' <param name="obj"></param>
        ''' <param name="ObjectType"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function CheckDBNull(ByVal obj As Object, _
                                          Optional ByVal ObjectType As enumObjectType = enumObjectType.StrType) As Object
            Dim objReturn As Object
            objReturn = obj

            If IsDBNull(obj) = True Then
                Select Case ObjectType
                    Case enumObjectType.StrType
                        objReturn = ""
                    Case enumObjectType.IntType
                        objReturn = 0
                    Case enumObjectType.DblType
                        objReturn = 0.0
                    Case enumObjectType.DateType
                        objReturn = New Date
                End Select
            End If

            Return objReturn

        End Function

你可能感兴趣的:(VB.NET)