I created a contact object which has and Addressess Collection which contain a series of Address objects. I wanted to bind my address collection to a datagrid, so I had to add the following code to the ContactAddresses collection so that when a person starts entering data in the new line in the data grid, this code will create a new object.
protected override object AddNewCore(){
ContactAddress result;result =
ContactAddress.NewContactAddress();OnAddingNew(
new AddingNewEventArgs(result));Add(result);
return result;}
My problem is this. When I type the new line into the datagrid everything is fine until I get to the last item. If I just hit enter, the focus will move to ANOTHER new line and the previous line I just got done typing disappears. If I tell it to save the Contact, it tries to save an address with all fields set to null.
However, if after I finishing typing in the last field for a new line, I then click on an already existing address record. The new address line stays on the datagrid and when I go to save it, things work like normal.
These are my objects
public class Contact : BusinessBase<Contact>public
class ContactAddress : BusinessBase<ContactAddress>public
class ContactAddresses : BusinessListBase<ContactAddresses, ContactAddressI have not done any modifications to the basic CSLA code except to implement my classes. Anyone have any ideas?
Further specifications.
Record A is an existing record. Record B is going to be my new record, so I go down to the new record line and start typing it. Everthing works fine as long as I don't type go to the new record line after entering Record B. If for any reason I go to the new record line without typing in a new record, as soon as I leave that line, I loose data. So here is what I have done so far.
1. Type in a new record but don't go to the new record line after the last field. Instead I click on the first record. All is fine.
2. Type in a new record and allow it to go to the next, blank new record line. All is ok until focus leaves the blank new record line. Then I loose the record I just typed in. The object still exists but all the data fields are set to null.
3. Typed in two new record in a row and let the focus move to the blank new record line. All is ok until focus leaves the blank new record line, then the first new record I typed in disappears from the DataGrid. The object is still there but it has null values.
The Id for my Address objects are an int which is an Identity from the SQL2005 server. So when a new object is created, there is no object Id. Do you think that may have something to do with it?
I am thinking the issue is in this code.
void System.ComponentModel.IEditableObject.CancelEdit()This is in Core.BusinessBase in the System.ComponentModel.IEditable section. I think that If I can find an event in the datagrid after entering a record and then make it do a snapshop, then I would resolve my problem.
P.S. I notice nobody is responding to my post, but I figured I may as well go through the semantecs so the next person has some reference.
I found the problem. Because my ID for my object is the Primary Key from a table in Ms SQL2005, I didn't put a value in for the Object Id. So every new object had an Id of zero (0). So when ever you were on a new line and you went into edit mode and cancelled it, the code would remove every object with an Id of zero because that Id matched the object that was created for the line that you started editing.
I corrected the problem by putting a private static int value in my Address Class.
static
int NewAddressUniqueValue = 0;When anything calls the NewContactAddress method to get a Contact Address, it calls decrements the value in NewAddressUniqueValue and assigns it to the Id of the ContactAddress object. Now the ContactAddress has a unique Id and won't disappear when deleting empty objects from the collection. There is no possibility of mix up with the real object Ids because they are values greater than 0 and the temporary ones are values less than or equal to zero. Also when the object is persisted to the database, it doesn't pass it's Object Id along and thus it gets a real one when that happens.
So I hope this helps anyone who comes accross it.
Copyright (c) Marimer LLC