NullReferenceException when binding DataGridView to datasource

G

Guest

I have code that is working in one place but not another. In one
spot, you click a button to open an XML file and have it read into a
dataset. I then set the datasource of my datagrid to the dataset, and
it loads perfectly. I then do the same thing with a filesystem
watcher, and I get the nullreference when I set the datasource. I
have verified that the dataset is full. The code is the exact same in
both spots. The other strange thing is that 15% of the time, the
filesystem watcher will work... Any help would be great!

-------------------------------
Declare the FileSystemWatcher and other.....
-------------------------------
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim fsw As New IO.FileSystemWatcher
AddHandler fsw.Created, AddressOf FSWProcess

Try
'If monitoring is enabled, get the path and set
the statusbar text
If monitor = 1 Then
fsw.Path = clsRegistry.GetValue("MonitorDir")
fsw.EnableRaisingEvents = True
statusBar.Items(2).Text = "Monitoring " &
fsw.Path
Else
statusBar.Items(2).Text = "Monitoring is not
enabled"
End If
Catch ex As Exception
clsEventLog.Write(ex.Message, 25)
End Try
End If
End Sub
-----------------------------
This is the manual process that works 100%
-----------------------------
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnBrowse.Click
Try
If Not ofd.ShowDialog = Windows.Forms.DialogResult.Cancel
Then
'Set the import file text
txtImportPath.Text = ofd.FileName
If mnuPopulate.Checked Then
'If auto populate is enabled then import the file
to the daatagrid
statusBar.Items(1).Text = "Status: Loading file "
Me.Refresh()
Cursor.Current = Cursors.WaitCursor
clsData.LoadData(txtImportPath.Text)
Cursor.Current = Cursors.Arrow
dgData.DataSource = ds.Tables(0)
statusBar.Items(0).Text = "Record Count: " &
dgData.RowCount
statusBar.Items(1).Text = "Status: Idle"
End If
End If
Catch ex As Exception
clsEventLog.Write(ex.Message, 20)
End Try

End Sub

-------------------------------
FileSystemWatcher where I get NullException
-------------------------------

Private Sub FSWProcess(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs)
Try
Dim t As New Thread(AddressOf Process)
threadCollection.Add(t, CStr(i))

If mnuMonitor.Checked Then
path = e.FullPath
btnCancel.Enabled = True

statusBar.Items(1).Text = "Status: Loading file "

Try
clsData.LoadData(e.FullPath)
dgData.DataSource = ds.Tables(0)

Catch ex As Exception
MsgBox(ex.ToString())
End Try
statusBar.Items(0).Text = "Record Count: " &
dgData.RowCount
t.Start()
End If

Catch ex As Exception
clsEventLog.Write(ex.Message, 13)
End Try
End Sub


--------------------------------
Public Class clsData

Public Shared Sub LoadData(ByVal path As String)
Dim xmlReader As New XmlTextReader(path)
Try
ds.ReadXml(xmlReader)
Catch ex As Exception
clsEventLog.Write(ex.Message, 30)
End Try
End Sub

End Class
 
B

Bart Mermuys

Hi,

I have code that is working in one place but not another. In one
spot, you click a button to open an XML file and have it read into a
dataset. I then set the datasource of my datagrid to the dataset, and
it loads perfectly. I then do the same thing with a filesystem
watcher, and I get the nullreference when I set the datasource. I
have verified that the dataset is full. The code is the exact same in
both spots. The other strange thing is that 15% of the time, the
filesystem watcher will work... Any help would be great!

' built-in auto-marshal
fsw.SynchronizingObject = Me

If you don't add this line above, then the events will be called from
another thread. And you should never access Control's from another thread
(eg. setting DataSource), if you don't want to use the built-in
synchronizing then you have to marshal the events to the UI thread yourself
using (Control/Form).Invoke and a delegate.

Note that when you drag a FileSystemWatcher on the Form, the designer will
automatically set this property.

And also, since you expect the lifetime of FileSystemWatcher to be beyond
Form_Load, you should not declare it as a local variable, but as a field
(aka member var) instead.

HTH,
Greetings
 

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