locked
failed deleted record cannot be modified ? RRS feed

  • Question

  • Hello !

    i have this code :

    -------------------------------------------------------------------------

    Private sub delete........

    try

    context.mytables.DeleteObject(object)

    context.SaveChanges()

    catch ex as exception

    MessageBox.show("The delete failed.Try again")

    End try

    end sub

    ---------------------------------------------------

    i have a form that display a record from database. If i try to press the button that execute the above sub , the record is deleted and everything is ok.But if during deletion , occur an exception , the error message in catch , is displayed and the record is not deleted.. After that if i try to modify this record  i get this error message:

    The object is in a detached or deleted state. An ObjectStateEntry in this state cannot be modified

    So the record is deleted or not ? What can i do to ensure that if a deletion operation fail , after that i can do other operations with this record?

    Thank you.

    Saturday, February 25, 2012 9:32 PM

Answers

  • Hi dcode25;

    As the exception states an entity that is in the Deleted or Detached state can not be modified. Now this does NOT mean that the entity has been deleted but waiting to be deleted from the database on the next SaveChanges method call.

    If you wish to change the state so that you can modify it you can do the following.

    ' Get a reference to the ObjectStateManager
    Dim objectStateManager As ObjectStateManager = context.ObjectStateManager
    ' Holds the object state of the entity whos object state is to be changed
    Dim stateEntry As ObjectStateEntry = Nothing
    
    ' Get the object state of the entity, from your sample code it is object
    context.ObjectStateManager.TryGetObjectStateEntry(DirectCast(object, IEntityWithKey).EntityKey, stateEntry)
    ' Change the state of the entity to Unchanged
    stateEntry.ChangeState(EntityState.Unchanged)
    
    ' Now you can change the properties of the entity object.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Allen_MSDN Thursday, March 1, 2012 1:54 AM
    Sunday, February 26, 2012 7:36 PM

All replies

  • Hi dcode25;

    In order to know what is causing the exception please modify your Catch code to this and when you throw the exception post the exception message and if one exist the inner exception.

    Catch ex As Exception
        MessageBox.Show("The delete failed.Try again" & _
            vbNewLine & "Excption : " & ex.Message)
        If ex.InnerException IsNot Nothing Then
            MessageBox.Show("Inner Exception : " & ex.InnerException.Message)
        End If
    End Try

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Sunday, February 26, 2012 2:46 PM
  • i know what is causing the exception. The sql server is disconnected from network.But this is not my question.i want to know if the deletion process fail for some reason ( in my case the problem with sql server) , why after that if i try to work with this record i get the message:

    The object is in a detached or deleted state. An ObjectStateEntry in this state cannot be modified

    But the record is not deleted.

    Sunday, February 26, 2012 3:24 PM
  • Hi dcode25;

    As the exception states an entity that is in the Deleted or Detached state can not be modified. Now this does NOT mean that the entity has been deleted but waiting to be deleted from the database on the next SaveChanges method call.

    If you wish to change the state so that you can modify it you can do the following.

    ' Get a reference to the ObjectStateManager
    Dim objectStateManager As ObjectStateManager = context.ObjectStateManager
    ' Holds the object state of the entity whos object state is to be changed
    Dim stateEntry As ObjectStateEntry = Nothing
    
    ' Get the object state of the entity, from your sample code it is object
    context.ObjectStateManager.TryGetObjectStateEntry(DirectCast(object, IEntityWithKey).EntityKey, stateEntry)
    ' Change the state of the entity to Unchanged
    stateEntry.ChangeState(EntityState.Unchanged)
    
    ' Now you can change the properties of the entity object.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Allen_MSDN Thursday, March 1, 2012 1:54 AM
    Sunday, February 26, 2012 7:36 PM