Date value coming 01/01/0001.00:00:00

Asked

Viewed 135 times

1

I’m having a problem with my API when I try to rescue a date from a column in my table in Sqlserver

I’m doing it this way

using (SqlCommand command = new SqlCommand()) {
                        command.Connection = connection;
                        command.CommandText = "SELECT TOP 1 DateStart, DateEnd from CheckUP where DeviceIMEI = @imei ORDER BY DateStart DESC";
                        command.Parameters.AddWithValue("imei", obj.DeviceIMEI);
                        SqlDataReader reader = command.ExecuteReader();

                        while (reader.Read()) {
                            CheckUP check = new CheckUP() {
                                DateStart = reader["DateStart"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(reader["DateStart"]),
                                DateEnd = reader["DateEnd"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(reader["DateEnd"]),
                            };

                            if (reader.IsDBNull(1) == true) {
                                System.DateTime today = DateTime.Today;

                                Debug.WriteLine("hoje"+ today);
                                Debug.WriteLine("data do banco" +  checkup.DateStart);

                                TimeSpan diferenceToday = today - checkup.DateStart;
                                totalLifeCheckUpProgress = diferenceToday.ToString();
                                Debug.WriteLine("Resultado"+totalLifeCheckUpProgress);
                            } else {
                                TimeSpan difetenceEnd = checkup.DateStart - checkup.DateEnd;
                                totalLifeCheckUpProgress = difetenceEnd.ToString();
                                Debug.WriteLine(totalLifeCheckUpProgress);
                            }
                        }
                    }

From what I understand this value being returned 01/01/0001.00:00:00 should be returned only if the value is null or less than 01/01/1970

but the value of the camnpo where I am rescuing is not null nor me nor nor me nor what 01/01/1970

the data type in the database is as smalldateTime

what I can is wrong ?

  • 1

    Dbnull.Value will give true if it has value, such that it will put your min there, you are confusing the ternary

  • @Lucasmiranda Really, I was confusing Thanks for the tip.

  • @Gabrielsilva Have you found a solution? If you couldn’t send an DER of the table that is being executed and more information

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.