Handling null dates

Posts   
 
    
dma550
User
Posts: 20
Joined: 11-Feb-2006
# Posted on: 28-Feb-2006 23:55:03   

I read about 5 threads on doing this, but I'm still at a loss!

I have need for a nullable date column. I tried several different ways to get this done. I am using devexpress controls, so I have it set so that datetime.minvalue shows as blanks on both my grids and my dateedit controls. This seems to work ok. However, when the save operation happens, which is simply this (using self servicing):

OrgSubscriber.Save(True)

it looks like it is trying to stuff datetime.minvalue into the null date column. Whats the best way to handle this? I have tried custom bindings, etc. If I have the devexpress control handle blank as a true null, I get exceptions thrown on dbull.

The column is nullable in the db.

Thanks!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 01-Mar-2006 07:11:44   

To set an Entity's field to a null value, use the method SetNewFieldValue as follows:

YouEntityObject.SetNewFieldValue("YourDateField", null));

Please check this thread: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=2568

dma550
User
Posts: 20
Joined: 11-Feb-2006
# Posted on: 01-Mar-2006 16:18:39   

Hi Walaa

Thanks for the reply. I have read that thread and others.

I am aware how to set it, but given the use of databound fields, and my simple save (ie not iterating through anything to fix things prior to saving), it would be preferable that the control handle this.

Is it proper to have the controls auto-translate to datetime.minvalue but using a custom binding sub parse it back into a null when saving? Is there any other approach to getting the auto generated code to drop a null in when a datetime is datetime.minvalue?

Thanks!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 01-Mar-2006 16:28:54   

Is there any other approach to getting the auto generated code to drop a null in when a datetime is datetime.minvalue?

Yes, you can intercept the OnSaveEntity event and handle write the code that changes the value from dateTime.MinValue to null using SetNewFieldValue(). you can also perform the same task through Validation, please check the LLBLGen Pro documentation "Using the generated code -> Validation per field or per entity"

dma550
User
Posts: 20
Joined: 11-Feb-2006
# Posted on: 01-Mar-2006 17:03:19   

Hi Walaa

Thanks again... I cannot see where the OnsaveEntity is available using self-servicing, is it available, and do you have an example? I searched this out here too.

Thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 01-Mar-2006 17:50:18   

Override OnSave() in the derived class to perform actions. OnSaveEntity is for adapter. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
dma550
User
Posts: 20
Joined: 11-Feb-2006
# Posted on: 01-Mar-2006 18:12:36   

Otis wrote:

Override OnSave() in the derived class to perform actions. OnSaveEntity is for adapter. simple_smile

Thanks, could you please point me to an example? I spent the last half hour searching for one... thanks

dma550
User
Posts: 20
Joined: 11-Feb-2006
# Posted on: 01-Mar-2006 18:48:01   

ok, I figured it out wink

Protected Overrides Sub OnSave() if me.GenesisDate = datetime.MinValue then me.SetNewFieldValue("GenesisDate",nothing) end Sub

Now, I then checked to see whether it fires every time courtesy of me hard setting it, and YAY it does not. You guys have quite the tool here!

JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 01-Mar-2006 19:17:54   

dma550 wrote:

I read about 5 threads on doing this, but I'm still at a loss!

I have need for a nullable date column. I tried several different ways to get this done. I am using devexpress controls, so I have it set so that datetime.minvalue shows as blanks on both my grids and my dateedit controls. This seems to work ok. However, when the save operation happens, which is simply this (using self servicing):

OrgSubscriber.Save(True)

it looks like it is trying to stuff datetime.minvalue into the null date column. Whats the best way to handle this? I have tried custom bindings, etc. If I have the devexpress control handle blank as a true null, I get exceptions thrown on dbull.

The column is nullable in the db.

Thanks!

I'm using Devex controls and I don't have a problem saving a null date. I just double-checked the database to make sure the date min value hadn't been slipped in there, but it's null. I wonder if you are doing something different than me?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 01-Mar-2006 19:20:05   

dma550 wrote:

ok, I figured it out wink

Protected Overrides Sub OnSave() if me.GenesisDate = datetime.MinValue then me.SetNewFieldValue("GenesisDate",nothing) end Sub

Now, I then checked to see whether it fires every time courtesy of me hard setting it, and YAY it does not. You guys have quite the tool here!

Must be my non-english native speaking but am I right that it doesn't work as expected? flushed

OnSave() is called prior to saving the entity, please consult the reference manual: EntityBase.OnSave() and OnSaveComplete().

When an entity is dirty and thus has to be saved, the entity ends up in the queue. The queue is then saved one entity at a time. Right BEFORE entity.Validate() is called, entity.OnSave() is called. You can make last minute changes here. After that routine, the actual query is produced by calling entity.InsertEntity or entity.UpdateEntity, depending on what type of action has to be executed. So if you override OnSave, you can make last minute changes, but only on entities which are already changed.

Frans Bouma | Lead developer LLBLGen Pro
dma550
User
Posts: 20
Joined: 11-Feb-2006
# Posted on: 01-Mar-2006 20:01:42   

Sorry Otis, yes it is working properly and I was cheering you on for doing it the right way and making me happy! I was afraid that if I stuffed a value at that point it would cause an extra update, but it doesn't, which is good.

I'm sorry, I'll try to be more clear in the future, I don't want to waste your time by trying to be cute simple_smile

Also, Jim, I'm not using the devexpress grid for edits, it's just display. The edit form has edit controls that share the same entity as the grid. I too wonder why it worked for you out of the box and why I had to do this extra step, most likely it was something the grid is automatically handling?

JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 01-Mar-2006 22:22:29   

I haven't had to put a date in a grid yet, but I am using the date editor. I have the NullDate property set to 1/1/0001 12:00:00 AM. If I don't change anything then when I save the date stays null in the database.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 02-Mar-2006 08:38:27   

dma550 wrote:

Sorry Otis, yes it is working properly and I was cheering you on for doing it the right way and making me happy! I was afraid that if I stuffed a value at that point it would cause an extra update, but it doesn't, which is good.

smile

Frans Bouma | Lead developer LLBLGen Pro