Mysteries when clearing a data table?

D

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?
 
S

Steve

dbuchanan said:
[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top