Base: handle non-existing column in NotNull
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user