I have a number of methods doing next:
var result = command.ExecuteScalar() as Int32?;
if(result.HasValue)
{
return result.Value;
}
else
{
throw new Ex
If you just want an exception when the returned value isn't an Int32 then do this:
return (int)command.ExecuteScalar();
If you want to throw your own custom exception then I'd probably do something like this instead:
int? result = command.ExecuteScalar() as int?;
if (result == null) throw new YourCustomException();
return result.Value;