It is not allowed to read or to write an instance marked for deletion.

3 posts, 1 answers
  1. Tajes
    Tajes avatar
    81 posts
    Member since:
    Sep 2009

    Posted 08 Nov 2010 Link to this post

    Hello everybody,
       I have the following problem. When I remove a row in a RadGridView in a Master-Detail Form I get this error:
    "It is not allowed to read or to write an instance marked for deletion."


    This grid is linked to data as following way:

    MyClass have a relation one to Many with MyClass2, MyClass.MyClass2 is a list implementing the relation

    bindingSource1.DataSource = myContext.getAll<MyClass>();
    bindingSource2.DataSource = bindingSource1;
    bindingSource2.DataMember = MyClass2;
    RadGridView.DataSource = bindinSource2;
    summary:
    context.getall<MyClass>()  ->  bindingSource1  ->  bindingSource2 (DataMember = "MyClass2")-> RadGridView


    I catch the bindingSource2.ListChanged event to remove the object from context:

    bindingSource2.ListChanged += new ListChangedEventHandler(b_ListChanged);

    void
    b_ListChanged(object sender, ListChangedEventArgs e)
    {
        if (e.ListChangedType == ListChangedType.ItemDeleted)
        {
            context.remove(((BindingSource)sender).List[e.NewIndex])
        }
    }

    And then, when I remove a row in the grid and after run this code the  "It is not allowed to read or to write an instance marked for deletion." error is thrown.

    I don't know what I'm doing wrong. Can anyone help me?
    Thanks and sorry for my English

    P.S. I attached a screenshoot of the error
  2. Tajes
    Tajes avatar
    81 posts
    Member since:
    Sep 2009

    Posted 08 Nov 2010 Link to this post

    Hello again,

      I have another issue in the same code, but this time when I add a new row in the grid.
      If I add a row, enter data, and saveChanges, all is fine. But if I add a row, enter data and add another row wihtout saving changes I get the following error:

    "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: Index"

    It's the same code of the last post but with if (e.ListChangedType == ListChangedType.ItemAdded)
  3. DevCraft banner
  4. Answer
    Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 10 Nov 2010 Link to this post

    Hi Iván Tajes,

    1. You are able to work with objects which are marked for deletion by doing the following:
    - Expose the underlying IObjectScope instance from the OpenAccessContext class:

    public partial class YourContextClassName 
        public IObjectScope Scope 
        
            get{ return this.GetScope(); } 
        
    }

    - Set the ITransactionProperties.ReadAfterDelete to true:

    context.Scope.TransactionProperties.ReadAfterDelete = true;

    2. You can refactor the ListChanged event handler using the following approach to achieve your goal:

    if(e.ListChangedType == ListChangedType.ItemAdded)
    {
        BindingSource source = sender asBindingSource;
        if(e.NewIndex >= 0 &&
            e.NewIndex < source.List.Count)
        {
            Category newCategory = source.List[e.NewIndex] asCategory;
            this.context.Add(newCategory);
        }
    }
    Hope that helps.

    Greetings,
    Damyan Bogoev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top