Avoiding NULL exception from database for unset field.

Posts   
 
    
clint
User
Posts: 150
Joined: 15-Nov-2005
# Posted on: 02-Apr-2009 23:49:37   

I have a database field of type int which doesn't allow nulls.

If I create a new entity, that int field is initialized to 0. I'm assuming that is due to the project setting: ConvertNulledReferenceTypesToDefaultValue = true.

If I save the new entity without setting that int field, I'll get an exception that says that database field cannot be null.

I'd like to avoid that exception by checking if the value is null before saving.

What is the proper way of checking that? After trying several things that didn't work, I'm assuming I need to check a field's IsChanged property.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 03-Apr-2009 03:40:07   

Is that field Nullable? LLBLGen Pro build version and runtime library version? (http://llblgen.com/tinyforum/Messages.aspx?ThreadID=7725) You could use LLBLGen Pro Validation to avoid send the entity to the persistence storage if there are errors.

David Elizondo | LLBLGen Support Team
clint
User
Posts: 150
Joined: 15-Nov-2005
# Posted on: 03-Apr-2009 16:23:02   

daelmo wrote:

Is that field Nullable?

No.

daelmo wrote:

LLBLGen Pro build version and runtime library version?

LLBLGen Pro Version 2.0.0.0 Final (November 6th, 2006) Runtime File Version = 2.0.0.61107

I apologize for not originally including that information. I didn't include it in my original message because I didn't think my question was specific to an issue in a particular version.

daelmo wrote:

You could use LLBLGen Pro Validation to avoid send the entity to the persistence storage if there are errors.

Yes, that is what I am doing.

So I'm trying to figure out what triggered the database to return an error about inserting a null value into a database field that doesn't allow nulls.

At first I was going to check if the field is null:


if ( entity.SomeIntField == null )
{
    // error
}

But then I realized that it is just a regular int and would never be null.

Then I tried something like this:


if ( entity.Fields["SomeIntField"].IsNull )
{
    // error
}

But then I realized that is really for use with database fields that DO allow nulls. But my database field doesn't allow nulls.

Since I saw that the int field was being initialized to zero, I thought I could check if it was still zero before I save:


if ( entity.SomeIntField == 0 )
{
    // error
}

I figured if it was still zero, then it wasn't set and therefore it would be an error. In my particular case, that would work because this field happened to be a foreign key field. But then I got to thinking, what if I had a situation where I had a a non-null int field where zero is a valid value?

I was curious if the zero value was the trigger that caused the database error about entering nulls in a non-nullable database field. So I explicitly set the field to zero and I no longer got the database null error. So now I knew that something else was triggering the database null error. Something in LLBLGen knew that the field had not been set and therefore it was trying to assign a null value to the database field.

So I was looking for something in LLBLGen that would tell me whether a field had been set. I am wondering if the IsChanged property is the thing to use:


if (!entity.Fields["SomeIntField"].IsChanged )
{
    // error
}

Or should I be using some other method to determine if a field has been set?

clint
User
Posts: 150
Joined: 15-Nov-2005
# Posted on: 03-Apr-2009 16:36:00   

This seems to work.


if ( entity.Fields["SomeIntField"].CurrentValue == null )
{
    // error
}

Is this the proper way to check if a field has been set or not?

clint
User
Posts: 150
Joined: 15-Nov-2005
# Posted on: 03-Apr-2009 17:33:04   

clint wrote:

This seems to work.


if ( entity.Fields["SomeIntField"].CurrentValue == null )
{
    // error
}

Is this the proper way to check if a field has been set or not?

Actually I just found this method which does the same thing:


if ( entity.TestCurrentFieldValueForNull(SomeEntityFieldIndex.SomeIntField))
{
    // error
}

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 03-Apr-2009 19:18:16   

To mitigate some of these issues for myself - I am setting Default values automatically whenever a new Entity is created. Cuts down on a lot of validation stuff.

You could do this in a couple ways:

  • Use OnInitialized() in each Entity's partial class
  • Use Business-layer Manager classes like an EntityFactory: CustomerManager.CreateNewCustomer() as CustomerEntityHope this helps!

Ryan

clint
User
Posts: 150
Joined: 15-Nov-2005
# Posted on: 03-Apr-2009 19:41:35   

rdhatch wrote:

To mitigate some of these issues for myself - I am setting Default values automatically whenever a new Entity is created. Cuts down on a lot of validation stuff.

You could do this in a couple ways:

  • Use OnInitialized() in each Entity's partial class
  • Use Business-layer Manager classes like an EntityFactory: CustomerManager.CreateNewCustomer() as CustomerEntityHope this helps!

Ryan

I was just thinking about doing that. Before using LLBLGen, I would always initialize my class member variables, so I figured LLBLGen must have something for initializing stuff too. Now I know that OnInitialized() is the place to do it. Thanks!