By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,586 Members | 623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,586 IT Pros & Developers. It's quick & easy.

Validating Data Using a Error Provider

P: n/a
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!

Jul 5 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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?
"Ryan" <Ty****@newsgroups.nospamwrote in message news:O%****************@TK2MSFTNGP05.phx.gbl...
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!

Jul 5 '06 #2

P: n/a
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 (wa****@online.microsoft.com, 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.

Jul 6 '06 #3

P: n/a
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

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:Ci**************@TK2MSFTNGXA01.phx.gbl...
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 (wa****@online.microsoft.com, 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.

Jul 6 '06 #4

P: n/a
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.

"Ryan" <Ty****@newsgroups.nospamwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:Ci**************@TK2MSFTNGXA01.phx.gbl...
>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 (wa****@online.microsoft.com, 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.


Jul 6 '06 #5

P: n/a
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/de...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 (wa****@online.microsoft.com, 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.

Jul 7 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.