473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

NullReferenceException when binding DataGridView to datasource

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

Feb 26 '07 #1
2 3882
Hi,

<no****@meatonconsulting.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
>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
' 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
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


Feb 27 '07 #2
Thanks! That is very good information!

Mar 1 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Vico | last post by:
Hi! I have this scenario: I have a windows form. I have a DataGridView Control which is bound to a DataSource. Under the DataGridView I have texboxes which represent each one of the columns in...
3
by: abc my vclass | last post by:
There are some programs written on .NET 1.1. These applications are apply n-tiers contains Data Access Layers or Business Logic Layer. Now, our company upgrade to .NET 2.0 and enhance or rewrite...
1
by: dbaguru | last post by:
I have a recordset ( converted from an SQL-DMO queryresult ) that I want to bind to a datagridview. I'm not finding any tech documents on this without using a dataadapter or table. thanks in...
0
by: Martin Widmer | last post by:
Hello again! I have a datagridview control on my form and am using VS.Net 2005. One column is set up as combo box column, and when I try to set the datasource for that combobox column at design...
7
by: Nathan | last post by:
I'd like to use an enumeration as a datasource for a drop-down box. Is there a way to do this?
2
by: Bob | last post by:
Got a datagridview with records bound to tableParent. Each time I go to a new row (single select) in the datagrid view I want a series of textboxes as well as another datagridview to shwo the...
1
by: GS | last post by:
I am perplexed . I thought it is easy to fix the binding error on cvtDtFrom. I only used the cvtDtFrom a couple of places but I found out the error occurred before form loading. I tried...
5
by: jehugaleahsa | last post by:
Hello: I am sure this question comes up a lot. I need to disable the controls on my Windows forms so that when the BindingSource is empty some the controls bound to it will be disabled. This...
0
by: myotheraccount | last post by:
I'm a newbie to c#, so bear with me. I'm trying to get data into from a DataReader, put it into a BindingList that is bound into a DataGridView. My code is: BindingList<stringbl = new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.