Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

an unhandled exception of type system.nullreferenceexception occurred in Microsoft.XRM.Tooling.Connector.dll

Posted on by Microsoft Employee

string crmConnectionString = ConfigurationManager.AppSettings["CRMConnectionString"];
CrmServiceClient service = new CrmServiceClient(crmConnectionString);
WhoAmIResponse whoAmIResponse = (WhoAmIResponse)service.Execute(new WhoAmIRequest());

QueryExpression query_Contacts = new QueryExpression("contact");
query_Contacts.ColumnSet = new ColumnSet("Accountname", "Fullname");
EntityCollection coll_Contacts = service.RetrieveMultiple(query_Contacts);

Can anyone tell me why I am getting error : 

an unhandled exception of type system.nullreferenceexception occurred in Microsoft.XRM.Tooling.Connector.dll

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: an unhandled exception of type system.nullreferenceexception occurred in Microsoft.XRM.Tooling.Connector.dll

    Hello All,

    As per latest Microsoft recommendation we are not supposed to use “AuthType=Office365”.

    docs.microsoft.com/.../authenticate-office365-deprecation

    We can use Application Account (Client ID & Secret Key) to generate Token & access Dynamics CRM Organization Service. But if you want to use User ID & PWD then Use (AuthType = OAuth)

    Sample Code :

    string connectionString = "AuthType = OAuth; Url = 'https://*****.crm.dynamics.com'; Username = '*******'; Password = '*******'; AppId = 51f81489-12ee-4a9e-aaae-a2591f45987d; RedirectUri = app://58145B91-0C36-4500-8554-080854F2AC97;LoginPrompt=Never";

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    CrmServiceClient crmServiceClient = new CrmServiceClient(connectionString);

    WhoAmIResponse whoAmIResponse = crmServiceClient.Execute(new WhoAmIRequest()) as WhoAmIResponse;

    Note : While trying to use this from Azure Function I got below errors :

    1) ERROR REQUESTING Token FROM THE Authentication contextNeed a non-empty authority

    2) One or more errors occurred. => An error occurred while sending the request. => The underlying connection was closed: An unexpected error occurred on a send. => Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. => An existing connection was forcibly closed by the remote hostERROR REQUESTING Token FROM THE Authentication context

    3) CurrentAccessToken = 'crmServiceClient.CurrentAccessToken' threw an exception of type 'System.NullReferenceException'

    Easily you can resolve these using just one additional line  :    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    Ref URL : support.microsoft.com/.../4051700  

    Microsoft Dynamics 365 Customer Engagement (online) to require TLS 1.2 for connectivity

    Pls let me know if you are facing any other issues.

    Thanks,

    Sumit

  • Jaafar Profile Picture
    Jaafar 5 on at
    RE: an unhandled exception of type system.nullreferenceexception occurred in Microsoft.XRM.Tooling.Connector.dll

    Hi Suchisuchi , Can you please guide on how to fix the issue as im getting the same eerror messssage. I am using a console app to connect to D365. with below code

              System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

               //Authenticate

               // connectionString is the URL of our Dynamics 365 API, username,password and authentication type(office 365)

               string connectionString = "AuthType = Office365; Url = vanillaenterprises.crm8.dynamics.com; Username = jaafar@vanillaenterprises.onmicrosoft.com;Password = Vanilla@123";

               //Create object for CRMServiceClient class

               CrmServiceClient service = new CrmServiceClient(connectionString);

               //Create contact record in Dynamics 365

               Entity contact = new Entity("contact"); // pass the logical name of entity

               contact.Attributes.Add("firstname", "Integrated");

               contact.Attributes.Add("lastname", "Console App");

               //Involke create method and pass the contact object , it will give us GUID, take everything into the GUID

               // Create the contact in Microsoft Dynamics CRM.

                   Guid contactGuid = service.Create(contact);

                   //Print the GUID

                   Console.WriteLine(contactGuid.ToString());

                   //stop the console

                   Console.Read();

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: an unhandled exception of type system.nullreferenceexception occurred in Microsoft.XRM.Tooling.Connector.dll

    Hi Can you please guide what was the issue as I am getting the same error message. I am using a console app to connect to D365.

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: an unhandled exception of type system.nullreferenceexception occurred in Microsoft.XRM.Tooling.Connector.dll

    Based on error you show user doesn't have privileges to create accounts. Validate his security roles please.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: an unhandled exception of type system.nullreferenceexception occurred in Microsoft.XRM.Tooling.Connector.dll

    Thanks for your reply. Thanks from bottom of my heart, I was able to resolve the issue, there was minor mistake in C# coding.. Thanks a lot for your time and help,

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: an unhandled exception of type system.nullreferenceexception occurred in Microsoft.XRM.Tooling.Connector.dll

    I believe your problem is connection string it should look like

    <add key="CRMConnectionString" value="AuthType=Office365; Url=https://**************.dynamics.com/; Username=Za.aa.com; Password=XX;" />

    and not like

    <add key="CRMConnectionString" value="AuthType='Office365'; Url='https://**************.dynamics.com/'; Username='Za.aa.com'; Password='XX';" />

    Fix it and try it again. Also please don't create duplicated threads.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: CRM connection from C# windows

    Hello,

    Check following article - docs.microsoft.com/.../sample-quick-start-xrm-tooling-api

    It should help you. Also if you want to get help you should provide what's your scenario, what code and references you've used and what errors do you get.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    CRM connection from C# windows

    Can anyone please give me one error free code for connecting to CRM through C# Windows form, I tried everything from net. Noways are helping me to connect. It throws so many errors that I got fed up.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: an unhandled exception of type system.nullreferenceexception occurred in Microsoft.XRM.Tooling.Connector.dll

    <Server>", "<Port>", "<OrgName>");

    What are these information, Mine is CRM online, i wont know servername, so not sure how to try that, Ravi

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: an unhandled exception of type system.nullreferenceexception occurred in Microsoft.XRM.Tooling.Connector.dll

    Hi Suchisuchi,

    Could you please try this SDK sample (Example 2) and see if it works. Notice the check "if (crmSvc != null && crmSvc.IsReady)" and else part to display the message if there is a problem in connection.

    msdn.microsoft.com/.../dn688174.aspx

    Basically you need to isolate and find if there is a problem in connecting to CRM or executing the retrieve multiple for contact. Break your code to just return the WhoAmI response first and see if that works or not.

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans