Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've encountered a really big problem in a program I'm working on.

Basically, what my program is suppose to do, is use LINQ to query the correct entity by its ID and then delete it.

The thing is, it won't ever delete the correct entity unless its the last entity in the list. It always deletes the entity right after it.

Here's the code I've been using throughout the program.
VB
Dim Cat = From c In PE.Categories
                  Where c.CategoryID = lblCategoryID.Text
                  Select c
        Try
            For Each c As NEIPurchasingDAL.Category In Cat
                PE.DeleteObject(c)
            Next
        Catch ex As Exception
            MessageBox.Show("There are no more categories to delete.", _
                     "Attention", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try

PE.SaveChanges()


I've tried working around with my code a little and had no luck, and I can't seem to find anything online.

Update:
The solution I proposed didn't fix the problem like I first thought. I still have the same problem of it not always deleting the correct entity, the solution I proposed seems to be more accurate though, but not perfect.
Posted
Updated 18-Apr-12 11:24am
v4
Comments
Steve Maier 11-Apr-12 14:27pm    
Does the real code have a PE.SaveChanges() in it for the context to actually persist the changes?
Mr.McCloud 11-Apr-12 15:36pm    
Yeah, this is just a small bit of my code that I copied.
Nelek 18-Apr-12 17:37pm    
Please update the solution you gave with the same text you added in the question as well. Not all the people pay attention on the date and can be confused with it.

1 solution

After messing around with it a little bit, I found a solution to my own problem.

What I did is set a variable as an object equal to the current value of the binding source. Then I suspended the binding source and called the Context.DeleteObject() method and finally resumed binding.

This fixed my problem.

Here's an example in case anyone else runs into this same problem.

VB
Dim (variable) As (object) = (Name)BindingSource.Current
(Name)BindingSource.SuspendBinding()
(Context).DeleteObject((variable))
(Name)BindingSource.ResumeBinding()


And here's the actual code I used.

VB
Dim CurrentObject As NEIPurchasingDAL.Category = CategoryBindingSource.Current
CategoryBindingSource.SuspendBinding()
PE.DeleteObject(CurrentObject)
CategoryBindingSource.ResumeBinding()


Update:
The solution I proposed didn't fix the problem like I first thought. I still have the same problem of it not always deleting the correct entity, the solution I proposed seems to be more accurate though, but not perfect.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900