I'm working on a C# console application project. Trying to insert a DateTime
value into SQL Server 2008 and this is working good. In case if my code fails I want to insert the default DateTime
to the table in database.
When I tried to insert I got an error.
SqlTypeException was unhandled:
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
Below I have pasted the specific code.
try
{
...
return DateTime.Now;
}
catch (WebException ex)
{
...
return default(DateTime);
}
Code I used to insert details in the LOGS table:
connection.Open();
for (int i = 0; i < LogList.Count; i++)
{
string aprocessLogQuery = "INSERT INTO PROCESS_LOGS VALUES (@fileId, @startTime, @endTime, @transferredTime)";
command = new SqlCommand(aprocessLogQuery, connection);
command.Parameters.Add("fileId", SqlDbType.Int).Value = LogList[i].fileId;
command.Parameters.Add("startTime", SqlDbType.DateTime).Value = LogList[i].fileGeneration_StartDateTime;
command.Parameters.Add("endTime", SqlDbType.DateTime).Value = LogList[i].fileGeneration_EndDateTime;
command.Parameters.Add("transferredTime", SqlDbType.DateTime).Value = LogList[i].fileTransferred_DateTime;
dataReader.Close();
dataReader = command.ExecuteReader();
}
connection.Close();
The default value of DateTime
is 01/01/0001 12:00:00 AM
, therefore it is not working. Can anyone please suggest alternate working solution?
Thanks