Unable to Edit Outlook Mobile contact using .net compact framework

Hi,
Whenever I try to remove or edit and update a contact in pocket outlook using .net compact framework I get the following error..

Error:
==============
System.InvalidOperationException was unhandled
Message="Can't modify the item collection inside a foreach statement"
StackTrace:
at PimItemCollectionEnumerator.collection_ListChanged()
at Microsoft.WindowsMobile.PocketOutlook.PimItemCollection.RemoveAt()
at EqualizerLibrary.Util.SetGroupInOutlook()
at EqualizerUI.MyGroupsForm.mitChangeGroup_Click()
at System.Windows.Forms.MenuItem.OnClick()
at System.Windows.Forms.Menu.ProcessMnuProc()
at System.Windows.Forms.Form.WnProc()
...
...

My code is like this:
=========================================================================================

public static void SetGroup(string iName, string group)

{

// Open an outlook session

OutlookSession outlookSession = new OutlookSession();

Contact contact = null;

// Retrieve the contacts from outlook

ContactCollection contactCollection = outlookSession.Contacts.Items;

bool isFound = false;

if (contactCollection != null)

{

if (contactCollection.Count > 0)

{

// Add the equals contacts a new.

for (int i = 0; i < contactCollection.Count; i++)

{

contact = contactCollectionIdea;

string inam = contact.Nickname;

if (inam == iName)

{

// Delete the current contact

//contactCollection.RemoveAt(i);

if (contact.Nickname != string.Empty && inam.StartsWith("="))

{

isFound = true;

break;

}

}

}

if (isFound)

{

contact.MiddleName = group;

contact.Update(); //THIS IS WHERE I GET THE ERROR

}

}

}

outlookSession.Dispose();

}

===============================================================================================

It would be very nice if someone could help me out on this...
Thanks a lot
Regards,
Khalid Naseem.
khalid@amsoft.net



Answer this question

Unable to Edit Outlook Mobile contact using .net compact framework

  • Joe Clifford

    Hi Noah,

    Given under is the complete stack trace I get. Even though I call the update method, the PimItemCollection.RemoveAt() function is called as shown by the stack trace... may be something internal to the API...

    System.InvalidOperationException was unhandled
    Message="Can't modify the item collection inside a foreach statement"
    StackTrace:
    at PimItemCollectionEnumerator.collection_ListChanged()
    at Microsoft.WindowsMobile.PocketOutlook.PimItemCollection.RemoveAt()
    at EqualizerLibrary.Util.SetGroup()
    at EqualizerUI.MyGroupsForm.mitChangeGroup_Click()
    at System.Windows.Forms.MenuItem.OnClick()
    at System.Windows.Forms.Menu.ProcessMnuProc()
    at System.Windows.Forms.Form.WnProc()
    at System.Windows.Forms.Control._InternalWnProc()
    at Microsoft.AGL.Forms.EVL.EnterModalDialog()
    at System.Windows.Forms.Form.ShowDialog()
    at EqualizerUI.ContactsForm.mitMyGroups_Click()
    at System.Windows.Forms.MenuItem.OnClick()
    at System.Windows.Forms.Menu.ProcessMnuProc()
    at System.Windows.Forms.Form.WnProc()
    at System.Windows.Forms.Control._InternalWnProc()
    at Microsoft.AGL.Forms.EVL.EnterMainLoop()
    at System.Windows.Forms.Application.Run()
    at EqualizerUI.Program.Main()

  • Robert Taylor

    I am still a little curious to see the exact exception dump to look at not only your methods on the stack but also what internal methods happened to be on the stack this time around. (Clearly at least the RemoveAt() also changed, probably some other methods have changed too). Could you re-run it and include the updated full exception text

    -Noah

    .Net Compact Framework


  • jasonwisdom

    Well I tried to reproduce the issue using the smaller sample method you indicated but the app worked just fine. Perhaps you could follow these steps on both the PPC emulator and whatever other device you may be using and see what the result is in each case We need to figure out what is different between my test and yours that is giving the different behavior.

    Here are the steps I used:

    1) Create new windows mobile 5 PPC Console app in VS2005

    2) Wrote this code into Program.cs:

    using System;

    using System.Collections.Generic;

    using System.Text;

    using Microsoft.WindowsMobile.PocketOutlook;

    namespace ConsoleApplication1

    {

    class Program

    {

    static void Main(string[] args)

    {

    // Open an outlook session

    OutlookSession outlookSession = new OutlookSession();

    // Retrieve the contacts from outlook

    ContactCollection contactCollection = outlookSession.Contacts.Items;

    Contact contact = contactCollection[0];

    contact.FirstName = "Khalid";

    contact.Update();

    outlookSession.Dispose();

    }

    }

    }

    3) Added reference to PocketOutlook assembly

    4) Set target device as WM5 PPC emulator deployed and ran

    5) Got indexoutofrange exception (oops didn't have a didn't have a contact 0) so I added a contact named 'Noah' using the PPC Contacts gui

    6) Re-ran the test and verified no error and that the contact had been changed to Khalid

    -Noah

    .Net Compact Framework


  • Savitas

    Thanks a lot Noah. You have been very helpful :-)

    Regards
    Khalid

  • Jonathan van de Veen

    Hi Noah,
    I think I used a previous stack trace..sorry. The current stack trace is exactly the same except it is at EqualizerLibrary.Util.SetGroup() function, the code for which I have included. I am not using any foreach loop.

    The code for EqualizerUI.MyGroupsForm.mitChangeGroup_Click() is as under

    private void mitChangeGroup_Click(object sender, EventArgs e)
    {
    if (selectedGroup != null)
    {
    // If the list state has the focus then set the selected state
    if (lstGroups.Focused)
    {
    Cursor.Current = Cursors.WaitCursor;

    // Set the group for the contact to the selected group.
    RulesHandler.ChangeGroup(serverAddress, owner, selectedContact,
    selectedGroup);

    // Update the group for the user in the address book
    Util.SetGroup(Util.GetIName(selectedContact), selectedGroup);

    //Set the privileges for the selected contact
    PrivilegesForm PrivilegesForm = new PrivilegesForm(parentForm, serverAddress,
    owner, selectedContact,true);

    PrivilegesForm.ShowDialog();
    this.Close();
    }
    }

    The
    RulesHandler.ChangeGroup function is used to send a Http request and there is no loop in it.
    I have to update the group in Outlook using
    Util.SetGroup and move on to another form.

    I get the same error even if I hard code the editing and updating on an outlook contact like

    public static void TestAddressBookUpdate()
    {
    // Open an outlook session
    OutlookSession outlookSession = new OutlookSession();

    // Retrieve the contacts from outlook
    ContactCollection contactCollection = outlookSession.Contacts.Items;

    Contact contact =
    contactCollection[0];
    contact.FirstName = "Khalid";
    contact.Update();

    outlookSession.Dispose();

    }

    All the help is highly appreciated..

    Regards,
    Khalid

  • J. Leite

    Looking in the stack trace is EqualizerLibrary or EqualizerUI part of your code If so could you include the code for those two functions

    at EqualizerLibrary.Util.SetGroupInOutlook()
    at EqualizerUI.MyGroupsForm.mitChangeGroup_Click()

    Until I could look at those two function all I could say is that the exception claims you are using the RemoveAt() method on a collection from within a foreach statement that is enumerating it, ie something like this:

    FooCollection fooGroup = new FooCollection();

    foreach(Foo currentFoo in fooGroup)

    {

    fooGroup.RemoveAt( 1 );

    }

    It is generally illegal to alter collections while they are being enumerated. Instead you could try enumerating a copy while you alter the original, or record the changes you want to make during the enumeration and then apply them afterwards. Hope that helps,

    -Noah

    .Net Compact Framework


  • jonnic

    Weird, still doesn't make much sense that RemoveAt() is called from SetGroup() given that the only use of it is commented out. I'll try it out your code tomorrow and see if I can get a similar result.

    -Noah

    .Net Compact Framework


  • Sam2

    This post has info about stand alone emulator support for your QA guys.

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=411454&SiteID=1

    Unforetunately I have never tried a manual install on a smartphone but there must be instructions somewhere... maybe someone else will fill that one in for me.

    -Noah

    .Net Compact Framework


  • Birch_P_A_UK

    Hi Noah,

    I am developing my application for the Windows Mobile 5.0 Smartphone. I have tested your code and it works ....and so does the code that I have written.. but randomly, that is, it works initally but throws the error when called from some other screens ( no set pattern found as yet). I tested it rigorously and discovered that it performs the operation that it is meant to do and then throws the error. So what I have done is that I handle the error using try...catch blocks so that even after throwing the exception it keeps on working. It is a dirty way but I have to make do for now..

    One other thing I would like to know is that How can I install the windows mobile 5.0 emulator without installing Visual Studio so that I can provide my application to the QA team to test. Having to install Visual Studio for each tester will not be possible. Also how do I install my application on the Smartphone emulator after having copied the CAB file to it as none of the folders available in the Start Menu of the emulator display it..

    Thanks a ton for all your help !

    Regards
    Khalid

  • Unable to Edit Outlook Mobile contact using .net compact framework