Home All Groups Group Topic Archive Search About

Add New in Bindings Navigator problem

Author
10 Nov 2005 5:05 PM
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

Author
11 Nov 2005 5:27 AM
Kevin Yu [MSFT]
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."
Author
11 Nov 2005 10:00 AM
Vayse
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


Show quote
"Kevin Yu [MSFT]" <v-k***@online.microsoft.com> wrote in message
news:alu5cCo5FHA.2672@TK2MSFTNGXA02.phx.gbl...
> 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."
>
Author
12 Nov 2005 3:21 AM
Kevin Yu [MSFT]
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."
Author
14 Nov 2005 10:20 AM
Vayse
To make things clearer, I've set up a simple example.
I have a screenshot here
http://h1.ripway.com/vayse/SampleProducts.bmp

In fact, I've even put a zip file here
http://h1.ripway.com/vayse/SampleProducts.zip

So, what should I do to stop the error occuring?
Thanks
Vayse



Show quote
"Kevin Yu [MSFT]" <v-k***@online.microsoft.com> wrote in message
news:HBor9gz5FHA.1236@TK2MSFTNGXA02.phx.gbl...
> 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."
>
Author
15 Nov 2005 8:59 AM
Kevin Yu [MSFT]
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."
Author
15 Nov 2005 12:06 PM
Vayse
Thanks, that will do the trick.

Show quote
"Kevin Yu [MSFT]" <v-k***@online.microsoft.com> wrote in message
news:$hH$jLc6FHA.3788@TK2MSFTNGXA02.phx.gbl...
> 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."
>
Author
16 Nov 2005 4:38 AM
Kevin Yu [MSFT]
You're welcome.

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

AddThis Social Bookmark Button