If you can alter the query, Pete's solution is the best. If you cannot, then you will need to add some additional tests.
VB has a very useful function,
IsNumeric
. Even though you are using C#, you can still access the method by adding a reference to
Microsoft.VisualBasic
. (There is probably a C# equivalent, but I don't know what it is. Hey, a .Net method is a .Net method.) So you might want to try this:
SqlCommand dbCommand = new SqlCommand();
dbCommand.CommandText = "SElECT MAX(PVRID) FROM PVR";
object result = dbCommand.ExecuteScalar();
if (Microsoft.VisualBasic.Information.IsNumeric(result))
{
int maxPVRNo = Convert.ToInt32(result);
if (maxPVRNo == 0)
{
}
if (maxPVRNo == 1)
{
}
}
else
{
}
}
What you are doing here is making the conversion to an integer after you have determined that the result is, in fact, a number. The only non-numeric value that
MAX
would return is
null
, so you can catch that as well.