istudent wrote:
Employee can clock in and clock out before they go for break and in that case I would like to insert DB Null value in PostBreakClockIn and PostBreakClockOut properties.
It is not what your code is actually doing. It only set
DBNull.Value
for
PostBreakClockOut
. And since you defined other variables to be nullable too, you have to account for their nullity as well. And you should use
DateTime
values instead of
TimeSpan
values. A
TimeSpan
is a
duration between two
DateTime
s.
public DateTime? PreBreakClockIn { get; set; }
public DateTime? PreBreakClockOut { get; set; }
public DateTime? PostBreakClockIn { get; set; }
public DateTime? PostBreakClockOut { get; set; }
cmd.Parameters.AddWithValue("@PreBreakClockIn", model.PreBreakClockIn.HasValue ? model.PreBreakClockIn.Value : DBNull.Value);
cmd.Parameters.AddWithValue("@PreBreakClockOut", model.PreBreakClockOut.HasValue ? model.PreBreakClockOut.Value : DBNull.Value);
cmd.Parameters.AddWithValue("@PostBreakClockIn", model.PostBreakClockIn.HasValue ? model.PostBreakClockIn.Value : DBNull.Value);
cmd.Parameters.AddWithValue("@PostBreakClockOut", model.PostBreakClockOut.HasValue ? model.PostBreakClockOut.Value : DBNull.Value);
istudent wrote:
I decided to assign New TimeSpan(0,0,0) as default value.
Is it good practice?
It depends on the meaning of the value. If a zero duration could be meaningful, then it is probably not a good idea, because then you cannot differentiate between a non-set value and a zero duration anymore. Moreover, this defeats the purpose of having declared nullable columns and properties.