Databinding - one dataset, multiple forms

J

Joe

Hi,

I'm trying to share one dataset across multiple forms in an MDI application.
I pass the same, shared, dataset into each child form through its
constructor. I am using databinding to bind my controls in each form to
this dataset. If I have one MDI child window open, modifying the DataSet
works fine - but if I have more than one window open, where controls in each
window are bound to the same table of the dataset, exceptions get thrown all
over the place - usually a Null Reference Exception from some WinForm
Control ("an exception occurred in Windows.Forms.dll" ...). Even
non-dataset actions, like opening an Open File Dialog cause thesee
exceptions to be thrown. I've also tried binding to DataViews local to each
child window, but this doesn't help. Any help would be greatly appreciated.

Dave Hagedorn
 
W

William Ryan

Joe: it's kind of hard to tell without looking at it, but I'm guessing
somewhere along the way the dataset is being disposed of. Instead of
passing it around, why not create the dataset as a module's property in
VB.NET or as a Static member of a class in C#. Then just reference it from
your forms. It sounds like something other than the dataset is the problem
if you have a form an not using any dataset references and opening a
OpenFile dialog is giving you drama.

So let's say you have SomeDataSet, and you open ChildOne passing in
SomeDataSet. YOu do your thing, then create AnotherChild passing in
SomeDataSet. Does the AnotherChild Open without the exceptions? If so, and
then you use an OpenFileDialog, is that throwing a null reference exception?

My first approach would be to use the DataSet in a shared/static fashion
(shared in the literal sense as in VB's equivalent to C#'s static). After
that, I'd start checking where the exceptions are being thrown. Is it every
reference to the dataset? Could you be referencing a table that doesn't
exist for instance. You may also want to start using
Debug.Assertions/Trace.Assertions before each time you reference the
Dataset. Debug.Assert(SomeDataSet !=Null) Debug.Assert(Not SomeDataSet is
nothing) or whatever Assert scheme would fit to verify the thing exists. If
you create the shared class instead of passing it around, I think it'll go
away if this is the problem, but you may also see some better performance
depending on how things are being passed.

Let me know exactly what's causing those exceptions, is it an
InitializeBinding, is it only when you do x on the third from, when does is
the exception first thrown. I think Debug.Writelines and Assertions should
help track down the problem.

Cheers,
Bill
 
D

Dave Hgaedorn

Hi Will:

Thanks for the reply, although I think I've found the problem - it seems
to be related to threading. When my forms load, I start a getData
procedure in a seperate thread so that my form can continue to load
while its dataset is populated. I'm not sure why this was causing the
problem, but making the call sequentially instead seems to work fine.

The problem was in opening dialog windows (ShowDialog()) only - any and
every time I opened a modal dialog I'd get either an SEHException, or a
NullReferenceException from withing windows.forms.dll

Thanks again,

Dave
 

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