I am trying to implement CoolStorage in my project, but am running into an issue when using date parameters. When I pass in null as the date parameter it adds the record successfully, but if I pass an actual date through I get an error when calling .Save() to write the new record to the database. I have got the (Activa) CoolStorage source code and have found the point where it fails, but can't figure how to fix it.
The code where it fails is as follows (specifically the dbCommand.ExecuteNonQuery()
line):
internal int ExecuteNonQuery(string sqlQuery, CSParameterCollection parameters)
{
long logId = Log(sqlQuery, parameters);
IDbCommand dbCommand = CreateCommand(sqlQuery, parameters);
try
{
dbCommand.ExecuteNonQuery();
return 1;
}
catch (InvalidOperationException)
{
return -1;
}
finally
{
LogEnd(logId);
}
}
The sqlQuery contains:
insert into [Schedule] ([TaskID],[StartTime],[MondayYn],[TuesdayYn],[WednesdayYn],[ThursdayYn],[FridayYn],[SaturdayYn],[SundayYn],[DefaultSysuserID],[DefaultTeamID],[ActiveYn])
values (@P17,@P18,@P19,@P20,@P21,@P22,@P23,@P24,@P25,@P26,@P27,@P28)
@P18 contains the offending date, which is a CSParameter object with a value of:
{17/12/2012 18:52:44}
Also, the database being used is Access. The error that's thrown is a OldDbException which reads {"Data type mismatch in criteria expression."}.
Can anyone offer any advice on how to resolve this?
EDIT: The Schedule StartTime field is defined in the Schedule table as Date/Time. I need it to have its required propetry set to True, but have disabled in order to test adding records by leaving StartTime out. The parameters are being applied via the CoolStorage classes as follows:
Schedule schedule = Schedule.New();
schedule.TaskID = task.TaskID;
schedule.StartTime = DateTime.Now;
schedule.MondayYn = true;
schedule.TuesdayYn = true;
schedule.WednesdayYn = true;
schedule.ThursdayYn = true;
schedule.FridayYn = true;
schedule.SaturdayYn = false;
schedule.SundayYn = false;
schedule.DefaultSysuserID = sysuser.SysuserID;
schedule.DefaultTeamID = sysuser.SysuserTeams.First().TeamID;
schedule.ActiveYn = true;
schedule.Save();
When I comment out the Schedule.StartTime = DateTime.Now line I can successfully add records, otherwise I get the error described above. I cannot change the format to text without editing my ORM mapping class, which will no doubt cause errors elsewhere. I guess I could alter the CoolStorage DataProvider class, but I'm assuming that this shouldn't be necessary?
EDIT2: As a test I intercepted the SQL posted above to remove the @P18 reference and hard code the date in its place and the record added correctly:
insert into [Schedule] ([TaskID],[StartTime],[MondayYn],[TuesdayYn],[WednesdayYn],[ThursdayYn],[FridayYn],[SaturdayYn],[SundayYn],[DefaultSysuserID],[DefaultTeamID],[ActiveYn])
values (@P17,#2012-12-01 12:00:00#,@P19,@P20,@P21,@P22,@P23,@P24,@P25,@P26,@P27,@P28)
I also tried modifying the value of the parameter by casting it as a string formatted as #yyyy-MM-dd hh:mm:ss# however I still received the Data type mismatch in criteria expression error.
EDIT3: I have fixed it by following Abhishek's (edit - also Dean's) suggestion of converting the DateTime to a string by amending the CSParameterCollection class as follows. Hopefully this won't cause problems if I decide to use a different database but it's fixed it for Access:
public CSParameter this[string name]
{
get
{
CSParameter parameter;
_parameterMap.TryGetValue(name, out parameter);
if (parameter.Value.GetType().Equals(typeof(DateTime)))
{
DateTime date = (DateTime)parameter.Value;
string dateString = date.ToString("yyyy-MM-dd hh:mm:ss");
parameter.Value = dateString;
}
return parameter;
}
}
I strongly feel that the problem is because the parameters are not in the same order when you are adding them to the parameter collection.
Make sure that they are in the same order and this applies to both sql statements or stored procedures.
ADO.NET does not support named parameters when using an OLEDB provider, and since you are connecting to an Access DB, you are actually using an OLEDB provider. So the order of the parameters does matter.
If they are in order and it's still not working, then I think that it might be an issue with the DateTime. Try converting it to string before adding it as a parameter.