I’m trying to execute parameterized SQLCommand, which should return 1 value. Instead, it return an empty set, no exception is thrown, the program just keeps going.
This is the query:
SELECT SUM(s.cost) AS cost
FROM Spending s JOIN Date d ON s.dateID = d.dateID
JOIN [Service category] sc ON s.[service categoryID] = sc.[service categoryID]
WHERE sc.category1 = 'Phone' AND s.personID = @person
AND d.month = @month AND d.year = @year
C# code is as follows:
SqlCommand commandSelect = new SqlCommand(query, conn);
foreach (string[] p in pars)
{
commandSelect.Parameters.AddWithValue("@" + p[0], p[2]);
}
SqlDataReader res = commandSelect.ExecuteReader();
where there are 3 parameters passes and when I check SQLcommand in the debugger, they are passed correctly. @person is a string and the other two are integers.
Any help would be appreciated.
EDIT:
I probably should also add the stucture of pars array:
pars[0][0] = "person"
pars[0][2] = "'IDString here'"
pars[1][0] = "month"
pars[1][2] = "2"
pars[2][0] = "year"
pars[2][2] = "2012"
Well, looks like the problem was that addWithValue() casts strings to NVARCHAR, which I believe caused trouble in WHERE clause, because my field was of VARCHAR type. Explicit typecasting solved the problem.