468,290 Members | 2,090 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,290 developers. It's quick & easy.

Mysteries when clearing a data table?

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

Nov 21 '05 #1
2 1775

"dbuchanan" <db*********@hotmail.com> wrote in message
news:11********************@g43g2000cwa.googlegrou ps.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
Nov 21 '05 #2
Thank you that was it.

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by rmatteson | last post: by
3 posts views Thread by RSH | last post: by
5 posts views Thread by Bill Jackson | last post: by
3 posts views Thread by Angel Blue01 | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.