My textbox datatype is DateTime.

Using the click event of a button, this code is placed following MyTableBindingSourceAddNew()

Dim CurrentDateTime As DateTime = DateTime.Now.ToString("dd.MM.yy hh:mm:ss")
Me.SaleDateTextBox.Text = CurrentDateTime

I then update table with TableAdapter.

On my form I have a dropdown combobox in a toolbar, display member being "SaleDate"
This displays DateTime, but time is always 12:00:00AM
The SaleDateTextbox.Text populates as Date.Now.

I am not getting correct date and time in both textbox and combobox.

All help appreciated.

Shane.

Dani AI

Generated

This looks like a classic data-binding vs formatting problem. converted the current date/time to a string and wrote that into a bound textbox right after AddNew; was right to flag the string/DateTime mismatch. When a control is data-bound the BindingManager can reapply defaults for the new row, and converting to/from string introduces parsing and culture/format risks that can drop the time portion or be ignored by the underlying DataRow.

Recommended, reliable approach:

  • After calling AddNew, set the SaleDate on the underlying row (BindingSource.Current) as a DateTime value instead of writing to the Text property.
  • Call BindingSource.EndEdit() before updating the TableAdapter so the edited values are pushed into the DataRow.
  • Turn on Option Strict to catch accidental string-to-DateTime conversions at compile time.
    Example (VB):

Dim drv As DataRowView = CType(MyTableBindingSource.Current, DataRowView)
drv("SaleDate") = DateTime.Now
MyTableBindingSource.EndEdit()
Me.MyTableTableAdapter.Update(Me.MyDataSet.MyTable)

Extra checks: confirm the database column actually stores a time component (SQL Server date loses time; use datetime/datetime2), use a DateTimePicker for user entry if appropriate, or handle the Binding.Format event to control display formatting. See the BindingSource.AddNew lifecycle notes and the VB Option Strict guidance for safer type handling: BindingSource.AddNew

Recommended Answers

All 3 Replies

Hi rproffitt. Thank you for your reply.
The ToString is irrelevant. If I display the variable CurrentDateTime in a MessageBox.Show, it returns correct DateTime.
However the field SaleDate does not populate with CurrentDateTime, only Date.Now. This is what I am not understanding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.