web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Object reference not set to an instance of an object.

(0) ShareShare
ReportReport
Posted on by

Hi All,

I'm fairly new on CRM I'm trying to insert records using IOrganization Service and I'm getting the error - Object reference not set to an instance of an object. look at my code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using Microsoft.Xrm.Tooling.Connector;
using NUnit.Framework;
using System.Configuration.Assemblies;
using System.ServiceModel.Description;

namespace JSE_CRM_MDS_Integration_Automation1
{
    public class CRM
    {

        private OrganizationServiceProxy _serviceProxy;
        private IOrganizationService _orgService;

        // Define the IDs needed for this sample.
        private Guid _accountId;


        public static void Main(string[] args)
        {
            CRM crm = new CRM();
            crm.Run();


        }
       
        public void Run()
        {
            //try
            //{
            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyCRMServer"].ConnectionString;
            string strOrganizationUri = System.Configuration.ConfigurationManager.AppSettings["MyUrI"];
            var credentials = new ClientCredentials();
            credentials.UserName.UserName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
            credentials.UserName.Password = System.Configuration.ConfigurationManager.AppSettings["Password"];
            var organizationUriIFD = new Uri(strOrganizationUri);
            //In-case I have organization service issue being null
            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
            //Use the connection string named "MyCRMServer from the configuration file "  
            CrmServiceClient crmSvc = new CrmServiceClient(System.Configuration.ConfigurationManager.ConnectionStrings["MyCRMServer"].ConnectionString);
            Console.WriteLine("Connection String Found on App.Config!!");
            //Wrapping connString to the CrmServiceClient Object
            CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(connectionString);
            _orgService = crmSvc.OrganizationWebProxyClient != null ? crmSvc.OrganizationWebProxyClient(IOrganizationService)crmSvc.OrganizationServiceProxy;
            _orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
            Console.WriteLine("Succesfully Connected to the IOrganization Service!!");
            CreateRequiredRecords();
         
            Entity account = new Entity("account");
            account.Attributes["AccountId"] = "141da83-3ee7-e811-80f3-005056b546bz";
            account.Attributes["RoleTypeId"] = "2";
            account.Attributes["RoleStatusId"] = "2";
            // Create an account record named account.
        _accountId = _orgService.Create(account);
 
            Console.Write(_accountId);
            //Console.WriteLine("{0} {1} {2} created,", account.LogicalName);
            Console.Write("Account created succesfully");

            //}
            //catch (Exception err)
            //{
            //err.ToString();
            //}
        }

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    The failure is upon this line _accountId = _orgService.Create(account); am I missing something here? is my logic correct?

    also examine my Connection String on App.config see below: let me know if anything is missed

     <add name="MyCRMServer" connectionString= "AuthType=AD;Url=tb51entcrmqa.crm.tb51.test.jse.co.za/main.aspx;"/>

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I see several errors:

    1. Connection string should not contain main.aspx. Check this docs.microsoft.com/.../mt608573(v=crm.8) to build proper url.

    2. You have 2 instances of CrmServiceClient - crmSvc and conn. What the point? Choose one, remove second.

    3. I'm pretty sure that you get an error because connection string is wrong so your service is actually not instantiated (and this is why you get null reference pointer exceptions). After you called CrmServiceClient constructor check IsReady property and continue code execution only when it is equal to true.

    4. Code that you use for filling of properties is incorrect as well. Here is how it should look like:

    Entity account = new Entity("account");

    //if you want to set Id of record use Id property instead of AccountId Attribute and this field is of Guid type, not string

    account.Id = new Guid("141da83-3ee7-e811-80f3-005056b546bz");//account.Attributes["AccountId"] = "141da83-3ee7-e811-80f3-005056b546bz";

    //access attributes using lower case. always. I'm not sure what type of this fields but I don't think it's string so doublecheck that

    account["roletypeid"] = "2";//account.Attributes["RoleTypeId"] = "2";

    //access attributes using lower case. always. I'm not sure what type of this fields but I don't think it's string so doublecheck that

    account.Attributes["RoleStatusId"] = "2";

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans