What code I should add to accept null from WHERE statement.
{
int numApprovals = 0;
string sql = \"SELECT COUNT(Type) AS OpenforApproval \" +
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;