Isuru,
In your initial code, what codition your code executed, if or else? What are the values you receive in ComandLine?
About your second question:
No, you don't.
If you connect correctly the application object, when you get the company object , the object will already connected to the company.
Best regards,
Diego Lother