all groups > dotnet windows forms databinding > august 2006 >
You're in the

dotnet windows forms databinding

group:

BindingNavigator throws Exception on Move away from New Row


BindingNavigator throws Exception on Move away from New Row AxelHecker
8/31/2006 2:08:01 AM
dotnet windows forms databinding:
Problem:

I have two DataTables linked as parent and child via BindingSources, where
the child BindingSource uses the parent BindingSource as DataSource and the
foreign key relation as DataMember in order to provide for proper observation
of parent child relations in the form.

When I want to create a new child record on an existing parent record, I
press '+' on the child BindingNavigator. The problem occurs when, in this
situation, without doing anything else, I press the MovePrevious symbol on
the BindingNavigator. A "NoNullAllowedException" is thrown, and the whole
form is stopped.

Interpretation: On moving to the new child record, this record is
automatically provided with the key entry it has in common with the existing
parent record. Given the fact that no other change occurs on the child
record, it is incomplete insofar the second key entry (that is necessary in
order to distinguish different childs belonging to the same parent) remains
empty. So when I move away from this record, the underlying BindingSource
tries to save it and finds this second key to be NULL.

Question: How can I deal with this (rather common) situation? The normal
behaviour of the BindingSource in the situation described should be: discard
new records that the user moves to but doesn't touch by making any change.
How can the BindingSource be made to understand that new records whose only
changes have been done by the underlyinig BindingSource functionality, should
be discarded as well?

This is what I already tried without success: using RowChanging and
RowChanged events of the DataTable (they are only called after the
exception); using DataError (not called at all) or other events of the
BindingSource.

Thanks for any hint! Axel Hecker.

PS: It seems that another post on this forum from August 8 raises the same
point. It is titled "How do I handle exceptions that are thrown during a move
method of the BindingNavigator" by "Peet", but without answer.

Axel Hecker
RE: BindingNavigator throws Exception on Move away from New Row AxelHecker
8/31/2006 11:10:02 PM
For anybody who is interested, I think I found the answer myself:

1) Set the MovePreviousItem property of the BindingNavigator to Nothing.
2) Select the bindingNavigatorMovePreviousItem object and define the Click
event
3) In the code for this Click event, check for the conditions described in
the original post: a) am I on the new row?
(((System.Data.DataRowView)BindingSource.Current).IsNew=True?) and b) if so,
will the new row provoke the problem described
((((System.Data.DataRowView)BindingSource.Current).IsNull("ChildKeyColumnName"),
where "ChildKeyColumnName" is the name of the primary key column that is
specific for the child records).
4) In this case, call BindingSource.CancelEdit()
5) Finally call BindingSource.MovePrevious() in order to do what the
MovePreviousItem is expected to do.

All this might have to be refined according to specific conditions; the
other Move items of the BindingNavigator might have to be treated the same
way etc.

I'd like to make a general comment. On my desktop, I have many books on
subjects like "ADO .NET Applications", "Databinding with Windows Forms 2.0"
etc. None of these books actually mentiones that the type of problem I
described exists at all, not to speak of possible solutions - although, as I
stated in my original post, the scenario I describe is quite a common one
that many developers (and users!) must fall over.

RE: BindingNavigator throws Exception on Move away from New Row AxelHecker
8/31/2006 11:16:01 PM
Typing error in my previous post:

Under 3)
(((System.Data.DataRowView)BindingSource.Current).IsNull("ChildKeyColumnName")

should read:
((System.Data.DataRowView)BindingSource.Current).Row.IsNull("ChildKeyColumnName")

AddThis Social Bookmark Button