I have a code that checks if a record exists:
using (var connection = new NpgsqlConnection(_connectionString))
{
await connection.OpenAsync();
var query = @"
SELECT COUNT(1)
FROM TimeEntries
WHERE EmployeeId = @EmployeeId AND Date::date = @Date";
using (var command = new NpgsqlCommand(query, connection))
{
command.Parameters.AddWithValue("@EmployeeId", timeEntry.EmployeeId);
command.Parameters.AddWithValue("@Date", timeEntry.Date.Date);
var count = (long)await command.ExecuteScalarAsync();
if (count > 0)
{
throw new InvalidOperationException("Pracownik już ma zarejestrowane godziny pracy w tej dacie.");
}
}
and i send there request like:
{
"date": "2024-12-29T21:42:58.251Z",
"hoursWorked": 0
}
and id in a query.
The problem is that it doesnt work.
The data in a table are as follow:
"id" "employeeid" "date" "hoursworked"
7 2 "2024-12-29" 8.00
I thought it doesnt work because here:
command.Parameters.AddWithValue("@Date", timeEntry.Date.Date);
the value of a parameter is 29.12.2024 00:00:00 instead of 29.12.2024 but the sql query:
SELECT COUNT(1)
FROM TimeEntries
WHERE EmployeeId = 2 AND Date::date = '2024-12-29 00:00:00';
return the proper value of 10, but in code the count is always 0...
It works with:
var data = new DateTime(timeEntry.Date.Year, timeEntry.Date.Month,
timeEntry.Date.Day, 0, 0, 0);
command.Parameters.AddWithValue("@EmployeeId", timeEntry.EmployeeId);
command.Parameters.AddWithValue("@Date", data);
but still i dont understand why previous version didnt work...
timeEntry(using theImmediate WindoworWatch Window) and include those in the question. Do not guess what the values are. Use the debugger.CREATE TABLEscript for TimeEntries .timeEntry.Date.Date.Kind