Reputation: 14586
My WPF desktop-based application uses ADO.Net Entity Framework in order to connect to SQL Server database. In one of the windows I have a DataGrid
with all data from tbl_users
, when user selects one of the rows (records) and clicks on Edit, application opens a new window with form, that includes all data that user can edit/update.
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 situation:
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
//assume inputted values to new object
tbl_users newUser = new tbl_users() {
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 the database. For example, in my table there is James Bond 007, after editing page (clicking in submit page) I see Austin Powers 008.
Upvotes: 1
Views: 1865
Reputation: 86882
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();
Upvotes: 2