trying to update fk to null

Posts   
 
    
vikramka
User
Posts: 42
Joined: 08-Sep-2004
# Posted on: 31-Aug-2006 00:20:56   

I am trying to change one of hte foreign keys to null and it just doesnt seem to take it. Here is the code.

                data.SetNewFieldValue((int)Protocol641FieldIndex.CurrentRequest_ID, null); //CHANGING TO NULL HERE
                string test = data.Fields[(int)Protocol641FieldIndex.CurrentRequest_ID].CurrentValue.ToString();
                //test = data.Fields[(int)Protocol641FieldIndex.CurrentRequest_ID].DbValue.ToString();
                test = data.Fields[(int)Protocol641FieldIndex.CurrentRequest_ID].IsForeignKey.ToString();
                test = data.Fields[(int)Protocol641FieldIndex.CurrentRequest_ID].IsChanged.ToString();
                //test = data.Fields[(int)Protocol641FieldIndex.CurrentRequest_ID]..IsChanged.ToString();

                data.SetNewFieldValue((int)Protocol641FieldIndex.StatusChangedDate, null);
                data.SetNewFieldValue((int)Protocol641FieldIndex.StatusChangedBy, null);

                //save facility recursively
                adapter.SaveEntity(data);

Am i missing something in the documentation?

vikramka
User
Posts: 42
Joined: 08-Sep-2004
# Posted on: 31-Aug-2006 00:44:30   

After further investigation it looks as if the entity has to be fetched first to set its fk field value to null


                DataAccessAdapter adapter = new DataAccessAdapter();

                Protocol641Entity data = new Protocol641Entity(2);
                adapter.FetchEntity(data);
                data.SetNewFieldValue((int)Protocol641FieldIndex.CurrentRequest_ID, null);
                adapter.SaveEntity(data);
            


the above code works


                DataAccessAdapter adapter = new DataAccessAdapter();

                Protocol641Entity data = new Protocol641Entity(2);
                data.IsNew = false;
                data.SetNewFieldValue((int)Protocol641FieldIndex.CurrentRequest_ID, null);
                adapter.SaveEntity(data);

this one doesn't. Is this behaviour intended or is it a bug?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 31-Aug-2006 08:22:19   

Try the following:

DataAccessAdapter adapter = new DataAccessAdapter();

                Protocol641Entity data = new Protocol641Entity(2);
                data.IsNew = false;
                data.SetNewFieldValue((int)Protocol641FieldIndex.CurrentRequest_ID, null);
                data.Fields.CurrentRequest_ID.IsChanged = true;
                adapter.SaveEntity(data);

When you don't load (fetch) the entity, it's members will hold default values (null for that specific field) then when you change it to null, the field does not recognize this as a change of value, because its value did not change actually, so you have to explicitly set IsChanged to true;

vikramka
User
Posts: 42
Joined: 08-Sep-2004
# Posted on: 31-Aug-2006 20:09:15   

Thanks. It works.