tags:

views:

423

answers:

3

What code I should add to accept null from WHERE statement.

{
    int numApprovals = 0;
    string sql = "SELECT COUNT(Type) AS OpenforApproval " +
       "FROM dbo.LeaveRequest " +
       "WHERE Type IN (2, 3, 4, 5, 6, 8, 13, 14, 16, 22) " +
       "GROUP BY MgtApproval " +
       "HAVING MgtApproval IS NULL";
       //"SELECT COUNT(EffectiveDate) AS OpenforApproval FROM LeaveRequest GROUP BY TimeStampApproval HAVING (TimeStampApproval IS NULL)";

    using (cn = new SqlConnection(ConnectionString()))
    {
        cn.Open();
        using (cmd = new SqlCommand(sql, cn))
        {
            cmd.CommandType = CommandType.Text;
            numApprovals = (int)cmd.ExecuteScalar();
        }
    }

    return numApprovals;
}
+5  A: 

Just:

WHERE Type IN (2, 3, 4, 5, 6, 8, 13, 14, 16, 22) OR Type IS NULL

But I'm not at all convinced that's what you really want, or the cause of the problem.

If you're getting an exception in the C# code, it's not going to be from the where clause.

I'm concerned by the fact that your connection seems to be reusing an existing variable, by the way. Bad idea. It should almost certainly be a local variable. You can also make your code simpler by returning from the middle of it:

string sql = ...;

using (var cn = new SqlConnection(ConnectionString()))
{
    cn.Open();
    using (cmd = new SqlCommand(sql, cn))
    {
        cmd.CommandType = CommandType.Text;
        return (int) cmd.ExecuteScalar();
    }
}

If the problem is as Jamie says, that ExecuteScalar is returning null, the easiest way of getting round that is to cast it to a nullable int and use the null coalescing operator:

return (int?) cmd.ExecuteScalar() ?? 0;
Jon Skeet
sqlexception: incoreect syntax near 'nullgroup'."WHERE Type IN (2, 3, 4, 5, 6, 8, 13, 14, 16, 22) OR Type IS NULL" + "GROUP BY MgtApproval " +
Kombucha
You haven't put a space after "NULL".
Jon Skeet
i.e. you should have "... IS NULL " + "GROUP BY ..." (or use a verbatim string literal to make this easier)
Jon Skeet
Jon: sorry to disagree with the SO God, but why would this fix it? The HAVING clause removes the zero count because it's a post aggregate filter, no?
gbn
Hi Jon,Thank you sO much. It works really good. As simple at that huh? Thanks again. :)
Kombucha
gbn: It's not clear what was actually wrong in the end - whether all that was required was to include items where the type was null, or whether it was the null-coalescing bit.
Jon Skeet
Since the goal is to get a count, I think it's more appropriate to return 0 instead of int? in this case. A count of null doesn't make any sense.
Jamie Ide
@Jamie: Indeed, which is why my code *does* return an int, with 0 if the result is null. That's the beauty of the null coalescing operator.
Jon Skeet
@Jon: I thought your solution would require int? as the method return type. I tried it myself then read the docs and realized this is a special feature of the null coalescing operator: "The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type."
Jamie Ide
Yes - basically the overall type of the expression is the type of the second operand. It's very useful.
Jon Skeet
+1  A: 

The problem is probably the direct cast to int. This throws an exception if cmd.ExecuteScalar() returns null. You need to decide what to return in that case. For this example I am returning 0 if cmd.ExecuteScalar() returns null

using (cn = new SqlConnection(ConnectionString()))
{
    cn.Open();
    using (cmd = new SqlCommand(sql, cn))
    {
        cmd.CommandType = CommandType.Text;
        object result = cmd.ExecuteScalar();
        numApprovals = result == null ? 0 : (int)result;
    }
}

return numApprovals;
Jamie Ide
Only because no rows are coming back because of the HAVING clause. However, your idea is correct but it may still fail the WHERE clause and return no rows anyway
gbn
+1  A: 

As an aside, it's much easier/clearer to format multi-line strings as:

string sql = 
@"SELECT COUNT(Type) AS OpenforApproval
FROM dbo.LeaveRequest
WHERE Type IN (2, 3, 4, 5, 6, 8, 13, 14, 16, 22)
GROUP BY MgtApproval
HAVING MgtApproval IS NULL";
mancaus