In certain situations, we want to set a field, such as a DateTime field, to NULL. This is accomplished with a simple code customization.
The example below uses the Orders table in the Southwind sample database included with Iron Speed Designer. We set the ShippedDate field to NULL if the user does not specify the Order Date on the Edit Orders page. The SaveData() method is overridden to check if there is an Order Date, and if not, we set the Shipped Date to NULL in the the page’s Page class.
Step 1: In Design Mode, select the page and then select the OrderRecordControl.
Step 2: In the SaveData() tab, override the SaveData() method to check if OrderDate is empty. If so, set the ShippedDate field to NULL. The SaveData() method is called when the “Save” button is clicked.
Add this code snippet after the Me.GetUIDate() call. This creates an instance of ColumnValue and sets it to nothing. This is similar to creating a record object and setting the Date field to NULL.
C#:
if ((this.OrderDate.Text.Trim().Equals(""))) {
BaseClasses.Data.ColumnValue cv = default(BaseClasses.Data.ColumnValue);
cv = new BaseClasses.Data.ColumnValue(null);
this.DataSource.SetShippedDateFieldValue(cv);
}
Visual Basic .NET:
If (Me.OrderDate.Text.Trim().Equals("")) Then
Dim cv As BaseClasses.Data.ColumnValue
cv = New BaseClasses.Data.ColumnValue(Nothing)
Me.DataSource.SetShippedDateFieldValue(cv)
End If
This code is automatically placed in the OrdersRecordControl class in EditOrders.Controls.cs (.vb).
Step 3: Build and run your application.
Now when the user clicks the “Save” button and there is no Order Date, the Shipped Date field is set to NULL.
Before:
During:
Notice the Order Date was removed while there is a Shipped Date.
After:
Part V: Customizing Application Code