0

I have a SQL Server table and I'm trying to bind controls to the corresponding columns of this table (I'm using EF4). One of the columns is of datatype time(0) which is bound to a DateTimePicker control.

The picker has a custom format of HH:mm. It also should treat NULL values in the DB table, therefore I added to it custom Format and Parse event handlers. The DB column is bound to the picker's Text property, and the picker shows correctly the time values in the DB table, including NULL values. (I tried the Value property, but the picker didn't show DB values correctly).

The problem is that when I do context.SaveChanges(), all the values are saved to the DB except for the picker value. I don't get any error message or something similar, so I can't understand why the picker fails to save its value. Changing only the picker value doesn't seem to have any effect on the DB table.

Any help or suggestion will be appreciated.

Here is my code:

Private Sub MyScreen_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim b As Binding = MyTimePicker.DataBindings.Item("Value")
    AddHandler b.Format, New ConvertEventHandler(AddressOf dtp_Format)
    AddHandler b.Parse, New ConvertEventHandler(AddressOf dtp_Parse)

    context = New MyDBEntities()
    Me.ProductsBindingSource.DataSource = context.Products
End Sub

Private Sub dtp_Format(sender As Object, e As ConvertEventArgs)
    Dim bnd As Binding = CType(sender, Binding)
    If bnd IsNot Nothing Then
        Dim dtp As DateTimePicker = CType(bnd.Control, DateTimePicker)
        If dtp IsNot Nothing Then
            If e.Value Is Nothing Then
                dtp.Checked = False
                e.Value = dtp.Value
            Else
                dtp.Checked = True
                dtp.Value = dtp.Value.Date + e.Value
            End If
        End If
    End If
End Sub

Private Sub dtp_Parse(sender As Object, e As ConvertEventArgs)
    Dim bnd As Binding = CType(sender, Binding)
    If bnd IsNot Nothing Then
        Dim dtp As DateTimePicker = CType(bnd.Control, DateTimePicker)
        If dtp IsNot Nothing Then
            If Not dtp.Checked Then
                dtp.ShowCheckBox = True
                dtp.Checked = False
                e.Value = DBNull.Value
            Else
                Dim val As DateTime = Convert.ToDateTime(e.Value)
                e.Value = New Nullable(Of DateTime)(val)
            End If
        End If
    End If
End Sub

UPDATE

OK, I changed the picker databinding to bind to the Value property instead of Text, and changed the dtp_Format code so that the picker shows the correct value.

There seems to be a problem with the picker validation, which probably prevents saving the picker value to the DB. If I change the picker value and try to move to another field, the Validating event is fired but not the Validated event, the focus doesn't move to the next field and I can't close the form. If I put DateTime.TryParse(MyTimePicker.Value, tmp) inside the Validating event and it returns True. If I set CausesValidation = False the problem is gone but still the picker value isn't saved to the DB.

I also noticed during debugging that e.Value inside dtp_Format method is of type TimeSpan whereas e.Value inside dtp_Parse is of type DateTime. Maybe it's what causes the problem? I tried to use TimeSpan inside of DateTime in the code of dtp_Parse but to no avail.

kodkod
  • 1,556
  • 4
  • 21
  • 43
  • It seems like binding to the Text property might be the reason for this problem. The selections are stored in `.Value` and thus this is the property which should be "binded". The reason why you got an error is, most likely, because this is a date variable and the corresponding column in the DB is text; you should either adapt your database or bind `.Value` such that it is treated as a string. After a quick research, I found quite a few references to this second option (although most of them for WPF). I cannot be of further help (have never been in such a situation). – varocarbas Nov 11 '13 at 08:33
  • How do I "bind `.Value` such that it is treated as a string"? I don't seem to find the way to do it in the code or the edmx designer. Could you give me the references you found, please? – kodkod Nov 11 '13 at 10:03
  • These ones are in Winforms (for a MaskedTextBox): http://stackoverflow.com/questions/2329164/how-to-apply-formatting-string-when-binding-datetime-to-maskedtextbox http://stackoverflow.com/questions/982261/bind-nullable-datetime-to-maskedtextbox/986666#986666 and here quite a few in google: https://www.google.com/search?q=binding+date+as+string+vb.net&oq=binding+date+as+string+vb.net&aqs=chrome..69i57.6899j0j7&sourceid=chrome&espv=210&es_sm=93&ie=UTF-8 – varocarbas Nov 11 '13 at 10:07

0 Answers0