Hi Nadeeja,
I'm still getting the same error - 'Object reference not set to an instance of an object.' after making all the changes you suggested still stucked!! I also added a new line for calling the Organization service to create an entity for me which is accountId = _orgService.Create(account); and its throwing an exception on that line see below: Please assist.
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;
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();
//Account account = new Account { Name = "Tester Account" };
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 Tester Account.
_accountId = _orgService.Create(account);
Console.Write(_accountId);
//Console.WriteLine("{0} {1} {2} created,", account.LogicalName);
Console.Write("Account created succesfully");