I have a query that should always be returning a single int. I have now logged it returning a string entirely unrelated to what it should be.
We\'ve been ge
There could be more than one WebSite table. Can you qualify the table with schema name:
SELECT isnull(MAX(PkID),0) FROM YourSchema.WebSite WHERE StoreID = @StoreID AND WebSiteStatusID = @WebSiteStatusID
The ExecuteScalar() function's return type is object
, and you declare your result variable with the var
keyword. That's not really a good combination, because you're putting a lot of pressure on the system to get the type inference right.
Is there any commonality to when it fails to return an int?
Since your query only ever returns a single column in a single row, what do you get if you use a more typesafe ExecuteReader and take the first column's value?
Is it always returning a row? If the WHERE
clause causes it to return no rows (say your parameters are not what you think they are), your ISNULL
doesn't come into effect - there are no rows at all, and ExecuteScalar is supposed to return a NULL.