I'm running this query on SQL Server 2008 using ODBC. If I take out the last part (OR li.DATEGENERATED >= (...))) then I don't get the error... but I can run it in SSMS just fine.
SELECT COUNT(*) FROM dbo.POITEMST li
WHERE (? IS NULL OR li.DATEGENERATED >= ?)
AND (? IS NULL OR (li.DATEGENERATED >= (
SELECT MAX(t.DATEGENERATED) FROM dbo.POITEMST t WHERE t.PONUM = ?)))
Here is how I'm setting up parameters
DbParameter startDateParam = cmd.CreateParameter();
startDateParam.ParameterName = "StartDate";
startDateParam.Value = (StartDate.HasValue ? (object)StartDate.Value : (object)(new DateTime(1753, 2, 2)));
cmd.Parameters.Add(startDateParam);
startDateParam = cmd.CreateParameter();
startDateParam.ParameterName = "StartDate";
startDateParam.Value = (StartDate.HasValue ? (object)StartDate.Value : (object)(new DateTime(1753, 2, 2)));
cmd.Parameters.Add(startDateParam);
DbParameter startPoParam = cmd.CreateParameter();
startPoParam.ParameterName = "StartPO";
startPoParam.DbType = DbType.String;
startPoParam.Value = (string.IsNullOrEmpty(StartPurchaseOrder) ? (object)DBNull.Value : (object)StartPurchaseOrder);
cmd.Parameters.Add(startPoParam);
startPoParam = cmd.CreateParameter();
startPoParam.ParameterName = "StartPO";
startPoParam.DbType = DbType.String;
startPoParam.Value = (string.IsNullOrEmpty(StartPurchaseOrder) ? (object)DBNull.Value : (object)StartPurchaseOrder);
cmd.Parameters.Add(startPoParam);
EDIT: Those dates are only in there because I was trying to figure this out... it was passing in DBNull before if there wasn't a start date.