Ramesh
Ramesh

Reputation: 59

How to add an existing contact to an existing group

How can I add ABPerson to ABGroups in MonoTouch?

i use ABGroup.Add() following exception fires

Unhandled Exception: System.ArgumentException: cfErrorHandle must not be null. Parameter name: cfErrorHandle

I select already exist ABPerson using ABPeoplePickerNavigationController.


ABAddressBook adBook = new ABAddressBook();

//ABPeoplePickerNavigationController SelectPerson event void HandleAbPeoplePickerSelectPerson (object sender, ABPeoplePickerSelectPersonEventArgs e) {

if(_isNew )
{

    CreateGroup (txtNewGroup .Text);

    AddPersontoGroup(txtNewGroup .Text, e.Person);


}

if(!e.Continue )
    this.NavigationController .DismissModalViewControllerAnimated (true);

}

public void AddPersontoGroup(string strGroupName,ABPerson person ) { ABGroup[] allGroups = adBook.GetGroups();

for (int rowIndex=0; rowIndex<allGroups.Length ;rowIndex++)
{
    ABGroup abGroup=allGroups [rowIndex];

    if(abGroup.Name ==strGroupName)
    {
        abGroup.Add(person);
        adBook.Save ();
        break;
    }
}

}

public void CreateGroup(string strGroupName) {

ABGroup grp = new ABGroup (); grp.Name = strGroupName; adBook.Add(grp); adBook.Save ();

}

Thanks

Ramesh K

Upvotes: 2

Views: 902

Answers (1)

poupou
poupou

Reputation: 43553

The address book can be peculiar. E.g. Adding a ABPerson to a ABGroup is valid ony if the ABPerson is part of the ABAddressBook (i.e. adding it to a group does not, automagically, does that).

This code, basically what you're doing with one extra line, will work.

        ABAddressBook adBook = new ABAddressBook ();
        ABGroup grp = new ABGroup ();
        grp.Name = "Test";
        adBook.Add (grp);
        adBook.Save (); 

        ABPerson p = new ABPerson ();
        adBook.Add (p);

        grp.Add (p);
        adBook.Save (); 

but if you remove the line adding the ABPerson to the ABAddressBook you'll get the same error that you already experience.

        adBook.Add (p);

Upvotes: 1

Related Questions