Home All Groups Group Topic Archive Search About

Mysteries when clearing a data table?

Author
28 Jun 2005 3:34 AM
dbuchanan
Help me understand the behavior of this code.

[Mystery #1]
If I run code 1 (below) the datagrid stays populated.

=== incidental code ===
Private dataSet1 As CLIP.dsTables

private Sub FillDataSet()
    Dim DAL As New CLIP.DataAccess
    DAL.daJob.Fill(dataSet1, "tblJob")
    Me.DataGrid1.DataMember = "tblJob"
    Me.DataGrid1.DataSource = dataSet1

End Sub
============

=== code 1 ===

Private Sub btnTestReload_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnTestReload.Click
    Me.DataGrid1 = Nothing
    Dim dataset1 As New CLIP.dsTables
    Dim myTable As New DataTable
    myTable.Clear()
End Sub

============

[Mystery #2]
But, if I run code 2 the datagrid is cleared why?

=== code 2 ===

Private Sub btnTestReload_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnTestReload.Click
    Me.DataGrid1 = Nothing
    Dim dataset1 As New CLIP.dsTables
    Dim myTable As New DataTable
    ClearTable(myTable)
End Sub

Private Sub ClearTable(ByVal myTable As DataTable)
    myTable.Clear()
End Sub

============

[Mystery #3]
If I run code 3 after either of these I get an error

=== Code 3 ===
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
    Call LoadDataInForm() 'The same code that the form_load uses to load
the data into the datagrid

End Sub
====

[Mystery #3 continued]
The error occurs during the datagrid load on these lines
        Me.DataGrid1.DataMember = "tblJob"
        Me.DataGrid1.DataSource = dataSet1

The error is a null reference exception. But why??? DataTable Clear
does not destroy the table just remove the rows. Any explaination?

---
Doug

Author
28 Jun 2005 7:35 AM
Steve
"dbuchanan" <dbuchana***@hotmail.com> wrote in message
news:1119929661.341005.6430@g43g2000cwa.googlegroups.com...
> [Mystery #3 continued]
> The error occurs during the datagrid load on these lines
> Me.DataGrid1.DataMember = "tblJob"
> Me.DataGrid1.DataSource = dataSet1
>
> The error is a null reference exception. But why??? DataTable Clear
> does not destroy the table just remove the rows. Any explaination?

You are destroying the reference to your datagrid in your earlier code with
Me.DataGrid1 = Nothing

If you want to remove the datasource from your datagrid use
Me.DataGrid1.datasource = Nothing

I think that could be the answer to mysteries 1-3

hth
Steve
Author
28 Jun 2005 10:01 AM
dbuchanan
Thank you that was it.