problems with NULL's

Posts   
 
    
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 25-Oct-2005 23:07:11   

Hi,

I have the following situation. I have an ArticleEntity object, with some fields in the database which are "allow nulls". These fields are intentionally nullable, because the values they might contain are actually "overwriting" default values. These default values are coming from another object (ArticleMaingroupEntity). So, when an article has nulls for those fields, it will use the values defined in corresponding fields in ArticleMaingroupEntity. So for instance, let's say i have a boolean field "Leeggoed". My code (added to the custom code area of the entity class) then to retrieve the value for this field looks like :


public bool EffectiveLeeggoed
{
get
{
    if (this.Fields[(int)ArticleFieldIndex.ArLeeggoed].IsNull)
        return this.ArLeeggoed;
    return this.ArticleMaingroup.ArmainLeeggoed;
}
}

Basically for all fields "XXX" using this mechanism i have defined an "EffectiveXXX" property which will check if the field is null, and if it is, go fetch the default value on the ArticleMaingroup entity.

Now in the GUI i provide the user with a button "Reset to default" (doing the obvious, setting all fields to null). This is where it goes wrong. I'm using the following code to set the values to null, but when i check afterwards, they are not changed. Am i missing something here ?


        public void ResetToDefaults()
        {
            if (this.HasNonDefaultSettings)
                Console.WriteLine("having non-default values values");
            this.SetNewFieldValue((int)ArticleFieldIndex.IntrastatId,null);
            this.SetNewFieldValue((int)ArticleFieldIndex.UnitId,null);
            this.SetNewFieldValue((int)ArticleFieldIndex.ArLot,null);
            this.SetNewFieldValue((int)ArticleFieldIndex.ArLeeggoed,null);
            this.SetNewFieldValue((int)ArticleFieldIndex.VatrateId,null);
            if (this.HasNonDefaultSettings)
***it should NOT come here but it does!  Console.WriteLine("should all be default values (nulls)");
        }

        /// <summary>
        /// will return true if the article has one or more default ArticleMain settings overwritten
        /// if false, all "EffectiveXXX" values will be the ones inherited from the ArticleMain
        /// </summary>
        public bool HasNonDefaultSettings
        {
            get
            {
                if (!this.Fields[(int)ArticleFieldIndex.IntrastatId].IsNull)
                    return true;
                if (!this.Fields[(int)ArticleFieldIndex.UnitId].IsNull)
                    return true;
                if (!this.Fields[(int)ArticleFieldIndex.ArLot].IsNull)
                    return true;
                if (!this.Fields[(int)ArticleFieldIndex.ArLeeggoed].IsNull)
                    return true;
                if (!this.Fields[(int)ArticleFieldIndex.VatrateId].IsNull)
                    return true;
                return false;
            }
        }

How should i do this ? I have tried the following alternatives :

if (!this.Fields[(int)ArticleFieldIndex.UnitId].IsNull) if (this.Fields[(int)ArticleFieldIndex.UnitId].DbValue != null) if (this.Fields[(int)ArticleFieldIndex.UnitId].CurrentValue != null)

but it seems to give the same results ...

Drewes
User
Posts: 67
Joined: 18-Aug-2003
# Posted on: 26-Oct-2005 04:04:04   

What you should do is save the record, the retrieve it agian, at that time it will reflect the update value null.

HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 26-Oct-2005 10:13:10   

yes, but i do not want to do that at this point. The user always has a choice later-on to close the dialog using the "cancel" button, so i definately do not want to save the null's here.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 26-Oct-2005 10:13:51   

IsNull checks if the field was fetched with a NULL. It doesn't check if it's CURRENTLY NULL. You should check CurrentValue's value if it's null or not. If not, set it to null. Now, all your tests fail.

In 1.0.2005.1, I've added a method to each entity: TestCurrentValueForNull(), which checks if the current value for a field is null. This also takes into account if an entity is new etc..

Frans Bouma | Lead developer LLBLGen Pro