locked
Index was outside the bounds of the array. RRS feed

  • Question

  • i have sharepoint 2007

    and i develop code to read colum Called "To" and seprated the users in this "To" to 3 parts first part will have the first name second part will have second name therd part will have the rest of names.

    and update a 3 colums with this values.

    but i recive this error :

    Index was outside the bounds of the array.
    my code is:
      public override void ItemAdded(SPItemEventProperties properties)
            {
               
                    using (SPSite site = new SPSite("http://sps/MySite"))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            #region
                            SPList oList = web.Lists["MyList"];
                            site.AllowUnsafeUpdates = true;
                            web.AllowUnsafeUpdates = true;
                            SPListItemCollection oListItems = oList.Items;
                            SPListItem oListItem = properties.ListItem;
                            
                            string ToNames = properties.ListItem["To"].ToString();
                            //string ToNames = properties.ListItem["To"].ToString().Replace("#", "");
                            ToNames = ToNames.Replace("0;#", "");
                            ToNames = ToNames.Replace("1;#", "");
                            ToNames = ToNames.Replace("2;#", "");
                            ToNames = ToNames.Replace("3;#", "");
                            ToNames = ToNames.Replace("4;#", "");
                            ToNames = ToNames.Replace("5;#", "");
                            ToNames = ToNames.Replace("6;#", "");
                            ToNames = ToNames.Replace("7;#", "");
                            ToNames = ToNames.Replace("8;#", "");
                            ToNames = ToNames.Replace("9;#", "");
                            ToNames = ToNames.Replace("#", "");
                            ToNames = ToNames.Replace("0", "");
                            ToNames = ToNames.Replace("1", "");
                            ToNames = ToNames.Replace("2", "");
                            ToNames = ToNames.Replace("3", "");
                            ToNames = ToNames.Replace("4", "");
                            ToNames = ToNames.Replace("5", "");
                            ToNames = ToNames.Replace("6", "");
                            ToNames = ToNames.Replace("7", "");
                            ToNames = ToNames.Replace("8", "");
                            ToNames = ToNames.Replace("9", "");
    
                            try
                            {
                                String textbox = ToNames;
                                //properties.ListItem["Remarks"] ="15- "+ ToNames;
                                //i have 3 varible as string
                                String a1 = "";
                                String b1 = "";
                                String c1 = "";
    
                                String[] split = textbox.Split(new String[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                                String outputA1 = String.Format("{0}={1}", a1, split[0]);
                                String outputB1 = String.Format("{0}={1}", b1, split[1]);
                                String outputC1 = String.Format("{0}={1}", c1, split[2] + ";" + split[3]);
    
                                
                                properties.ListItem["AC1"] = outputA1;
                                properties.ListItem["AC2"] = outputB1;
                                properties.ListItem["AC3"] = outputC1;
    
                            }
                            catch (Exception ex)
                            {
                                properties.ListItem["Remarks"] = ex.Message.ToString();
                            }
    
                            oListItem.Update();
                            web.AllowUnsafeUpdates = false;
                            site.AllowUnsafeUpdates = false;
                            #endregion
    
                        }
                    }
               
              
            }
    

    Thanks for any help :) my blog is: http://www.waelk.com
    Monday, January 30, 2012 1:31 PM

Answers

  • Firstly I want to understand what is the exact requirement. Can you explain with an example.

    As per my understanding, you have a people picker field which has multiple users like User A, User B, User C, User D ......User N

    Now you want, string first = "User A", string second = "User B" and string third = "User C;User D;.... User N"

    If my understanding is correct, why don't you make use of SPFieldUserValueCollection.

    Try something like below

    SPFieldUserValueCollection userValueCollection = new SPFieldUserValueCollection();
    
    userValueCollection = (SPFieldUserValueCollection)item["Internal name for To field"];
    
    string first = String.Empty;
    string second = String.Empty;
    string third = String.Empty;
    
    
    if(userValueCollection.Count > 0)
    {
            first = userValueCollection[0].User.LoginName;
            second = userValueCollection[1].User.LoginName;
    
            for(int i=2;i<userValueCollection.Count;i++)
            {
                   third += userValueCollection[i].User.LoginName+";";
            }
    
    }
    
    
          

     

    Its just a rough idea and the code is something in mind which I haven't tested. You may need to check for any syntax error that might have occured as I have not tested it.

     

     

    • Marked as answer by Pengyu Zhao Monday, February 6, 2012 8:11 AM
    Monday, January 30, 2012 6:31 PM
  • this part looks suspicious:

    String[] split = textbox.Split(new String[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
    String outputA1 = String.Format("{0}={1}", a1, split[0]);
    String outputB1 = String.Format("{0}={1}", b1, split[1]);
    String outputC1 = String.Format("{0}={1}", c1, split[2] + ";" + split[3]);
    

    in this code you assume that "split" array always contains 4 elements. You should check the length of array before to write this code.


    Blog - http://sadomovalex.blogspot.com
    CAML via C# - http://camlex.codeplex.com
    • Marked as answer by Pengyu Zhao Monday, February 6, 2012 8:11 AM
    Monday, January 30, 2012 9:11 PM

All replies

  • Hi,

    Which code line occurs error?


    Software Engineer http://www.yazilimdilleri.net http://www.ugurkizmaz.com
    Monday, January 30, 2012 1:46 PM
  • Firstly I want to understand what is the exact requirement. Can you explain with an example.

    As per my understanding, you have a people picker field which has multiple users like User A, User B, User C, User D ......User N

    Now you want, string first = "User A", string second = "User B" and string third = "User C;User D;.... User N"

    If my understanding is correct, why don't you make use of SPFieldUserValueCollection.

    Try something like below

    SPFieldUserValueCollection userValueCollection = new SPFieldUserValueCollection();
    
    userValueCollection = (SPFieldUserValueCollection)item["Internal name for To field"];
    
    string first = String.Empty;
    string second = String.Empty;
    string third = String.Empty;
    
    
    if(userValueCollection.Count > 0)
    {
            first = userValueCollection[0].User.LoginName;
            second = userValueCollection[1].User.LoginName;
    
            for(int i=2;i<userValueCollection.Count;i++)
            {
                   third += userValueCollection[i].User.LoginName+";";
            }
    
    }
    
    
          

     

    Its just a rough idea and the code is something in mind which I haven't tested. You may need to check for any syntax error that might have occured as I have not tested it.

     

     

    • Marked as answer by Pengyu Zhao Monday, February 6, 2012 8:11 AM
    Monday, January 30, 2012 6:31 PM
  • this part looks suspicious:

    String[] split = textbox.Split(new String[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
    String outputA1 = String.Format("{0}={1}", a1, split[0]);
    String outputB1 = String.Format("{0}={1}", b1, split[1]);
    String outputC1 = String.Format("{0}={1}", c1, split[2] + ";" + split[3]);
    

    in this code you assume that "split" array always contains 4 elements. You should check the length of array before to write this code.


    Blog - http://sadomovalex.blogspot.com
    CAML via C# - http://camlex.codeplex.com
    • Marked as answer by Pengyu Zhao Monday, February 6, 2012 8:11 AM
    Monday, January 30, 2012 9:11 PM