views:

333

answers:

2

Question #1: Latest working Version

I'm currently using SubSonic 2.1 built 491. Is there a later built? Where can I get it? I know 2.2 was released but it doesn't come with a Setup and I wouldn't know how to modify the App.Config/Web.Config to work with it.

Question #2: Issue with dateTimePicker control on Windows Form. I keep getting System.FormatException trying to retrieve data From SubSonic to that control or saving data from that control to the Database through SubSonic. For example, if I want to save the Time only, I can use the .Text property. To save the Date, I need to use the .Value property of the control. I've tried all sorts of conversion such as Convert.ToDateTime(dateTimePicker.Value.ToString()) and others but I can't find a consistent pattern that would not throw an exception. Any help on this would be greatly appreciated.

Thanks and regards,

Frank Martorana

A: 

Re Question 1 - I don't think any app.config / web.config change is required between SS2.1 and 2.2

For Question 2 - are you using the new data types in MSSQL2008 by any chance? If so, SS2.2 doesn't seem to handle them properly yet.

A: 

Just stumbled upon your post. I'm not sure if It was a FormatException, but I got a Exception with a databound DateTimePicker, too.

The problem is that

DateTimePicker.MinimumDateTime = #1/1/1753#

while

DateTime.MinValue = #1/1/0001#
New DateTime = #1/1#0001#

So if 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 this with my own inherited DateTimePicker (sorry, but it's written in vb)

To use it you can just bind your Subsonic object to the value property. But you have to set the ShowCheckBox property to true and bind a bool value to the CheckedValue property (to indicate that the date is set) which is persisted in your db, too.

Now If an empty date is returned, the date is set to Now and the checkedValue to false, leading to a disabled DateTimePicker. If you check the Checkbox or choose a date with the calender) the checkbox is set to true and the Checkbox is checked.

Attention: 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
SchlaWiener