using SetNewFieldValue

Posts   
 
    
Asimov
User
Posts: 113
Joined: 05-Dec-2003
# Posted on: 27-Oct-2004 15:50:12   

Hi!

If I use SetNewFieldValue to set a field to null (SetNewFieldValue("field", null) ), I know this call changes the IsChanged flag to true for the specified field, but I also noticed it doesn't turn the IsNull flag to true! (I tested using a DateTime field). There if I do:


entity.SetNewFieldValue("field", null);
if(entity.Field["field"].IsNull) Response.Write("test");
if(entity.TestForOriginalFieldNullValue("field")) Response.Write("test2");

nothing will be written. Am I doing something wrong? How can I check that my field is null after the call to SetNewFieldValue?

Thank you!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 27-Oct-2004 16:17:05   

IsNull is for signalling if the table field, the field is mapped on, is NULL in the database, so it is only useful to read IsNull after an entity read. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Asimov
User
Posts: 113
Joined: 05-Dec-2003
# Posted on: 27-Oct-2004 16:30:39   

But let's say I read an entity from the database and I put it into the session. A user can modify it in the session before resaving it to the database. One of the fields in the entity is nullable. Therefore, when the user saves the entity in the session, if the texbox corresponding to the nullable field (which is a date) is empty, what should I do to mark it as being null in memory? Should I manually access IsNull and then check back on IsNull to know whether it is null in memory? Thanks wink

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 27-Oct-2004 17:20:08   

Asimov wrote:

But let's say I read an entity from the database and I put it into the session. A user can modify it in the session before resaving it to the database. One of the fields in the entity is nullable. Therefore, when the user saves the entity in the session, if the texbox corresponding to the nullable field (which is a date) is empty, what should I do to mark it as being null in memory? Should I manually access IsNull and then check back on IsNull to know whether it is null in memory? Thanks wink

You can't mark it as null in memory, at least not with some sort of flag. This is because NULL doesn't exist in the world of entities in memory, as you can't work with NULL in .NET, at least not with valuetypes, they always have a value.

To make a field NULL in the database, use the code you use now, SetNewFieldValue(). To test if a field is null/Nothing, use myEntity.Fields[index].CurrentValue==null

Frans Bouma | Lead developer LLBLGen Pro
eugene
User
Posts: 85
Joined: 05-Oct-2004
# Posted on: 21-Jun-2006 11:06:27   

Hi there,

I am trying to figure out the current value of a boolean field in an entity. For example, with an entity that had not been persisted (IsNew == true) I would expect the Field[index]CurrentValue or the function base.GetCurrentFieldValue(index) to return a null if IsChanged == false. Instead I am getting the default value of the field (a false). I will try and summarize the issue:

  • DB value of a field, irrespective of changes in the session: Field.DBValue
  • Actual value of a field (set through the accessor), irrespective of DB Value: ???

The funny thing is that GetCurrentFieldValue returns false, yet when I try to persist the entity and no value is saved, since IsChanged is returning null.

The only workaround I can see is checking on IsChanged and then using the getter to get the value, which doesn't really work well with bool having only two possible values on of which is the default :-)

Thank you very much in advance for your reply!

Best regards

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 21-Jun-2006 16:52:35   

The funny thing is that GetCurrentFieldValue returns false, yet when I try to persist the entity and no value is saved, since IsChanged is returning null.

A value won't get written in the database if it's not been changed in the entity. (IsChanged = false)

Would you please check the topic "Entities, NULL values and defaults" in the LLBLGen Pro manual "Using the generated code -> SelfServicing/Adapter -> Using the entity classes"

eugene
User
Posts: 85
Joined: 05-Oct-2004
# Posted on: 21-Jun-2006 17:01:28   

Hi there,

sorry for the hassle, I just found the TestCurrentFieldValueForNull method which wonderfully serves my needs!

Thanks