I am using datetime2
as the datatype for checkIn
and checkOut
. Previously I can add into database with this code.
//value for checkIn = 12/25/2015 2:00:00 PM
checkIn = DateTime.ParseExact(Session["checkInDate"].ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture).AddHours(14);
//value for checkOut = 12/26/2015 12:00:00 PM
checkOut = DateTime.ParseExact(Session["checkOutDate"].ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture).AddHours(12);
strInsert = "INSERT INTO Reservation ( checkInDate, checkOutDate) VALUES (@checkInDate, @checkOutDate)";
cmdInsert = new SqlCommand(strInsert, conn);
cmdInsert.Parameters.AddWithValue("@checkInDate", checkIn);
cmdInsert.Parameters.AddWithValue("@checkOutDate", checkOut);
But now it doesn't work and I am getting this error;
"Conversion failed when converting date and/or time from character string".
I think the error is caused by the check in value which contain the "PM" and "AM", but it is weird because previously I am able to add this into the database.
Does anybody know how to fix this?