none
Index out of range Exception

    Question

  • Dear All,

    I am converting a long CSV file to ms access file. The file has about 352274 records. its gets converted but when tries to updates records it throws an exception "index was out of range. must be non negative and less than the size of the collection"

    am Here is the code where it throws the exception.

      private void UpdateDeptAuhbr()
            {
                OleDbConnection cn = default(OleDbConnection);
                OleDbConnection cn1 = default(OleDbConnection);
                OleDbCommand cmd = default(OleDbCommand);
                OleDbDataAdapter da = default(OleDbDataAdapter);
                int icount = 0;
                DataTable dt = null;
                string UserID = null;
                string[] UserID1 = null;
    
    
               
                System.DirectoryServices.DirectoryEntry ADEntry = new System.DirectoryServices.DirectoryEntry("LDAP://ereffe.errr.tt.ad/DC=ere,DC=errr,DC=ad");
                try
                {
                    cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Persist Security Info = False;" + "Data Source=C:\\AuhJTB.mdb;");
                    cn.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
    
                cmd = new OleDbCommand("SELECT DISTINCT UserName FROM AuhJTB where Division =''", cn);
    
                //SELECT UserName FROM AuhJTB where  UserName like 'KWTHQ\*'
                da = new OleDbDataAdapter(cmd);
                dt = new DataTable();
                da.Fill(dt);
    
                cn.Close();
    
    
    
                try
                {
                    cn1 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Persist Security Info = False;" + "Data Source=C:\\AuhJTB.mdb;");
                    cn1.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
    
                            
                foreach (DataRow row in dt.Rows)
                {
                    UserID1 = row["UserName"].ToString().Split('\\');
                    UserID = UserID1[1].Trim();
                    DirectorySearcher search = new DirectorySearcher(ADEntry);
    
                    search.Filter = "(&(objectClass=User)(samaccountname=" + UserID + ")) ";
                    search.PropertiesToLoad.Add("department");
                    //SearchResult result;
                    Int32 j = 0;
                    foreach (SearchResult result in search.FindAll())
                    {
                        try
                        {
                            // MessageBox.Show(result.Properties("department")(j).ToString())
    
                            try
                            {
                                cmd = new OleDbCommand("UPDATE AuhJTB SET Division = '" + result.Properties["department"][j].ToString() + "' WHERE UserName = '" + row["UserName"].ToString() + "'", cn1);
                                icount = cmd.ExecuteNonQuery();
    
                             
                                break;
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.ToString());
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }
    
                       
                    }
                   
                }
                cn1.Close();
            }

    Here is where i always get the error 

      cmd = new OleDbCommand("UPDATE AuhJTB SET Division = '" + result.Properties["department"][j].ToString() + "' WHERE UserName = '" + row["UserName"].ToString() + "'", cn1);

    Kindly have a look and Advice


    • Edited by roma_victa Sunday, March 17, 2013 2:13 PM
    Sunday, March 17, 2013 2:05 PM

Answers

  • It is likely that the DirectoryEntry that you are using does not have the department attribute set.  You need to check to ensure that it has any values prior to using it in the Update statement.

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Marked as answer by roma_victa Sunday, March 17, 2013 5:47 PM
    Sunday, March 17, 2013 3:13 PM

All replies

  • i can use the same update command in the MS access directly.

    and it updates about 1141 records. But only when it runs through the C# it gives the error

    Sunday, March 17, 2013 2:31 PM
  • It is likely that the DirectoryEntry that you are using does not have the department attribute set.  You need to check to ensure that it has any values prior to using it in the Update statement.

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Marked as answer by roma_victa Sunday, March 17, 2013 5:47 PM
    Sunday, March 17, 2013 3:13 PM
  • That was it. It does not have the department attribute set.

    Thank you 

    Sunday, March 17, 2013 5:48 PM