SubSonic and dateTimePicker control on Windows Form
Question # 1: Latest working version
I am currently using SubSonic 2.1, built by 491. Is there a later build? Where can I get it? I know 2.2 was released, but it doesn't come with the installation and I don't know how to change the App.Config / Web.Config to work with it.
Question # 2: There is a problem with the Windows Form DateTimePicker control.
I keep getting System.FormatException
trying to fetch data from SubSonic into this control, or save data from this control to the database via SubSonic.
For example, if I only want to store time, I can use the .Text property. To store the date, I need to use the .Value property of the control.
I've tried all sorts of conversions like Convert.ToDateTime(dateTimePicker.Value.ToString())
others, but I can't seem to find a consistent pattern that doesn't throw an exception. Any help on this would be greatly appreciated.
Just came across my post. I'm not sure if it was a FormatException, but I got a DateTimePicker data-bound exception.
The problem is
DateTimePicker.MinimumDateTime = #1/1/1753#
while
DateTime.MinValue = #1/1/0001#
New DateTime = #1/1#0001#
So, if you bind a property to a DataGridView that returns a DateTime value earlier than # 1/1/1753 # or later, then # 12/31/9998 # (DateTimePicker.MaximumDateTime) you get an exception.
I solved it with my own inherited DateTimePicker (sorry but written in vb)
To use it, you can simply bind the Subsonic object to the value property. But you have to set the ShowCheckBox property to true and bind the bool value to the CheckedValue property (to indicate that the date is set) which is also stored in your db.
Now If an empty date is returned, the date is set to Now and the checkedValue is false, which will disable the DateTimePicker. If you check the box or select a date with a calendar), check the box under True and the check box is checked.
Warning: do not bind the Checked property directly, bind the CheckedValue property.
Imports System.ComponentModel
Public Class MyDateTimePicker
Inherits System.Windows.Forms.DateTimePicker
<Bindable(True)> _
Public Overloads Property Value() As DateTime
Get
If Not MyBase.Checked And (MyBase.Value < DateTimePicker.MinimumDateTime Or MyBase.Value > DateTimePicker.MaximumDateTime) Then
Return DateTime.Now
Else
Return MyBase.Value
End If
End Get
Set(ByVal value As DateTime)
If ((value < DateTimePicker.MinimumDateTime Or value > DateTimePicker.MaximumDateTime) Or value = #1/1/1900#) Then
MyBase.Value = DateTime.Now
MyBase.Checked = False
Else
MyBase.Value = value
End If
End Set
End Property
Private _CheckedValue As Boolean
<Bindable(True)> _
Public Property CheckedValue() As Boolean
Get
Return _CheckedValue
End Get
Set(ByVal value As Boolean)
_CheckedValue = value
End Set
End Property
Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseUp(e)
RefreshCheckedValue()
End Sub
Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs)
MyBase.OnKeyDown(e)
RefreshCheckedValue()
End Sub
Private Sub RefreshCheckedValue()
CheckedValue = Me.Checked
If Not Me.DataBindings("CheckedValue") Is Nothing Then
Me.DataBindings("CheckedValue").WriteValue()
End If
End Sub
Protected Overrides Sub OnBindingContextChanged(ByVal e As System.EventArgs)
MyBase.OnBindingContextChanged(e)
Static checkedInitialized As Boolean
If Not checkedInitialized AndAlso Not Me.DataBindings("CheckedValue") Is Nothing Then
Me.Checked = Me.CheckedValue
checkedInitialized = True
End If
End Sub
End Class
a source to share