Set Default of DateTime field to "today"?

G

Guest

Maybe someone can help me: How can I set the default of a DateTime field used
in a WinForms application to "Today".

This is what I tried:

- The underlying ADO DataColumn of data type DateTime has a default; but
this can only be set to a specific value; afaics, there is no expression
returning a DateTime value with the current date.

- In my WinForms application, when the user is on a new record, the value
of the corresponding DateTimePicker control is set to today like this:

if (((System.Data.DataRowView)this.bsCourses.Current).IsNew)
this.dtDate.Value = System.DateTime.Today;

where this.bsCourses is the current BindingSource and this.dtDate is the
DateTimePicker control (the code is excuted in the PositionChanged event of
the BindingSource). The DateTimePicker control properly accepts the current
date, but out of some reason, when the underlying DataRow is updated by means
of DataBinding, this date is not used, but is set to Null. Only when, in the
DateTimePicker control, the value is set explicitly by the user, everything
works as expected.

Any idea why or how this may otherwise be solved? Thanks for any hint.
 
P

PGC

Hi AxelHecker,

Maybe this may help ...

If this is a SQL table then you can set the default value for new rows in
the table on this date column. In Enterprise Mgr. Table designer, click on
the column and in the "Default Value" property for the column set it equal
to "GetDate()"
This will make SQL server set the date equal to the server date. (This may
be an issue if your server is in a different timezone to the client.)

PGC
 
G

Guest

Thanks very much PGC for your answer!

Well, the GetDate() function in SQL is exactly what I was looking for in
ADO, but it seems that this doesn't exist.

This is the solution I found: on creating an instance of the related ADO
dataset, I set the default of all DateTime columns to today like this:

<<DataTable>>.Columns["Date"].DefaultValue = System.DateTime.Today;

The only disadvantage here is that, in case a user starts the program on a
certain day, but continues to work over midnight, the default value will
still be the day he or she started the program.

Thanks again,
--
Axel Hecker



PGC said:
Hi AxelHecker,

Maybe this may help ...

If this is a SQL table then you can set the default value for new rows in
the table on this date column. In Enterprise Mgr. Table designer, click on
the column and in the "Default Value" property for the column set it equal
to "GetDate()"
This will make SQL server set the date equal to the server date. (This may
be an issue if your server is in a different timezone to the client.)

PGC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top