-2

I am getting an above exception while converting datetime to varchar

if (columnDefs[i].IndexOf("Date") > -1)
{
    // Date search
    subQuery += columnDefs[i] + 
                " = '%" + 
                DateTime.Parse(searchValues[i]).ToString("dd/MM/yyyy") + 
                "'" + " and ";
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
prathamesh93
  • 1
  • 1
  • 5

1 Answers1

0

When working with SQL do not hardcode but paramterize queries; assuming that you work with MS SQL which has @parameter_name syntax:

if (columnDefs[i].IndexOf("Date") >= 0)
{
    // Date search: we a parameter, not its value into the query
    subQuery += $" ({columnDefs[i]} = @prm_{columnDefs[i]}) and ";
}

...

// when executing the query
using (var myQuery = new SqlQuery(subQuery, myConnection)) {
  // we set parameters' values
  for (int i = 0; i < columnDefs.Count(); ++i) {
    if (columnDefs[i].IndexOf("Date") >= 0) {
      //TODO: check the actual parameter value type (SqlDbType.DateTime?) 
      var prm = new SqlParameter($"@prm_{columnDefs[i]}", SqlDbType.DateTime);
      //TODO: check actual string format (d/M/yyyy?)
      prm.Value = DateTime.ParseExact(
         searchValues[i], 
        "d/M/yyyy", 
         CutureInfo.InvariantCulture);

      myQuery.Parameters.Add(prm);
    }
  }

  ...
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215