Note that there are some explanatory texts on larger screens.

plurals
  1. PObind DateTimePicker to a DB column of datatype time(0)
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COIt 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).
      singulars
    2. COHow 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?
      singulars
    3. COThese 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
      singulars
 

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