ErrorProvider/BindingSource Problem

G

Guest

Hi,

I'm trying to drop a strongly-typed DataSet onto a form, attach a
BindingSource to one of the tables as well as an ErrorProvider. The control
contains a set of TextBoxes which I bind to columns within the columns within
the same table I bind the BindingSource and ErrorProvider too.

When the control loads up, I call the BindingSource.AddNew() method to start
editing a new row within the DataTable. This works fine and throws an
exception if the first TextBox (which is bound to a column whose AllowDBNull
value is set to false) is left blank. The problem that I'm having is that
the ErrorProvider doesn't seem to be working, but instead an DataException is
generated and the ErrorProvider is never displayed.

If I bind the ErrorProvider to the same DataSet/DataMember that the
BindingSource is bound to it doesn't seem to be working either because it
always displays the ErrorProvider and never clears it and an exception is
still generated when the required column is left empty.

I can send code if you'd like to investigate.

Thanks.
 
L

Linda Liu [MSFT]

Hi Jason,

Thank you posting.
This is a quick note to let you know that I am performing research on this
issue and will get back to you as soon as possible. I appreciate your
patience.




Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
L

Linda Liu [MSFT]

Hi Jason,

Firstly, I am so sorry keeping you waiting for a long time.

In your project, if you set the DataSource property of the ErrorProvider to
the BindingSource, the ErrorProvider will monitor the error occuring in the
table to which the BindingSource has been attached. When the data entered
conflict the constrain of the table's XML Schema, the ErroProvider will
display besides the controls that contain error data and this will happen
without any additional code.

The ErrorProvider can monitor the error in the rows that have existed in
the table. However if a new DataRow is added to the table which contains
invalid data, the ErrorProvider doesn't monitor this error and instead an
DataException is generated.

Perhaps there is another way to validate the new DataRow when it is added
to the table.

Unfortunately, although there's a TableNewRow event in DataTable, this
event is fired after a new DataRow has been created by calling the NewRow()
method of DataTable or the AddNew() method of BindingSource. The new
DataRow instance is detached and has not been added to the collection. This
event doesn't seem to be a proper event for validing the new DataRow,
because at this time data haven't been entered into the new DataRow
instance.

So it seems that you have to write code to check whether a new DataRow
contains error data before the new DataRow is added to the table.

Hope this is helpful to you.

If you have any other concerns or need anything else, please don't hesitate
to tell me.




Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
G

Guest

Hi Linda,

Unfortunately, this problem is occurring with existing rows, and not just
new rows. I have an inherited control with a strongly-typed DataSet, a
BindingSource and an ErrorProvider. The BindingSource is bound to the
DataSet (and has the DataMember property set to a table within), The
ErrorProvider is then bound to the BindingSource. When I call the EndEdit
method on the BindingSource, the ErrorProvider does not signal an error on
the TextBox that is bound to a column within my DataTable, but instead throws
a DataException.

Thanks.
 
L

Linda Liu [MSFT]

Hi Jason,

Thank you for your reply.

I set up a test project and I had a try in my project following the steps
described in your post. When I call the EndEdit method on the
BindingSource, the ErrorProvider doesn't signal an error on the TextBox
just as you described. But when I bound the TextBox to a column within the
BindingSource, an error signal appeared on the TextBox after I called the
EndEdit method on the BindingSource.

Because you have bound the ErrorProvider to the BindingSource, the
ErrorProvider will monitor the error in the BindingSource and the TextBox
should be bound to one of the columns within the BindingSource to enable
the ErrorProvider to work when an error occurs. If you bind the
ErrorProvider to a DataTable, to enable the ErrorProvider to work you
should bind the TextBox to one of the columns within the DataTable.

Hope this is helpful to you. If the problems still exists after you bind
the TextBox to the BindingSource, you could send a sample project which can
reproduce the problem as an attachment in your reply. If you can't send the
sample project in the community, you could send it to my email address. To
get my actual email, remove the "online" from my display email.

If you have any other concerns or need anything else, please don't hesitate
to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
G

Guest

Hi Linda,

I'm wondering if you can outline what Microsoft's recommendations are for
validating databound controls. There are obviously some deficiencies in the
existing model and I think this is a fairly standard requirement for most
data-driven applications. I think it's unreasonable to expect that we'd
handle the ColumnChanging event on every single column that needs to be
validated and call the Row.EndEdit method to ensure validation occurs, then
turn around and write our own custom validators for new records, since this
only seems to work for existing records. Are we stuck writing our own custom
validation model right from the begining like we were with 1.1? I can't
speak for anyone else, but my preference would be to do validation at the
Data Entity level (i.e. the DataTable level) and simply have those errors
communicated back to the UI, as opposed to duplicating custom validation code
in each UI that touches my data.
 
L

Linda Liu [MSFT]

Hi Jason,

Thank you for your response.

This is a quick note to let you know that I am performing research on this
issue and will get back to you as soon as possible. I appreciate your
patience.




Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
L

Linda Liu [MSFT]

Hi Jason,

When the values of a DataRow in a DataTable change, four events will be
raised. They are ColumnChanging event, ColumnChanged event, RowChanging
event and RowChanged event. Data validation will be done in the RowChanging
event handler inside the DataTable. If the data being entered conflict with
the restriction of the DataSet schema, errors will occurs but no exception
will be thrown. If an ErrorProvider is used to monitor the errors in the
DataTable, the ErrorProvider will appear besides the related controls on
the UI displaying the error messages .

The DataRow's BeginEdit method turns off the RowChanging and RowChange
events after each individual column change. In that case, the events are
not raised until the EndEdit method has been called. The BeginEdit method
is called implicity when the user changes the value of a data-bound
control. So you should call the DataRow's EndEdit method in the
ColumnChanging event handler to enable the RowChanging event to validate
the data being entered inside the DataTable.

Either you call the BindingSource's AddNew or the DataTable's NewRow method
to add a new DataRow, the new DataRow's RowState is Detached. When the
values in a Detached RowState DataRow change , the RowChanging event will
not be raised. That's to say, DataTable will not validate the data in a new
DataRow. You should add your custom code to validate the data in the new
DataRow before you add the new DataRow to the DataTable.

Hope this is helpful to you.
If you have any other concerns or need anything else, please don't hesitate
to let me know.




Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 

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