Base: handle non-existing column in NotNull

This commit is contained in:
Jonathan Jenne 2023-07-24 10:41:36 +02:00
parent 5ec049ff61
commit 73f95de4c8
2 changed files with 30 additions and 5 deletions

View File

@ -1,16 +1,17 @@
Public Class DatabaseEx Public Class DatabaseEx
''' <summary> ''' <summary>
''' TODO: Deprecate
''' Checks a Row value for three different `null` values, ''' Checks a Row value for three different `null` values,
''' Nothing, Empty String, DBNull ''' Nothing, Empty String, DBNull
''' '''
''' Returns the original value if the value is not null, or `defaultValue` ''' Returns the original value if the value is not null, or `defaultValue`
''' </summary> ''' </summary>
''' <typeparam name="T">The type of the value</typeparam> ''' <typeparam name="T">The type of the value</typeparam>
''' <param name="Row">The DataRow that contains the value</param> ''' <param name="pRow">The DataRow that contains the value</param>
''' <param name="Column">The column name</param> ''' <param name="pColumn">The column name</param>
''' <param name="DefaultValue">The default value</param> ''' <param name="pDefaultValue">The default value</param>
''' <returns>The original value or the default value</returns> ''' <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 Public Shared Function NotNull(Of T)(ByVal pRow As DataRow, pColumn As String, pDefaultValue As T) As T
Return ObjectEx.NotNull(Row.Item(Column), DefaultValue) Return ObjectEx.NotNull(pRow.Item(pColumn), pDefaultValue)
End Function End Function
End Class End Class

View File

@ -78,6 +78,7 @@ Public Module ModuleExtensions
<Extension()> <Extension()>
Public Function ItemEx(Of T)(pRow As DataRow, pFieldName As String, Optional pDefaultValue As T = Nothing) As T Public Function ItemEx(Of T)(pRow As DataRow, pFieldName As String, Optional pDefaultValue As T = Nothing) As T
Try Try
If TableContainsColumn(pRow.Table, pFieldName) = False Then Return pDefaultValue
Return ObjectEx.NotNull(pRow.Item(pFieldName), pDefaultValue) Return ObjectEx.NotNull(pRow.Item(pFieldName), pDefaultValue)
Catch ex As Exception Catch ex As Exception
Return Nothing Return Nothing
@ -87,6 +88,7 @@ Public Module ModuleExtensions
<Extension()> <Extension()>
Public Function ItemEx(Of T)(pRow As DataRow, pFieldIndex As Integer, Optional pDefaultValue As T = Nothing) As T Public Function ItemEx(Of T)(pRow As DataRow, pFieldIndex As Integer, Optional pDefaultValue As T = Nothing) As T
Try Try
If TableContainsColumn(pRow.Table, pFieldIndex) = False Then Return pDefaultValue
Return ObjectEx.NotNull(pRow.Item(pFieldIndex), pDefaultValue) Return ObjectEx.NotNull(pRow.Item(pFieldIndex), pDefaultValue)
Catch ex As Exception Catch ex As Exception
Return Nothing Return Nothing
@ -114,4 +116,26 @@ Public Module ModuleExtensions
Return Nothing Return Nothing
End Try End Try
End Function 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 End Module