It's stated in the documentation that ExecuteScalar
returns the first column of the first row in the result set, or a null reference (Nothing
in Visual Basic) if the result set is empty.
So here you need to compare the result with Nothing (and also DBNull, just to be on the safe side), for example:
Using connection As New SqlConnection(My.Settings.MyConnectionString)
Using command As New SqlCommand("SELECT Id From Table1 WHERE Id = -1", connection)
connection.Open()
Dim m_result As Object = command.ExecuteScalar()
If (m_result Is Nothing Or m_result Is DBNull.Value) Then
MessageBox.Show("None") 'Return value was null
Else
MessageBox.Show(Convert.ToInt32(m_result)) 'There was a return value
End If
End Using
End Using