Validating Data Using a Error Provider

R

Ryan

A binding navigator control adds the following code for when the Save button is clicked:
Me.Validate()
Me.UserBindingSource.EndEdit()
Me.UserTableAdapter.Update(Me.UserDataSet.User)"

You can add code to the column changing event for the dataset by using the dataset designer, for example:

Private Sub UserDataTable_ColumnChanging(ByVal sender As System.Object, ByVal e As System.Data.DataColumnChangeEventArgs) Handles Me.ColumnChanging
If (e.Column.ColumnName = Me.WindowsLoginColumn.ColumnName) Then
If CType(e.ProposedValue, String) = "" Then
e.Row.SetColumnError(e.Column, "Cannot be blank")
Else
e.Row.SetColumnError(e.Column, "")
End If
End If

The problem is, the validation is not actually done until the .EndEdit event is fired. Me.Validate can be overriden for form validation (not validation on the dataset columns). So EndEdit throws a runtime error say if you have Null for a column that does not allow Nulls. I'm having a very hard time getting my validation to work using the error provider rather than throwing runtime errors. Help!
 
R

Ryan

Ok I'm just going to use form validation and column validation I guess (seems silly). How do I return false since the controls_validating is a sub and not a function?
A binding navigator control adds the following code for when the Save button is clicked:
Me.Validate()
Me.UserBindingSource.EndEdit()
Me.UserTableAdapter.Update(Me.UserDataSet.User)"

You can add code to the column changing event for the dataset by using the dataset designer, for example:

Private Sub UserDataTable_ColumnChanging(ByVal sender As System.Object, ByVal e As System.Data.DataColumnChangeEventArgs) Handles Me.ColumnChanging
If (e.Column.ColumnName = Me.WindowsLoginColumn.ColumnName) Then
If CType(e.ProposedValue, String) = "" Then
e.Row.SetColumnError(e.Column, "Cannot be blank")
Else
e.Row.SetColumnError(e.Column, "")
End If
End If

The problem is, the validation is not actually done until the .EndEdit event is fired. Me.Validate can be overriden for form validation (not validation on the dataset columns). So EndEdit throws a runtime error say if you have Null for a column that does not allow Nulls. I'm having a very hard time getting my validation to work using the error provider rather than throwing runtime errors. Help!
 
W

Walter Wang [MSFT]

Hi Ryan,

Thank you for your post.

Based on my understanding, your question is how to use ErrorProvider to
display errors when working with a DataSet. If I've misunderstood anything,
please feel free to post here.

First, please take a look at following MSDN Library article:

#Walkthrough: Adding Validation to a Dataset
http://msdn2.microsoft.com/en-us/library/ms171930.aspx

From this example, we will see that the ErrorProvider is working when you
move focus away from the field. This will give the user a visual clue of
which field is filled wrong value. Also, the validation will only work when
you changed the field's value.

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang ([email protected], remove 'online.')
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.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Ryan

Hi Walter,

I actually used that walkthrough prior to this post. There are 2 problems
with it.
1) If the user put's nothing in the field (leaves it blank), the
ErrorProvider does not appear. If using the ErrorProvider control during
the control's (text box for example) validating event rather than in the
dataset, it works whether the user enters anything or not, it validates when
the control loses focus whereas the dataset column_changing validating event
only occurs if the field has changed.
2) Just because the error provider error is set (using seterror()), it still
does not stop the database update from happening. So if the field contains
something that violates the dataset integrity a runtime error will occur.
Perhaps a try-catch block that does nothing in the catch block is the way to
go (since the ErrorProvider handles informing the user of the input error).

Ryan
 
R

Ryan

Hmmm I tried the try-catch block but it seems the error occurs before the
DataTable_ColumnChanging event occurs (where the validation takes place).
So if a field violates database integrity, nothing happens.
 
W

Walter Wang [MSFT]

Hi Ryan,

Thank you for your post.

Using the DataTable's ColumnChanging event, it must occur after you changed
the data, so for a default empty field, this event will not be triggered,
thus the ErrorProvider didn't appear. I recommend you take a look at
following article:

#Extending Windows Forms with a Custom Validation Component Library
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/htm
l/winforms03162004.asp

With this custom validation component library, you can use these validators
and check each validator's IsValid property after you called "Validate"
method in the save button's click event. If any validator is invalid, don't
call remaining "EndEdit" and "TableAdapter.Update".

Hope this helps. Please feel free to post here if anything is unclear.


Regards,
Walter Wang ([email protected], remove 'online.')
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.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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