Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am learning to build Employee Time sheet which holds clock in and clock out before and after break.

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.

I assign nullable TimeSpan to these properties but I am not getting error to set DBNull value while insert operation.

I have also set Time(7) data type in my DB table and set it to allow null.

What I have tried:

public int Id { get; set; }
        public int Day { get; set; }
        public string EmployeeName { get; set; }
        public TimeSpan? PreBreakClockIn { get; set; }
        public TimeSpan? PreBreakClockOut { get; set; }
        public TimeSpan? PostBreakClockIn { get; set; }
        public TimeSpan? PostBreakClockOut { get; set; }


using (SqlConnection con = new SqlConnection(cs))
           {
               using (SqlCommand cmd = new SqlCommand(query.ToString(), con))
               {
                   cmd.CommandType = System.Data.CommandType.Text;

                   if (update)
                   {
                       cmd.Parameters.AddWithValue("@Id", model.Id);
                   }
                   cmd.Parameters.AddWithValue("@EmployeeName", model.EmployeeName);
                   cmd.Parameters.AddWithValue("@Day", model.Day);
                   cmd.Parameters.AddWithValue("@PreBreakClockIn", model.PreBreakClockIn);
                   cmd.Parameters.AddWithValue("@PreBreakClockOut", model.PreBreakClockOut);
                   cmd.Parameters.AddWithValue("@PostBreakClockIn", model.PostBreakClockIn);
                   cmd.Parameters.AddWithValue("@PostBreakClockOut", model.PostBreakClockOut??DBNull.Value);

                   con.Open();

                   rowAffected = cmd.ExecuteNonQuery();
               }
           }
Posted
Updated 23-Feb-20 18:47pm
Comments
Jörgen Andersson 24-Feb-20 7:40am    
Also, stop using AddWithValue!
https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/

I decided to assign New TimeSpan(0,0,0) as default value.

Is it good practice?
 
Share this answer
 
Comments
phil.o 24-Feb-20 0:49am    
Please use the green Improve question widget when you want to bring some more details to the question. Do not use solutions for that purpose.
istudent 24-Feb-20 8:35am    
Sorry, I did not know that. I would do it from next time. Thank you for educating me.
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 DateTimes.
C#
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.
 
Share this answer
 
v2
Comments
istudent 24-Feb-20 8:38am    
The system I am trying to build only calculates how many hours each employee work in a day and total hours worked by a employee for that day. I do not need to worry about the date. That's the scenario.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900