Is this behaviour normal?

  • Thread starter Thread starter Özden Irmak
  • Start date Start date
Ö

Özden Irmak

Hi,

I have some simple dataentry screens, textboxes are bound into a datasource.

I connect to Sql Server 2000. When the user wants to add a new record, I
call "AddNew()" method of bindingcontext but it gives me an error saying
"CustomerId cannot be null value". This field is int and does not allow
nulls. But I don't do any posting of the data, just adding a new onw? The
field is not marked as an identity.

In another screen there are varchar fields and they also don't allow nulls
but I don't get this error?

Isn't this weird?

I'm using VS.Net 2003 and C#.

Any help?

Regards,

Özden
 
Whether the column allows DBNull or not is not determined by whether it is
an identity column, but by whether the property "AllowDBNull" of the column
is True or False. You need to take a look at the schema for the DataTable
object that you are working with.

The reason that you don't get an exception when you leave the TextBoxes
empty is that an empty TextBox does not get translated to DBNull, but is
inserted into the DataRow field as a zero-length string!

The solution to your problem is very simple: respect the constraints that
exist on your table and you'll be fine!
 
Hello Diego,

Thank you for your answer but I think you got me wrong.

It would be true if I update that new row into the database table without
filling CustomerId field, but this error occurs when I call "AddNew()"
Method of BindingContext object.

I made a simple screen in VB6 uses that table (An Adodc control and some
textboxes bound to the fields in that table) and it successfully works
(trying to call addnew method of ADODB.Recordset), what do you say about
this?

Regards,

Özden
 
The field does not allow nulls. So when you add a new row to the table,
guess what the value of that field is? It is null. But the table does not
allow nulls for that column so you get the error. The table checks the
constraints.

What you need to do is create the row, let the user fill in an id for it,
set that column in the row and only then, can you add it to the datatable.

Now, this really defeats the whole purpose of databinding, since one would
naturally want to add a blank row to the table and let the user fill in the
values. But there doesn't seem to be good support for this.
 
Hello Marina,

Thank you for your answer...

I can understand to get an exception if I try to update a datarow but this
is not the case.

I'm not adding any empty datarow to datatable's collection, nor calling any
update methods of both the dataset or dataadapter. All I'm doing is to call
the "AddNew" method of BindingContext. This should cause all the bound
controls on a form to clear their texts/values and wait for input for the
new record. Having a "not allow null" field shoudln't cause this and this
case is not reproduced in any other platfom (Neither in VB6 nor on Delphi)

Regards,

Özden
 
I think you need to read up on DataSets and DataTables. What you don't seem
to understand is that your DataSet contains a database schema similar to
that which exists on the database, with similar constraints, like Unique and
FK. At the same time, there is no connection from the DataSet to your
database, but this does not matter. AddNew() fails because of the
constraints that are defined on you DataTable object.
 
I am guessing that calling AddNew on the bindingcontext is the equivalent of
adding a new datarow to the datatable. That's what it does for you behind
the scenes. The row automatically becomes part of the datatable, and then
you get the problem since it checks the constraint right away. The way
binding works, is that the row is always part of the datasource - otherwise
how would it know when it is OK to actually add it.

From then on, any values filled in are placed in the datarow. But the
error already occurred.
 
Back
Top