locked
An unhandled exception of type 'Microsoft.Xrm.Sdk.SaveChangesException' occurred in Microsoft.Xrm.Sdk.dll RRS feed

  • Question

  • Hello Techies,

    I'm using Microsoft.Xrm.Sdk (8.1.0.359) in "CRM online 2016" context.
    I get an "unhandled exception of type 'Microsoft.Xrm.Sdk.SaveChangesException' occurred in Microsoft.Xrm.Sdk.dll" with an inner exception "Object reference not set to an instance of an object."

    Below is my source code : 

    var readableUniqueString = "_" + DateTime.Now.ToString("ddMMyyyyhhmmss"); var uri = new Uri("https://janiceconsultingfr.api.crm4.dynamics.com/XRMServices/2011/Organization.svc"); var serviceProxy = CrmUtils.GetServiceProxy(uri); var orgService = (IOrganizationService)serviceProxy; var orgContext = new OrganizationServiceContext(orgService); orgContext.AddObject( new Account() { Name = "Janice Consulting_" + readableUniqueString

    }); orgContext.SaveChanges(); orgContext.Dispose();

    The same approach works fine with the below code where i read data before calling the SaveChanges

    var readableUniqueString = "_" + DateTime.Now.ToString("ddMMyyyyhhmmss");            
    var uri = new Uri("https://janiceconsultingfr.api.crm4.dynamics.com/XRMServices/2011/Organization.svc");            
    var serviceProxy = CrmUtils.GetServiceProxy(uri); 
    var orgService = (IOrganizationService)serviceProxy;            
    var orgContext = new OrganizationServiceContext(orgService);  
    var contacts = orgContext.CreateQuery<Contact>().First();//without this line NULL reference error is thrown        
    orgContext.AddObject(new Account()            {                
    Name = "Janice Consulting_" + readableUniqueString            });            
    orgContext.SaveChanges();           
    orgContext.Dispose();

    Many thanks for your valuable inputs.
    Clement

    Thursday, September 29, 2016 8:50 AM