I'm using the following code to setup my winforms databinding. For some reason, when I go to save my entity, i get an sql exception b/c the LastContact field is trying to send in the DateTime.MinValue, rather than DbNull.Value. The column in the database allows nulls, but I can't figure out why this is trying to send in 1/1/1.
Any ideas?
/// <summary>
/// Initializes the forms control bindings.
/// </summary>
private void InitializeControlBindings(CustomerEntity customer) {
_customer = customer;
_bindingManager = this.BindingContext[_customer];
// take a snapshot of the entity's state.
_customer.SaveFields(ENTITY_CHECK_POINT);
// begin our bindings.
base.BindField(txtFullName, "Text", _customer, "FullName");
base.BindField(txtJobTitle, "Text", _customer, "JobTitle");
base.BindField(txtCompany, "Text", _customer, "Company");
base.BindField(txtEmail, "Text", _customer, "Email");
base.BindField(txtStreet, "Text", _customer, "Street");
base.BindField(txtCity, "Text", _customer, "City");
base.BindField(txtState, "Text", _customer, "State");
base.BindField(txtZipCode, "Text", _customer, "ZipCode");
base.BindField(txtBusinessPhone, "Text", _customer, "BusinessPhone");
base.BindField(txtBusinessFax, "Text", _customer, "BusinessFax");
base.BindField(txtHomePhone, "Text", _customer, "HomePhone");
base.BindField(txtMobilePhone, "Text", _customer, "Mobile");
base.BindField(txtNotes, "Text", _customer, "Notes");
base.BindField(cbStatus, "Value", _customer, "StatusId");
base.BindField(cbRating, "Value", _customer, "RatingId");
base.BindField(cbSource, "Value", _customer, "SourceId");
base.BindField(cbSaleStage, "Value", _customer, "SaleStageId");
// we need to hook this up seperately in order to catch out format/parse events.
cbLastContact.DataBindings.Clear();
Binding b = new Binding("Text", _customer, "LastContact");
b.Format += new ConvertEventHandler(FormatDateTime);
b.Parse += new ConvertEventHandler(ParseDateTime);
cbLastContact.DataBindings.Add(b);
}
/// <summary>
/// Formats the datetime value.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormatDateTime(object sender, ConvertEventArgs e) {
if (e.DesiredType != typeof(string)) return;
if (Convert.ToDateTime(e.Value) == DateTime.MinValue) {
e.Value = String.Empty;
}
}
/// <summary>
/// Parses the datetime value.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ParseDateTime(object sender, ConvertEventArgs e) {
if (e.DesiredType != typeof(DateTime)) return;
if (Convert.ToString(e.Value).Trim() == String.Empty) {
e.Value = DateTime.MinValue;
}
}