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