0

I am writing a C#.Net windows application using EF for communicating with SQL Express. I have a Contacts table in my application. For editing a Contact I have called a new windows form by ShowDialogue() method and used ApplyCurrentValue method. In this form if I get the list of my contacts (using db.Contacts.ToList() method), the changes could be seen but when I close the form and want to refresh the gridview which exist in the main form, I cannot see the changes unless I restart the whole application.

here is the code for showing a new form:

NewContactForm contact = new NewContactForm();
        contact.TopLevel = true;
        contact.ShowInTaskbar = false;
        contact.ShowDialog();
        RefreshForm();
        FillGridView();

I have changed the contactToEdit object's properties and want to save changes. So:

public void UpdateContact(Contact contactToEdit)
    {
        db.Contacts.ApplyCurrentValues(contactToEdit);
        db.SaveChanges();
    }

Now I want to show the list of contacts using this code which is in the FillGridView():

db.Contacts.ToList();

Am I missing some code? I even used db.AcceptAllChanges() but useless

Monoloox
  • 1
  • 3
  • possible duplicate of [EF4 Context.ApplyCurrentValues does not update current values](http://stackoverflow.com/questions/4038713/ef4-context-applycurrentvalues-does-not-update-current-values) – Chris Gessler May 15 '12 at 10:31
  • I do not trust ApplyCurrentValues. – Eon May 15 '12 at 10:45
  • the same code is working properly in web forms. the question is why it does need a restart in windows form – Monoloox May 15 '12 at 10:51
  • the only reason I suspect it happening is because windows forms don't have postbacks, therefore it does not re-run any queries to bind data to your list/control/whatever – Eon May 15 '12 at 10:53
  • Web forms, especially in ASP.NET and ASP.NET MVC is postback driven (every time you make an action, did you notice your web form refreshes?) maybe you have some select query somewhere to bind your data or list your data which is handled in the postback method which is responsible for your actual display of the data. That was confusing. :D – Eon May 15 '12 at 10:55
  • I modified the question. take a look – Monoloox May 16 '12 at 04:31

2 Answers2

0

Here is some code you might want to try.

db.Contacts.Single(Contact => Contact.ContactID == <lets say, 5>).Name = "Pete";
db.Contacts.Single(Contact => Contact.ContactID == <lets say, 5>).Surname = "Repeat";

db.SaveChanges();

This is the method I use to update records in the database using the Entity Framework. See if this helps. You can test using your toList() method or querying the Contacts model

var contactquery = (from x in db.Contacts where x.ContactID == <lets say, 5> select new {contactName = x.Name});

List<string> contactNames = new List();

foreach (var name in contactquery)
{
     contactNames.Add(name.contactName);
}

update This is just a clarification update. Where I say , it could be any value which represents the field stated in the Single() method. What single() does is it extracts only the first record from the query determined by the lambda expression in it.

update 2 fixed a error in my code. Please revise yours

Eon
  • 3,833
  • 10
  • 46
  • 75
  • wow , formatting screwed out :) , guess its because of the apostrophe's. nothing serious, it is to be replaced. – Eon May 15 '12 at 10:42
  • you did just like me but didn't use apply method at all. any way I got the same result – Monoloox May 15 '12 at 10:54
  • The difference by mine is no applyCurrentValues method. and Since you are using windows forms, make sure the data is actually written to your db after the method executes (I take it you know how to step-into with debugging) right after the SaveChanges() method. If yes, then ensure your list/Control gets REPOPULATED with data. maybe your ToList() was bugging out, maybe you should Iterate through a select query. – Eon May 15 '12 at 10:57
  • everything is fine. I have a deleteobject and I have no problem in deleting object and after deleting the list refill and shows correct data. the problem just occurs while updating – Monoloox May 15 '12 at 11:03
0

Ok .. that is the solution which worked for me

In my main form (parent scope) I refreshed the modified object using this simple code:

db.Refresh(System.Data.Objects.RefreshMode.StoreWins, contactToEdit)

and everything works nice ever after!

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
Monoloox
  • 1
  • 3