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 :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Updating a record from entity framework

(0) ShareShare
ReportReport
Posted on by 1,703

CRM2016 On Premises

I don't know if this is the right place to ask this question.

I have a web project that I have created an entity framework using CDATA Dynamics CRM ADO.Net Provider.

I am only using the CRM Account and Contact entities

In my web project I can view the contents of the CRM Contacts entity via the entity framework, however I'm struggling ( inexperience )  to update a record. I have tried several flavours sourced from the net, but keep getting an 'Object reference not set to an instance of an object'

My simple code below. I have check I do have an entry in CRM Contact with a full name of John Smith, and the field new_password does exsist

Can anyone point me in the right direction to try and track down the problem, or has anyone else used the CDATA component

Code  

        Try
            Dim updateId As String = "3950B4DB-A4EE-EA11-8B7C-00155D011B24"
            Dim _newcontext As CDataEntities = New CDataEntities()
            Dim updateRecord = _newcontext.Contact.Where(Function(Ind) Ind.FullName = "John Smith").FirstOrDefault()
            updateRecord.new_password = "ABCDEF123"
            _newcontext.SaveChanges()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try

I have the same question (0)
  • Pete_N Profile Picture
    1,703 on at

    I have modified my code to try and get to the bottom of this

           Try

               Dim db As CDataEntities = New CDataEntities()

               Dim result = db.Contact.SingleOrDefault(Function(b) b.ContactId Is "3950B4DB-A4EE-EA11-8B7C-00155D011B24")

               _logging.WriteErrorLog(result.FullName & " " & result.new_password) ' Testing only to see if record is found

               If result IsNot Nothing Then

                   result.new_password = "ABCDEF123"

                   Dim v = db.SaveChanges() ' error occours

               End If

           Catch ex As Exception

               _logging.WriteErrorLog(ex.Message.ToString)

               _logging.WriteErrorLog(ex.StackTrace.ToString)

           End Try

    Log File

    08/09/2020 10:42 John Smith password123

    08/09/2020 10:42 Object reference not set to an instance of an object.

    08/09/2020 10:42    at System.Web.UI.ParseChildrenAttribute.GetHashCode()

      at System.Collections.Generic.ObjectEqualityComparer`1.GetHashCode(T obj)

      at System.Linq.Set`1.InternalGetHashCode(TElement value)

      at System.Linq.Set`1.Find(TElement value, Boolean add)

      at System.Linq.Enumerable.<ExceptIterator>d__73`1.MoveNext()

      at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)

      at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

      at System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.<GetAttributes>b__3(PropertyInfo pi)

      at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)

      at System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.GetAttributes(PropertyInfo propertyInfo)

      at System.Data.Entity.Internal.Validation.EntityValidatorBuilder.BuildPropertyValidator(PropertyInfo clrProperty)

      at System.Data.Entity.Internal.Validation.EntityValidatorBuilder.BuildValidatorsForProperties(IEnumerable`1 clrProperties, IEnumerable`1 edmProperties, IEnumerable`1 navigationProperties)

      at System.Data.Entity.Internal.Validation.EntityValidatorBuilder.BuildTypeValidator[T](Type clrType, IEnumerable`1 edmProperties, IEnumerable`1 navigationProperties, Func`3 validatorFactoryFunc)

      at System.Data.Entity.Internal.Validation.EntityValidatorBuilder.BuildEntityValidator(InternalEntityEntry entityEntry)

      at System.Data.Entity.Internal.Validation.ValidationProvider.GetEntityValidator(InternalEntityEntry entityEntry)

      at System.Data.Entity.Internal.InternalEntityEntry.GetValidationResult(IDictionary`2 items)

      at System.Data.Entity.DbContext.ValidateEntity(DbEntityEntry entityEntry, IDictionary`2 items)

      at System.Data.Entity.DbContext.GetValidationErrors()

      at System.Data.Entity.Internal.InternalContext.SaveChanges()

      at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()

      at System.Data.Entity.DbContext.SaveChanges()

      at TEST.Login.UpdateLocked() in D:\VS Projects\2020\Website Development\TEST\Account\Login.aspx.vb:line 138

  • Suggested answer
    TNS Profile Picture
    1,197 on at

    Hi,

    When you update a record you have to pass guid of the record, i think you are not passing guid that's why you are getting the object reference error .

    As you are updating contact so you have to pass contactid like _newcontext.contactid ,

    Mark my answer verified if it is helpful

  • Pete_N Profile Picture
    1,703 on at

    HI,

    Thank you for your reply. I've based my code on several posts on entity framework, so am a little confused as to how I pass the contactid within the code i have written

  • Suggested answer
    Pankaj Gogoi Profile Picture
    3,177 on at

    Hi Pete,

    Please check if there are any required fields that needs to be filled before you save the record. Also check the link below.

    https://stackoverflow.com/questions/17136455/nullreferenceexception-in-dbcontext-savechanges

    Good Luck

    Best Regards

    PG

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 137 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 57

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans