Originally Posted by
jmcilhinney
While your code may work, it's not optimal. First, there's no point using HasRows and While. You know for a fact that there can never be more than one record returned because you are specifying it in the query. If you test HasRows and it is True then you know for a fact that there is exactly one row, so what's the loop for? If you use While then that will handle both possible cases, i.e. zero rows and one row, so what's the HasRows check for? Just use one or the other.
That said, you shouldn't be using a data reader at all. The ExecuteScalar method is specifically intended for queries that return a single value, which yours does.