Click here to Skip to main content
13,002,163 members (87,452 online)
Rate this:
Please Sign up or sign in to 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 18-Apr-12 3:15am
B Birajdar 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.
Sastry_kunapuli 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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 7 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100