Why am I getting this error during design time?

Error Number 5: Key cannot be null

If Me.Visible = True Then
            If RetrieveSubjectsAndSection(Username, Subject) = True Then
            End If
        End If

RetrieveSubjectsAndSection function checks if record exist. The code is written inside a Usercontrol. Also I have not ran the program yet.

Everytime I move the Usercontrol on the form, the error pops up. As if it is trying to connect.

The Visual Studio IDE uses reflection to render user controls so it is compiling and loading the control which runs the code in the constructur and user control's load event.

Take a look at this article on detecting if the code is being run from a designer:

I found the solution, thanks snake.

If Me.DesignMode = False then
'control/form is in Runtime
end if