Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
To Null or Not To Null
Message
Information générale
Forum:
ASP.NET
Catégorie:
Formulaires
Divers
Thread ID:
00859705
Message ID:
00859733
Vues:
18
>We are converting a Foxpro database to SQL and all the "empty" dates became 01/01/1900 in SQL. When we bind the data to a textbox, it display 01/01/1900 which is very confusing for the customer. If we programmatically set the empty dates to NULL values, it display blanks in the textbox. However, when we bind the field to a datepicker control, the control can't handle NULL value and give a casting error.
>
>What should be change to get the datepicker working? The control or the structure? And how?

I just went through this same excercise. What I did is not use nulls and instead convert dates that are in the year of 1900 to empty dates for display and set them back to that value on return. This happens automatically in my databinding form control classes.

With the DateTimePicker there's no direct way around this problem. What I did here is subclass it and overlay a text box on top of it. I then set
/// <summary>
/// Summary description for dsTextBox.
/// </summary>
[ToolboxBitmap(typeof(DateTimePicker)),
DefaultProperty("TextValue")]
public class wwDateTimePicker :  System.Windows.Forms.DateTimePicker, Westwind.Windows.IwwDataControl
{

protected System.Windows.Forms.TextBox textinput; 

public wwDateTimePicker() 
{
	this.Format = DateTimePickerFormat.Short;
	this.Width = 85;
	this.CalendarTitleBackColor = SystemColors.InactiveCaption;

	textinput = new TextBox();
	textinput.Left = this.Left +2;
	//textinput.Height = this.Height - 4;
	textinput.Size= this.ClientSize;
	textinput.Width = textinput.Width - 20;
	textinput.Top = this.Top + 2;

	textinput.BackColor = SystemColors.ControlLightLight;
	textinput.BringToFront();
	textinput.Visible = true;
	textinput.BorderStyle = System.Windows.Forms.BorderStyle.None;

	this.Controls.Add(textinput);

	// *** Force events to update
	this.ValueChanged += new EventHandler(wwDateTimePicker_ValueChanged);
	this.textinput.Validated +=new EventHandler(textinput_Validated);
	this.textinput.KeyPress +=new KeyPressEventHandler(textinput_KeyPress);
}

/// <summary>
/// Text value that can be bound to for null and empty values.
/// This value is set internally, but can also be externally accessed
/// for these special situations.
/// </summary>
public string TextValue 
{
	get 
	{
		try 
		{
			if (this.Value < Convert.ToDateTime("01/01/1905") )
				return "";
			
			return this.Value.ToString("d");
		}
		catch 
		{
			return "";
		}
	}
			
	set 
	{  
		
		if (value == "") 
		{
			this.Value = Convert.ToDateTime("01/02/1900");
		}
		else 
		{
			try 
			{
				this.Value = Convert.ToDateTime(value);
			}
			catch 
			{ 
				this.Value = Convert.ToDateTime("01/02/1900"); 
			}
		}

	}
}

protected void wwDateTimePicker_ValueChanged(object sender, EventArgs e) 
{
	try 
	{
		if (this.Value < Convert.ToDateTime("01/01/1901") ) 
		{
			this.textinput.Text = "";
			return;
		}
		
		this.textinput.Text = this.Value.ToString("d");
	}
	catch 
	{
		this.textinput.Text = "";
	}
}

/// <summary>
/// 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void textinput_Validated(object sender, EventArgs e) 
{
	this.TextValue = this.textinput.Text;  // force a refresh
}
}
I also added a TextValue property which holds the value you see and can also edit.

Now, in order to deal with NULL values rather than empty values, you would have to bind to the TextValue field and check for null there and possibly set a flag when a value is null. Everytime the value is changed change the value to non-null then check before binding back.

It's a hack for sure but it works.

+++ Rick ---
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform