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,
. Even though you are using C#, you can still access the method by adding a reference to
. (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();
int maxPVRNo = Convert.ToInt32(result);
if (maxPVRNo == 0)
if (maxPVRNo == 1)
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
would return is
, so you can catch that as well.