Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a DataGridView with EntityDataSource; DataGridView has deleting and updating enabled. Everything works well, but when I hit F5 key I get following exception:

System.Data.OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

I've spent last hour or so trying to find a solution with Google, but I am obviously missing something as I am not sure how to transcend those information to working solution and I start to feel like a simpleton.

Can someone explain me what am I missing or doing wrong?
In simple English =/.
Posted
Comments
bbirajdar 18-Apr-12 9:25am    
Please post the code if you want us to find the problem in your code.

I guess you are missing the .SaveChanges() on the entity after editing it.
Oshtri Deka 18-Apr-12 9:45am    
?There is no code, only markup.
DataGridView with its dataSource, dataSource can automatically add, remove and update data; I change data with Command buttons on grid.

Updating data works well, deleting also, when I navigate off and on the page data shown in grid is up-to-date, my problem only occurs when I am trying to refresh a page and I wonder why it happens?

It never happened with SQLDataSource or ObjectDataSource.
S@53K^S 18-Apr-12 10:47am    
Entity Framework keeps a track of all the associated entities.So if anything changes from the last method call then it will raise the exception.
Let me explain this with an example.

If you are binding a List<employees> where Employee is an entity class.Then Entity Framework keeps track of all that.So if you are trying to change any of its values like update address of the employee "a" then you have to make sure save the changes back to the datacontext.SQLDataSource and other datasources you have to get the changes back from the UI element to underlying source object,but here it is done automatically so when a record in the DataGrid changes its underlying Entity gets updated automatically.
Oshtri Deka 24-Apr-12 6:28am    
Nothing? Come on, this is easily reproduced.
1.Make model from database
2.create web form with gridView
3.add entity dataSource to gridView
4.delete row from grid
5. refresh page
6. boom!

1 solution

I am little embarrassed, but this was BIG "noobish" mistake on my part.

I was deleting rows programmatically (i.e. in base via different context), instead of deleting them on grid. That was it.

Silly me.
 
Share this answer
 

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