This is a migrated thread and some comments may be shown as answers.

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

2 Answers 204 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Tajes
Top achievements
Rank 1
Tajes asked on 08 Nov 2010, 04:01 PM
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 Answers, 1 is accepted

Sort by
0
Tajes
Top achievements
Rank 1
answered on 08 Nov 2010, 04:55 PM
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)
0
Accepted
Damyan Bogoev
Telerik team
answered on 10 Nov 2010, 05:11 PM
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
Tags
Getting Started
Asked by
Tajes
Top achievements
Rank 1
Answers by
Tajes
Top achievements
Rank 1
Damyan Bogoev
Telerik team
Share this question
or