I have a query that was built and works using SQL Developer. When I use the same query in an Oracle DataReader object, I receive ORA-01858: a non-numeric character was found where a numeric was expected
If I remove the clause to check the CRTDDATE column, the query works in the DataReader.
query:
SELECT count(distinct(H.id)) AS Completed, T.Cat, 'Task' as Type
FROM HISTORY H
INNER JOIN Tasks T ON H.id = T.id
WHERE H.Step In ('1.41', '1.61', '6.41', '6.61')
AND T.Cat = :cat
and H.CRTDDATE >= :sdate and H.CRTDDATE <= :edate
GROUP BY T.Cat, 'Task'
Code:
using (OracleConnection conn = new OracleConnection(ConnectionString))
{
OracleCommand cmd = new OracleCommand(query, conn);
cmd.Parameters.Add("sdate", startDate);
cmd.Parameters.Add("edate", endDate);
cmd.Parameters.Add("cat", cat);
await conn.OpenAsync();
using (var dr = await cmd.ExecuteReaderAsync())
{
if (dr.HasRows)
{
while (await dr.ReadAsync())
{
var report = new IPTCompletedReport();
var count = dr.GetString(0);
report.Completed = 0;
report.IPT = dr.GetString(1);
report.Type = dr.GetString(2);
results.Add(report);
}
}
}
}
Values:
startDate = {1/1/2021 12:00:00 AM}
endDate = {8/17/2022 12:00:00 AM}
cat = "DRV"
The error occurs at this line: using (var dr = await cmd.ExecuteReaderAsync())
How can I change the query to allow the DataReader to accept it? Should I use a DataAdapter instead?
I have several other queries and DataReaders in this file that are functioning properly. Most of them have where
clauses featuring date checks.
Using the comment from @madreflection, I added cmd.BindByName = true
and that fixed my problem.
Thanks to everyone who provided suggestions.