StrataFrame Forum

Strataframe Datetime picker control and null dates

http://forum.strataframe.net/Topic15245.aspx

By Ross L. Rooker, Sr. - 3/30/2008

I have spent quite a bit of time reviewing the forums and need to know what you suggest with Strataframe on this issue.

The SQL Database has datetime columns that do allow null values. I do not want them to reflect 1/1/1800 or 1/1/1900. I want them to reflect NULL if they are null.

My question is what settings need to be made in the BO Mapping as well as the datetime picker control to make this work.

I am using c#.

By StrataFrame Team - 3/31/2008

You'll want to make sure the values in the database are NULL for starters.  Then, you can use the BOMapper to customize the field(s) that are dates.  When you customize the field, there is a selection of Null Value Options that you can choose from.  You'll want the one that says Return Alternat on Null / Set Null on Alternate (value type).  Then, put

new DateTime(1800, 1, 1)

in for the replacement value.  When you access that field on the business object, it will return 1-1-1800 when the value in the database is Null and when you set the property to 1-1-1800, it will set the database to Null.  Set the corresponding properties on the DateTimePicker, too, and it will display blank when the value is 1-1-1800.

There are lots of creative ways to handle Null dates in .NET because of the fact that a date is a value type, not a reference type.  You could use the Nullable<DateTime> generic (DateTime?), or you could google it and find some solutions like CSLA's SmartDate class that handles it in a unique way.

By Michael Reese - 3/31/2008

Hey Ross

Set the column to nothing in the BO Mapper. Then drop a datetimepicker on the form, add a checkbox to it nd in the edit button click you can use something like the code below. Then you users will be able to null the field out if they want to. It looks pretty cool too.

I am using the Infragistics control but the SF control should work the same.

Michael

**********

    Private Sub txtDateOfBirth_EditorButtonClick1(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinEditors.EditorButtonEventArgs) Handles txtDateOfBirth.EditorButtonClick
        Dim stateButton As StateEditorButton = CType(e.Button, StateEditorButton)

        If (stateButton.Checked) Then
            e.Button.Editor.Value = Nothing
        Else
            e.Button.Editor.Value = DateTime.Today
        End If
    End Sub

By Ross L. Rooker, Sr. - 4/1/2008

BEN, I did what you indicated and it does work except when I use the DEL key to delete the date I get the error:

NullReferenceException
  Object reference not set to an instance of an object.

Source     : MicroFour StrataFrame UI

Stack Trace:
   at MicroFour.StrataFrame.UI.Windows.Forms.DateTimePicker.CreateManualCaret()
   at MicroFour.StrataFrame.UI.Windows.Forms.DateTimePicker.OnGotFocus(EventArgs e)
   at System.Windows.Forms.Control.WmSetFocus(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.DateTimePicker.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
   at System.Windows.Forms.Nativewindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

It appears about 3 times and then does blank out the date. Do you know why?

By Trent L. Taylor - 4/1/2008

You need to set a custom format to make the error go away.  This is actually not an issue but by design when wanting to have the overall NULL support since we extend the capabilities of the underlying DateTimePicker.  You can have the DateTimePicker show any date or date/time combination that you want, but you need to use a CustomFormat.  The reason for this is because we have added a lot of functionality to the DateTimePicker for the NULL support as well as caret support when in a NULL state, etc.  So if you are going to use NULLs, then you need to use a custom format.
  1. Set the Format property to Custom
  2. Set the CustomFormat property to the desired date display format.  You can get more details on this in the .NET documentation, but if you want to use a 04/01/2008 format, then it would look like this MM/dd/yyyy
By Ross L. Rooker, Sr. - 4/1/2008

Thanks... worked like a charm in case anyone else needs to know.