Note that there are some explanatory texts on larger screens.

plurals
  1. PObind DateTimePicker to a DB column of datatype time(0)
    text
    copied!<p>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 <code>time(0)</code> which is bound to a <code>DateTimePicker</code> control. </p> <p>The picker has a custom format of <code>HH:mm</code>. It also should treat <code>NULL</code> values in the DB table, therefore I added to it custom <code>Format</code> and <code>Parse</code> event handlers. The DB column is bound to the picker's <code>Text</code> property, and the picker shows correctly the time values in the DB table, including <code>NULL</code> values. (I tried the <code>Value</code> property, but the picker didn't show DB values correctly).</p> <p>The problem is that when I do <code>context.SaveChanges()</code>, 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.</p> <p>Any help or suggestion will be appreciated.</p> <p>Here is my code:</p> <pre><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 </code></pre> <p><strong>UPDATE</strong></p> <p>OK, I changed the picker databinding to bind to the <code>Value</code> property instead of <code>Text</code>, and changed the <code>dtp_Format</code> code so that the picker shows the correct value.</p> <p>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 <code>Validating</code> event is fired but not the <code>Validated</code> event, the focus doesn't move to the next field and I can't close the form. If I put <code>DateTime.TryParse(MyTimePicker.Value, tmp)</code> inside the <code>Validating</code> event and it returns <code>True</code>. If I set <code>CausesValidation = False</code> the problem is gone but still the picker value isn't saved to the DB.</p> <p>I also noticed during debugging that <code>e.Value</code> inside <code>dtp_Format</code> method is of type <code>TimeSpan</code> whereas <code>e.Value</code> inside <code>dtp_Parse</code> is of type <code>DateTime</code>. Maybe it's what causes the problem? I tried to use <code>TimeSpan</code> inside of <code>DateTime</code> in the code of <code>dtp_Parse</code> but to no avail. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload