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.