If you are getting Object reference not set to an instance of an object or Unable to Login to Dynamics CRMObject reference not set to an instance of an object. When connecting to Dynamics 365 version 9 or higher it is probably because of a change to use TLS1.2.
The screenshot below shows the CRM login error that is caused by not using the correct TLS version.
What is TLS
TLS is a protocol called “Transport Layer Security". Its purpose is to protect messages communicated over HTTP and TCP.
The documentation out there is saying to resolve the issue simply use .Net Framework 4.6 or higher.
The second way to resolve the problem is to add the following line of code
The full working code to save a contact in Dynamics 365 version 9 or higher looks like this. The important lines are line 4, importing the System.Net namespace and line 18, enforcing the code to use TLS1.2.
Personally, I am finding that changing the Visual Studio project to .Net Framework 4.6 or higher is not resolving the problem. Please leave a comment If you know why using the latest versions of the .Net Framework is not working, I am keen to know and would be very appreciative.
For further understanding you may want to read the following articles from Microsoft
Preparing for the mandatory use of TLS 1.2 in Office 365 - https://support.microsoft.com/en-us/help/4057306/preparing-for-tls-1-2-in-office-365
Solving the TLS 1.0 Problem - https://www.microsoft.com/en-us/download/details.aspx?id=55266