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 ...