Base: handle non-existing column in NotNull
This commit is contained in:
parent
5ec049ff61
commit
73f95de4c8
@ -1,16 +1,17 @@
|
||||
Public Class DatabaseEx
|
||||
''' <summary>
|
||||
''' TODO: Deprecate
|
||||
''' Checks a Row value for three different `null` values,
|
||||
''' Nothing, Empty String, DBNull
|
||||
'''
|
||||
''' Returns the original value if the value is not null, or `defaultValue`
|
||||
''' </summary>
|
||||
''' <typeparam name="T">The type of the value</typeparam>
|
||||
''' <param name="Row">The DataRow that contains the value</param>
|
||||
''' <param name="Column">The column name</param>
|
||||
''' <param name="DefaultValue">The default value</param>
|
||||
''' <param name="pRow">The DataRow that contains the value</param>
|
||||
''' <param name="pColumn">The column name</param>
|
||||
''' <param name="pDefaultValue">The default value</param>
|
||||
''' <returns>The original value or the default value</returns>
|
||||
Public Shared Function NotNull(Of T)(ByVal Row As DataRow, Column As String, DefaultValue As T) As T
|
||||
Return ObjectEx.NotNull(Row.Item(Column), DefaultValue)
|
||||
Public Shared Function NotNull(Of T)(ByVal pRow As DataRow, pColumn As String, pDefaultValue As T) As T
|
||||
Return ObjectEx.NotNull(pRow.Item(pColumn), pDefaultValue)
|
||||
End Function
|
||||
End Class
|
||||
|
||||
@ -78,6 +78,7 @@ Public Module ModuleExtensions
|
||||
<Extension()>
|
||||
Public Function ItemEx(Of T)(pRow As DataRow, pFieldName As String, Optional pDefaultValue As T = Nothing) As T
|
||||
Try
|
||||
If TableContainsColumn(pRow.Table, pFieldName) = False Then Return pDefaultValue
|
||||
Return ObjectEx.NotNull(pRow.Item(pFieldName), pDefaultValue)
|
||||
Catch ex As Exception
|
||||
Return Nothing
|
||||
@ -87,6 +88,7 @@ Public Module ModuleExtensions
|
||||
<Extension()>
|
||||
Public Function ItemEx(Of T)(pRow As DataRow, pFieldIndex As Integer, Optional pDefaultValue As T = Nothing) As T
|
||||
Try
|
||||
If TableContainsColumn(pRow.Table, pFieldIndex) = False Then Return pDefaultValue
|
||||
Return ObjectEx.NotNull(pRow.Item(pFieldIndex), pDefaultValue)
|
||||
Catch ex As Exception
|
||||
Return Nothing
|
||||
@ -114,4 +116,26 @@ Public Module ModuleExtensions
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function TableContainsColumn(pTable As DataTable, pColumnName As String) As Boolean
|
||||
Try
|
||||
If pTable Is Nothing Then Return False
|
||||
If String.IsNullOrEmpty(pColumnName) Then Return False
|
||||
|
||||
Return pTable.Columns.Contains(pColumnName)
|
||||
Catch ex As Exception
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function TableContainsColumn(pTable As DataTable, pColumnIndex As Integer) As Boolean
|
||||
Try
|
||||
If pTable Is Nothing Then Return False
|
||||
If String.IsNullOrEmpty(pColumnIndex) Then Return False
|
||||
|
||||
Return pTable.Columns.Count > pColumnIndex
|
||||
Catch ex As Exception
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
End Module
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user