Here is my scenario:
EntitySpaces Version # 2007.0.0328.0
MyGeneration Version # 1.2.0.5
Vista DB Version #3.20.1.20
I have a collection which is loaded at runtime, then bound to a listview object.
Code:
1 private EmployeeCollection findEmployees()
2 {
3 //get initial EmployeeCollection object
4 emps = new EmployeeCollection();
5
6 //do the search and return the results
7 emps.Query.Load();
8
9 //sort results based on ddlSearchSortOrder control
10 if (ddlSearchSortOrder.SelectedItem != null)
11 {
12 ColumnHeader column = (ColumnHeader)ddlSearchSortOrder.SelectedItem;
13 emps.Sort = (string)column.Tag + " " + sortOrder;
14 }
15
16 return emps;
17 }
When a user double-clicks on the Employee, we open a new form by storing the current 'employee' in a variable, and passing it to the new form.
Code:
1 private void btnSelectEmployee_Click(object sender, EventArgs e)
2 {
3 //make sure we have a valid employee selected
4 if (lvFoundEmployees.SelectedItems.Count > 0)
5 {
6 _presenter.OnCloseView();
7 _presenter.OpenEmployeeForEdit((Employee)lvFoundEmployees.SelectedItems[0].Tag);
8 }
9 else
10 {
11 _presenter.OnNoEmployeeSelected();
12 }
13 }
Then, we allow the user to make some changes to the form, and click the save button, which calls our save method.
Code:
1 internal void OnSaveDraft_Click()
2 {
3 if (employee != null)
4 {
5 //save employee to db with DRAFT status
6 employee.EmployeeStatusID = EmployeeStatus.DRAFT;
7
8 //Call EndEdit() for all editable DataBindingSources in child views.
9 _employeeDetailView.EndEditOnDataBindingSources();
10 _employeeJobcodeView.EndEditOnDataBindingSources();
11
12 //save the employee to the local DB
13 using (esTransactionScope scope = new esTransactionScope())
14 {
15 employee.EndEdit();
16 employee.Save();
17 scope.Complete();
18 }
19
20 //notify the user of save success
21 string msg = String.Format(Resources.dialog_SaveEmployeeSuccessful_Draft, employee.ToString());
22 shellNotificationService.Show(msg, Resources.caption_SaveEmployeeSuccessful, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
23
24 //close the form
25 this.OnCloseView();
26 }
27 }
The probem occurs on line #16 when we call the Save() method. It throws an error which says "You must call Save() on the esEntityCollection". For some reason the employee object is holding onto the rest of the collection when it is passed to the form.
- Is there anyway to 'drop' the collection once the employee is passed to the 'edit' form? I really don't want to make another call to the db if possible.
- If not, I was thinking about using the code below, which would basically call the employee.Save() if employee.collection is null, or otherwise call employee.collection.save(). However, is this only going to save the current employee, b/c its the only one which has been modified - or will it save the ENTIRE collection??
BTW - It would be REALLY helpful if there was an easy way to change the text color in these posts (without editing the HTML) !!