title: "Data type mismatch" when using date Parameters
body: <p>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.</p>
  
  <p>The code where it fails is as follows (specifically the <code>dbCommand.ExecuteNonQuery()</code> line):</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>The sqlQuery contains:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>@P18 contains the offending date, which is a CSParameter object with a value of:</p>
  
  <pre><code>{17/12/2012 18:52:44}
  </code></pre>
  
  <p>Also, the database being used is <strong>Access</strong>. The error that's thrown is a OldDbException which reads <em>{"Data type mismatch in criteria expression."}</em>.</p>
  
  <p>Can anyone offer any advice on how to resolve this?</p>
  
  <p><strong>EDIT:</strong> 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:</p>
  
  <pre><code>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();
  </code></pre>
  
  <p>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?</p>
  
  <p><strong>EDIT2:</strong> 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:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>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.</p>
  
  <p><strong>EDIT3:</strong> 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:</p>
  
  <pre><code>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;
      }
  }
  </code></pre>