views:

63

answers:

1

Hi,

My WPF desktop-based application uses ADO.Net Entity Framework in order to connect with SQL Server database. In one of the windows I have a DataGrid with all data from tbl_users, when user selects one of the row (record) and clicks on Edit, application opens a new window with form, that includes all data that user can edit/update.

My question is how to save changes (update/edit) of one of the table's values to database via ADO.NET Entity Framework?

Here are some code snippets that help understand a situtation:

1 - Edit window constructor

public object objToBeEdited;

public WinWorkers_EditWorker(tbl_users userToBeEdited)
{
    this.Title = Glidus.Properties.Resources.WinWorkers_EditWorker_WinName + Glidus.Properties.Resources.WinApp_WinName;
}

2 - Method Update, that doesn't work

tbl_users newUser = new tbl_users() //assume inputted values to new ibject
{
    userName = this.WinWorkers_AddEditWorkers_Form_UserName.Text,
    userPassword = this.WinWorkers_AddEditWorkers_Form_Password.Text,
    userAccessLevel = this.WinWorkers_AddEditWorkers_Form_UserAccessLevel.Text
};

//default minimal password length is 4
if (App.IsInputValueMinLenOK(newUser.userPassword, 4))
{
    EntityKey key = App.glidusContext.CreateEntityKey("tbl_users", objToBeEdited);

    if (App.glidusContext.TryGetObjectByKey(key, out objToBeEdited))
    {
        App.glidusContext.ApplyCurrentValues<tbl_users>(key.EntitySetName, newUser);
    }

    try
    {
        App.glidusContext.SaveChanges();
    }
    catch (Exception ex)
    {
        App.UnitedHandleException(ex);
    }

Please, help me, how to implement update ADO.NET record from database.

For example, in my table there is James Bond 007, after editing page (clicking in submit page) I see Ostin Pawers 008

+2  A: 

To edit or update you can use stub entities

Category category = new Category { ID = 5};
Context.AttachTo(“Categories”,category);

Product product = new Product {
     Name = “Bovril”,
     Category = category
};
Context.AddToProducts(product);
Context.SaveChanges();
John Hartsock