Setting the value for Field[index]

Posts   
 
    
NickD
User
Posts: 224
Joined: 31-Jan-2005
# Posted on: 26-Sep-2008 06:11:35   

LLBLGen 2.6 VS2008 MSSql2005

I want to allow the user to pick a field to update and then send me a comma separated key/value pair file to update just the records in that file. So, I've got a table called "person" that has various fields like birthdate or gender or lastname. If they wanted to update the birthdate field, they'd send a comma separated list with person_id, birthdate. How can I make this work without a bunch of switch statements? Here's my attempt which I knew was doomed because of the casting error you'll see at the line that says:

person.Fields[fieldIndex].CurrentValue = personKeyPair[1];

...full method...


public static int ImportSingleField(string fileToOpen, int fieldIndex, short cbedsYear)
        {
            int recordsImported = 0;    //Keep track of how many records are imported
            using (StreamReader sr = new StreamReader(fileToOpen))
            {
                string line;
                CbedsYearEntity cbedsYr = new CbedsYearEntity();
                cbedsYr.FetchUsingPK(cbedsYear); //Get the year we're dealing with;
                while ((line = sr.ReadLine()) != null)
                {
                    PersonEntity person = new PersonEntity();   //Create a new PersonEntity instance
                    string[] personKeyPair = line.Split(',');
                    int localPersonID = Convert.ToInt32(personKeyPair[0].Trim('"'));
                    bool isFetched = person.FetchUsingUCLocalPersonIdCbedsYr(localPersonID.ToString().PadLeft(10, '0'), cbedsYear);

                    person.Fields[fieldIndex].CurrentValue = personKeyPair[1];
                    person.Save();
                    recordsImported++;
                }
            }

            return recordsImported;
        }

...what am I missing?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-Sep-2008 08:20:25   

Hi NickD, try this,

person.SetNewFieldValue(fieldIndex, personKeyPair[1]);

Is recommended to use SetNewFieldValue, as this method set all the entity and field properties to indicate that the field value has been changed. If no change is detected the Save method will return true but without any effect at DB.

David Elizondo | LLBLGen Support Team