datatable.select with filter

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the following code in a on page load event of a user control:

<Other Code>
Dim dt As DataTable ' Also tried using New
dt = m_ClaimHistoryEntity.DataTable 'Error occurs here
m_ClaimHistoryEntity.Clear()
m_ClaimHistoryEntity.Load(FilterTable(dt))

dgClaimHistory.DataSource = m_ClaimHistoryEntity.DataTable
<Other Code>

Private Function FilterTable(ByVal currDataTable As DataTable) As
DataTable
Dim filterExpress As New StringBuilder

If m_bOpenOnly = True Then
'Limit to Open Status
filterExpress.Append("(Status = 'Open' OR IsNull(Status,'') =
'')")
End If

Dim myRow As DataRow
Dim dt As DataTable
Dim rowSet As DataRow()

rowSet = currDataTable.Select(filterExpress.ToString)

For Each myRow In rowSet
dt.Rows.Add(myRow)
Next myRow

Return dt
End Function

I keep receiving the generic error:
Object reference not set to an instance of an object

My goal is simple, I have a populated datatable called:
m_ClaimHistoryEntity.DataTable

I want to a subset of the data in the table, so I want to apply a filter.
Based on the results I want to set a datagrid datasource called:
dgClaimHistory

The code above started with the following simple syntax that keep growing as
tried to isolate the error:

Dim filterExpress As New StringBuilder

If m_bOpenOnly = True Then
'Limit to Open Status
filterExpress.Append("(Status = 'Open' OR IsNull(Status,'') =
'')")
End If

dgClaimHistory.DataSource =
m_ClaimHistoryEntity.DataTable.Select(filterExpress.ToString)

It there is a better approach I'm open to suggestions. If possible, please
provide VB examples. Thanks.

Josh.
 
Hi,

jmhmaine said:
I have the following code in a on page load event of a user control:

<Other Code>
Dim dt As DataTable ' Also tried using New
dt = m_ClaimHistoryEntity.DataTable 'Error occurs here

If you get an "Object reference not set to an instance of an object"
exception at the above line, then most likely m_ClaimHistoryEntity is not
initialized at that point, it is null. If ClaimHistoryEntity must be set by
a property then wait until it is set and do the binding then.

Once you have fixed that, you can filter using a DataView:

Dim dv as DataView
dv = m_ClaimHistoryEntity.DataTable.DefaultView

If ( m_bOpenOnly ) Then
dv.Filter = "Status = 'Open' OR IsNull(Status,'NULL') = 'NULL' "
End If

dgClaimHistory.DataSource = dv


HTH,
Greetings
 
Back
Top