Yea, it’s been over a year since I updated this blog, but spending 15-20 minutes finding a solution to something so simple got a bit annoying so here’s the solution for future searchers!
If you run executescalar it returns the first column/result. The problem is if you don’t have a result it returns nothing.
Keyword: nothing
Yes you’re working with a database so you would assume it’s DBNull, but if there is an empty table SQL is not going to return DBNull, but will return nothing.
Solution:
Dim sqlResult as object = sqlCMD.executescalar()
if sqlResult is nothing then
‘ No result found
else
sqlResult.tostring ‘result found
end if
Simple!