Add New in Bindings Navigator problem

  • Thread starter Thread starter Vayse
  • Start date Start date
V

Vayse

Vb.net 2005, Developer Edition.
When one creates a bindings navigator, one of the standard buttons created
on the toolbar is Add New. Which works fine if you create a new item.
However, if you don't enter any data, and click previous record or Move
First, an error is generated. (there are previous records) In my case, in
frmProducts I get "ProductCode cannot be null"
Two questions

1) How do I trap for this error. Since theres no code in frmProducts.vb for
the Previous Record button, I'm unsure of how to trap it.

2) How do I solve this issue?

Thanks
Vayse
 
Hi Vayse,

You're getting the error, because the typed dataset doesn't allow null in
the ProductCode column, and you didn't input anything into this field, so
the exception is thrown.

I assume that you're using a DataGridView to list all the data in the
dataset. So in this case, the exception is thrown by the DataGridView
instead of the BindingNavigator. If so, you can handle the
DataGridView.DataError event to trap this. Here are the steps:

1. Click the DataGridView on the designer.
2. In the properties window, switch to event view.
3. Double click on DataError and it will create the event handler for you.
4. Add code in the event handler.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi Kevin
I'm not using the Datagridview, just a normal form. I don't see a DataError
event in the form events, or in the ProductsBindingNavigator.
What should I do in this case?
Thanks
Vayse
 
Hi Vayse,

Do you mean that you have only a BindingNavigator on the form without
showing the data? In this case the only thing we can do is to set the
DataSet's EnforceConstraint property to true. However, this is not
recommended.

Since the ProductCode field doesn't allow null value, it's better for us to
add a control to input data for it.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi Vayse,

Thanks for your code to reproduce it. I checked your code, to walkaround
this issue, you have to validate the content of the ProductCode TextBox
when adding a new row. You can handle the ProductCodeTextBox.Validating
event to achieve this.

Private Sub ProductCodeTextBox_Validating(ByVal sender As
System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles
ProductCodeTextBox.Validating
If Me.ProductCodeTextBox.Text = String.Empty Then
e.Cancel = True
MessageBox.Show("No Product Code!")
End If
End Sub

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
You're welcome.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top