I have a query that works in Access perfectly, but does not seem to work in C#. I am pretty sure it is related to the parameters I am adding because if I hard code dates surrounded by '#' symbols then it works fine. Does anyone know what could be the solution here? Thanks Ahead
string queryAccessNewHires =
@"SELECT cdate([Date of Hire]), Count([SSN]) AS DateCount
FROM [FED]
WHERE
IIf(
IsNull([Date of Hire]), False,
IIf([Date of Hire] <> 'DoesNotApply'
AND [Date of Hire] <> ""
AND CDate([Date of Hire]) > CDate(PBP)
AND CDate([Date of Hire]) < CDate(PBE), True, False
)
)
GROUP BY [Date of Hire]
ORDER BY [Date of Hire]";
OleDbCommand cmdNewHires = new OleDbCommand(queryAccessNewHires, conn);
cmdNewHires.Parameters.AddWithValue("PBP", tbTYB.Text);
cmdNewHires.Parameters.AddWithValue("PBE", tbTYE.Text);
EDIT:: Thank you for the help. Instead of receiving the "Syntax error in query expression" error, it seems to be understanding what I want when I change the code. However the new error I am receiving is: "The OleDbParameterCollection only accepts non-null OleDbParameter type objects, not String objects"
string queryAccessNewHires = @"SELECT cdate([Date of Hire]), Count([SSN]) AS DateCount
FROM [FED]
WHERE IIf(IsNull([Date of Hire]),False,
IIf([Date of Hire]<>'DoesNotApply'
AND [Date of Hire]<>""
AND CDate([Date of Hire])>CDate(?)
AND CDate([Date of Hire])<CDate(?), True, False))
GROUP BY [Date of Hire]
ORDER BY [Date of Hire]";
OleDbCommand cmdNewHires = new OleDbCommand(queryAccessNewHires, conn);
cmdNewHires.Parameters.Add(tbTYB.Text);
cmdNewHires.Parameters.Add(tbTYE.Text);
EDIT: Still no solution found. I am thinking that I may have possibly made this harder than it needs to be so I want to show you guys the original query that I want to run from C#. [Date of Hire] is a varchar and needs to be converted. I cannot change the design. This query returns "Invalid use of null." I do not understand why this query does not execute.
SELECT cdate([Date of Hire]), Count([SSN]) AS DateCount
FROM [FED]
WHERE (CDate([Date of Hire]) > #1/1/2013#
AND CDate([Date of Hire]) < #12/31/2013#)
GROUP BY [Date of Hire]
ORDER BY [Date of Hire]