cathyb

cathyb

  • NA
  • 4
  • 0

Cannot push a null value to a database using an inherited DateTimePicker

Aug 20 2004 12:17 PM
We have had to develop an inherited datetimepicker control (component)which will allow null values to be viewed as well as accepted. The problem is that I can get the control to display an empty string but it will not write it to the database. It leaves the original value in the database. I am binding the text of the control. When I debug and get to the update part of the code it does update because the datasetchanges are null. Additional information: The control has a required property that can be set to false, this allows the user to delete a date and leave it empty. If it is set to true the user cannot delete the date, but null values from the datebase can be displayed. The following is all of the code: using System; using System.ComponentModel; using System.Collections; using System.Diagnostics; using System.Windows.Forms; namespace MATBaseClassLibrary { /// /// Summary description for MATDateTimePicker. /// public class MATDate : System.Windows.Forms.DateTimePicker { #region Visual Studio Generated Code /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public MATDate(System.ComponentModel.IContainer container) { /// /// Required for Windows.Forms Class Composition Designer support /// container.Add(this); InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } public MATDate() : base() { /// /// Required for Windows.Forms Class Composition Designer support /// InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // this.CustomFormat = "MM/dd/yy"; //this.CustomFormat = " "; this.Format = System.Windows.Forms.DateTimePickerFormat.Custom; } /// /// Clean up any resources being used. /// #region Component Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.errorProvider1 = new System.Windows.Forms.ErrorProvider(); // // MATDate // } #endregion #endregion #region Variables private DateTimePickerFormat oldFormat = DateTimePickerFormat.Short; private string oldCustomFormat = null; private bool _IsNull = false; private bool _IsAltered = false; private bool _IsRequired = true; private bool _IsDelete = false; private System.Windows.Forms.ErrorProvider errorProvider1; #endregion #region Properties //Property Defaulted to true //True does not allow user to delete a date resulting in an DBNull value [Browsable(true)] public bool Required { get { return _IsRequired; } set { _IsRequired= value; } } //hides the inherited member DateTimePicker.value //overriddes the Value property to accept a null value as DateTime.MinValue public new DateTime Value { get { if (_IsNull && !_IsDelete) { return DateTime.MinValue; } if (_IsDelete) { return DateTime.MinValue; } else { return base.Value; } } set { if ((value == DateTime.MinValue)) { if (_IsNull == false) { oldFormat = this.Format; oldCustomFormat = this.CustomFormat; _IsNull = true; } this.Format = DateTimePickerFormat.Custom; this.CustomFormat = " "; this.Text = null; } // else if (_IsDelete) // { // this.Format = DateTimePickerFormat.Custom; // this.CustomFormat = " "; // this.Checked = true; // this.Text = null; // this.Checked = false; // } else { if (_IsNull) { this.Format = oldFormat; this.CustomFormat = oldCustomFormat; _IsNull = false; } base.Value = value; } } } //override the text property //Text can accept a null by altering the string value public override string Text { get { if (_IsNull) { base.Text = null; return base.Text; } else if (_IsDelete) { base.Text = null; return base.Text; } else { return base.Text; } } set { if ((value == null) || (value == "")) { if(_IsAltered && !_IsNull) //Date was originally a DBNull value and now a date was selected { value = base.Text; _IsAltered = false; base.CustomFormat = "MM/dd/yy"; base.Format = DateTimePickerFormat.Custom; base.Text = ""; } else { base.CustomFormat = " "; base.Format = DateTimePickerFormat.Custom; base.Text = ""; } } else if(_IsDelete) //delete key was pressed and text was changed to null { //this.Value=Convert.ToDateTime(value); base.CustomFormat = " "; base.Format = DateTimePickerFormat.Custom; base.Text = ""; } else { base.CustomFormat = "MM/dd/yy"; base.Format = DateTimePickerFormat.Custom; base.Text = value; } } } #endregion #region Methods //event triggered when a user selects a date from the datetimepicker protected override void OnCloseUp(EventArgs eventargs) { base.OnCloseUp (eventargs); if (Control.MouseButtons == MouseButtons.None) { if (_IsNull) { this.Format = oldFormat; this.CustomFormat = oldCustomFormat; _IsNull = false; } else { _IsAltered = true; base.CustomFormat = "MM/dd/yy"; base.Format = DateTimePickerFormat.Custom; } } } //event triggered when delete key is pressed protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown (e); if (_IsRequired ==false) { if (e.KeyCode == Keys.Delete) { _IsDelete = true; this.Value = DateTime.MinValue; } } } #endregion } }

Answers (3)