0

i have problem with visual studio or entity framework. Situation is simple: I have Employes, and i have data grid view for present, when i click on Edit employe, i get ID from employe

int employeID= Convert.ToInt32(dgvEmployes.SelectedRows[0].Cells[0].Value);
frmEditEmploye edit = new frmEditEmploye ();
edit.employ = new ePoliticsServis.Data.Employ();
edit.employ= eServis.Data.EmployesService.SelectEmployeById(employeID);

and i get Employe from db, and i load employe in new form for editing,and i save updated date, and go in database refresh table and all is good, all is updated, butproblem is, when i back on preview, datagrid view (datagrid is refreshed with new data, binding, all is ok) and again click on same employe for editing, i have form for editing but with old data, no with new upadated data from database for the same employe, when i shotdown my application and run again then i get new data refreeshed and updated. i really dont know where is problem..

Anna Dino
  • 219
  • 1
  • 5
  • 12

1 Answers1

0

I suspect the problem is your data context is retrieving the data from memory and not the database see a previous answer of mine for a possible solution.

Remember to surround your data context in a Using statement to correctly dispose of it.

From your latest comment I can see you are probably not disposing of your context correctly, I think this is called 'connection' in your code. This is probably a member variable of the class,

You'll probably need to change the method to something like this:

//You need to check the type Connection is and use it here. I'm using 'Connection' because I don't know the name of your context.

Using (var myContext = new Connection()) 
{
  var returnValue =  myContext.dm.epsp_Employ_SelectById(id).FirstOrDefault();
}

return returnValue;

One more thing...Check the database is being updated after you save the record. If it isn't you need to call SaveChanges().

Community
  • 1
  • 1
Damon
  • 3,004
  • 7
  • 24
  • 28
  • yea, i think i have to refresh my db context? where i will do this? – Anna Dino Sep 16 '13 at 11:13
  • context.Refresh(RefreshMode.StoreWins, myObject); this is an example of refreehs, where i will call this method? and wher i will found name of my context? – Anna Dino Sep 16 '13 at 11:20
  • From what I can see in your sample code, it'll more than likely be in the eServis.Data.EmployesService.SelectEmployeById method. – Damon Sep 16 '13 at 11:25
  • what you need to see? i will send you whatever, epolitic is my project name data is folder for EDMX file and for others data services folders and classes like EmployeService, CustomerService and etc.. thank you a lot and please help – Anna Dino Sep 16 '13 at 11:33
  • public static Employ SelectEmployById(int id) { return Connection.dm.epsp_Employ_SelectById(id).FirstOrDefault(); } – Anna Dino Sep 16 '13 at 11:37
  • I need to see it ALL! Just kidding, just take a step back and logically examine the layers of your code. Put a breakpoint in the method and step into it, trace each of the steps before it eventually calls the generated code in the EDMX file. The data context would have called this method. Once you've got that code, update your question with the code from this method and we'll see what we have. – Damon Sep 16 '13 at 11:45
  • oo hah thanks :) i'did , my Update function in EDMS file is in this link: http://pastebin.com/tjYeiwqu – Anna Dino Sep 16 '13 at 12:17
  • There's your problem, you're calling a stored procedure to update your database and the objectcontext is now out of sync. Follow my advice above and either dispose of your objectContext or refresh it. – Damon Sep 16 '13 at 12:23
  • please my existence is questionable, help me please, i dont know how di it? :( please help me good mman – Anna Dino Sep 16 '13 at 13:00
  • can I add this pieces of code under return of my update function... (in context file) var context = ((IObjectContextAdapter)Connection.dm).ObjectContext; context.Refresh(System.Data.Objects.RefreshMode.StoreWins, context); – Anna Dino Sep 16 '13 at 16:46
  • Sure you can try that, you'll need to change your existing code slightly i.e. store the return value of your stored procedure call into a variable, then call the refresh, then return the value from the stored procedure. I'd really take a look though at how you are managing the EF context in the greater scheme of things. It needs to be disposed of correctly otherwise your application will just get slower and slower the more it's used. If it is, then it's all good :) – Damon Sep 16 '13 at 17:07
  • i was try, but nothing... how i call refresh? in context? or on button when i call update function? i really dont know what is exactly context and wich name is, i will send you some importent things and please can you tell me what is name of my context.. for minut i will send photo – Anna Dino Sep 16 '13 at 17:13
  • second: http://s8.postimg.org/dq027ldad/image.png third photo: http://s23.postimg.org/vec6i8096/image.jpg – Anna Dino Sep 16 '13 at 17:16