locked
Update date code RRS feed

  • Question

  • Hi,
    I am trying to update the "name" and "begindate" value in Pricelevel entity record.
    Here's the code i am using for it:
    pricelevel pricelist = new pricelevel();

                    pricelist.name = "Price List 2";
                    string datestring = "05/05/2009";
                    string begindatestring;
                    DateTime parseddate= new DateTime();
                    parseddate = Convert.ToDateTime(datestring);
                        begindatestring = pricelist.begindate.Value.ToString();
                        begindatestring = parseddate.ToString();
                   
                    pricelist.pricelevelid = new Key();
                    pricelist.pricelevelid.Value = new Guid("704DA13B-C396-DD11-A0E2-0003FF607C97");

                    service.Update(pricelist);
    the name is updated normally but the on the date i am gettin the following exception: "object reference not set to an instance of an object".
    What's wrong with my code?
    Thanks in advance

    Monday, December 15, 2008 8:44 AM

Answers

  • Please review the sample code from the SDK below:

    //SAMPLE CODE TO SET THE DATE FIELDS
    appointmentReq.SearchWindowStart = new CrmDateTime();
    appointmentReq.SearchWindowStart.Value = DateTime.Now.ToUniversalTime().ToString();

    // SAMPLE CODE TO CREATE RECORD
    CrmAuthenticationToken token = new CrmAuthenticationToken();
    // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
    token.AuthenticationType = 0; 
    token.OrganizationName = "AdventureWorksCycle";
     
    CrmService service = new CrmService();
    service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
    service.CrmAuthenticationTokenValue = token;
    service.Credentials = System.Net.CredentialCache.DefaultCredentials;

    // Create the contact object.
    contact contact = new contact();

    // Create the properties for the contact object.
    contact.firstname = "Jesper";
    contact.lastname = "Aaberg";
    contact.address1_line1 = "23 Market St.";
    contact.address1_city = "Sammamish";
    contact.address1_stateorprovince = "MT";
    contact.address1_postalcode = "99999";
    contact.donotbulkemail = new CrmBoolean();
    contact.donotbulkemail.Value = true;

    // Create the contact in Microsoft Dynamics CRM.
    Guid contactGuid = service.Create(contact);

    Hassan.


    Monday, December 15, 2008 10:50 AM

All replies


  • Hi Sabine,

        You need to retrieve before you can update. Try this.

                    uid guidPriceLevel = new Guid("{704DA13B-C396-DD11-A0E2-0003FF607C97}");
    pricelevel pricelist = (pricelevel) service.Retrieve(EntityName.pricelevel.ToString(),guidPriceLevel , new AllColumns());
    pricelist.name = "Price List 2";
    string datestring = "05/05/2009";
    string begindatestring;
    DateTime parseddate = new DateTime();
    parseddate = Convert.ToDateTime(datestring);
    begindatestring = pricelist.begindate.Value.ToString();
    begindatestring = parseddate.ToString();

    pricelist.pricelevelid = new Key();
    pricelist.pricelevelid.Value = new Guid("{704DA13B-C396-DD11-A0E2-0003FF607C97}");
    service.Update(pricelist);

    Hassan.
    Monday, December 15, 2008 9:18 AM
  • ok so this is in case i am updating the record. But what can be done in case of creation? i need to set the pricelist fields values before creation. It's working with the name but it's giving the same exception for the date. What could be done in this case?

    Monday, December 15, 2008 10:27 AM
  • Please review the sample code from the SDK below:

    //SAMPLE CODE TO SET THE DATE FIELDS
    appointmentReq.SearchWindowStart = new CrmDateTime();
    appointmentReq.SearchWindowStart.Value = DateTime.Now.ToUniversalTime().ToString();

    // SAMPLE CODE TO CREATE RECORD
    CrmAuthenticationToken token = new CrmAuthenticationToken();
    // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
    token.AuthenticationType = 0; 
    token.OrganizationName = "AdventureWorksCycle";
     
    CrmService service = new CrmService();
    service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
    service.CrmAuthenticationTokenValue = token;
    service.Credentials = System.Net.CredentialCache.DefaultCredentials;

    // Create the contact object.
    contact contact = new contact();

    // Create the properties for the contact object.
    contact.firstname = "Jesper";
    contact.lastname = "Aaberg";
    contact.address1_line1 = "23 Market St.";
    contact.address1_city = "Sammamish";
    contact.address1_stateorprovince = "MT";
    contact.address1_postalcode = "99999";
    contact.donotbulkemail = new CrmBoolean();
    contact.donotbulkemail.Value = true;

    // Create the contact in Microsoft Dynamics CRM.
    Guid contactGuid = service.Create(contact);

    Hassan.


    Monday, December 15, 2008 10:50 AM