Conversion from type 'DBNull' to type 'String' is not valid

后端 未结 8 742
攒了一身酷
攒了一身酷 2020-12-09 16:06

i am receiving this problem

Conversion from type \'DBNull\' to type \'String\' is not valid.

Line 501: hfSupEmail.

8条回答
  •  再見小時候
    2020-12-09 16:55

    To handle it from code, here is a small extension method

    Imports Microsoft.VisualBasic
    Imports System.Runtime.CompilerServices
    
    Public Module HTMLExtensionMethods
         _
        Public Function DefaultIfDBNull(Of T)(ByVal obj As Object) As T
            Return If(Convert.IsDBNull(obj), CType(Nothing, T), CType(obj, T))
        End Function
    End Module
    

    Call it like this.

    hfSupEmail.Value = dt.Rows(0)("SupEmail").DefaultIfDBNull(Of String)()
    

提交回复
热议问题