We help IT Professionals succeed at work.
Get Started

Help I am pulling hair out!!!! Stuck with Arraylists

NewMom2Brandon
on
329 Views
Last Modified: 2010-04-16
  I have two problem areas which I marked below. The problem I am having is when searching my document for a certain phrase i.e "Search For Me". If it is found twice in the document it does the "iXMLErrorMsgIndex++;" setting it to 2. However the arraylist collections are showing 1. What can I do to solve this. I have tried so many things over 3 days and I am still having issues.

Thank you for you help

  //Check to see if the file exists
   if (File.Exists(sDirectory + sCurrentFileName))
   {    
       // Save any messages found in the error log.
      ArrayList CurrentErrorLogDateAL = new ArrayList();

     //Initialize the array lists containing the messages found in the errorlog.
     //Current Error Log File Date
     XmlNodeMessageChild = xmlNodeErrorLogMessage.SelectSingleNode(TAGS.ErrorLogFileDate);
     CurrentErrorLogDateAL.Add(XmlNodeMessageChild.InnerText);

     // Get timestamp of Current errorlog file.
     DateTime dCurrentErrorLogFileDate = File.GetLastWriteTime(sDirectory + sCurrentFileName);
     String sCurrentErrorLogFileDate = dCurrentErrorLogFileDate.ToString
             ("s",System.Globalization.DateTimeFormatInfo.InvariantInfo);  
     dCurrentErrorLogFileDate = Convert.ToDateTime(sCurrentErrorLogFileDate).Date;

     if ((dCurrentErrorLogFileDate == dDayBefore) || (dCurrentErrorLogFileDate == dToday))
     {
           if (dDateTimeCurrent > dRunLast)
           {
                foreach (string sLogMsgLookingFor in LogMsgLookingFor)
     {
          bool bFound = false;

         //open the file for reading                                    
                    StreamReader objReader = new StreamReader(sDirectory + sCurrentFileName);

         string sCurrentLine = objReader.ReadLine();
         string sPreviousLine = "";
**********************************************************************************
    Should I really terminate this while loop if the log message you are looking for has been found?
    see next note). Could this be part of the index problem.? "bFound = true;
**********************************************************************************
         while(sCurrentLine != null)
         {
             if(sCurrentLine.IndexOf(sLogMsgLookingFor) > -1)
             {                                                            
                 if (sPreviousLine != null)
                 {      
                    bFound = true;
                 }
             }

             if (bFound == true)
             {                                                            
                 //Change previous line to when even happened time                                    
                 DateTime dErrorTime = Convert.ToDateTime(sPreviousLine).Date;
                 DateTime dPreviousLine = Convert.ToDateTime(sPreviousLine);
                String sErrorDateTime = dPreviousLine.ToString("dd-MMM-yyyy HH:mm:ss");

                 //Get the LogErrorTime from XML File
                XmlNode xmlLogErrorTime = xmlNodeErrorLogMessage.SelectSingleNode(TAGS.LogErrorTime);
                DateTime dLogErrorTime = Convert.ToDateTime(xmlLogErrorTime.InnerText);
                String sLogErrorTime = dLogErrorTime.ToString("dd-MMM-yyyy HH:mm:ss");    
               
               //Set the LastFoundTime to today in xml file.                                        
               XmlNode xmlNodeLastFoundTime = xmlNodeErrorLogMessage.SelectSingleNode (TAGS.LastFoundTime);
                  xmlNodeLastFoundTime.InnerText = dDateTimeCurrent.ToString
                                  ("s",System.Globalization.DateTimeFormatInfo.InvariantInfo);      

**********************************************************************************
if bFound == true twice (because "sLogMsgLookingFor" is in errorlog.txt twice), then iXMLErrorMsgIndex will = 2, and LastFoundTimeAL.Count will still = 1, causing the else block to be executed. I believe this is where the index error is happening. I don't know if it is happening else where since it stops here
**********************************************************************************
                 if(iXMLErrorMsgIndex < LastFoundTimeAL.Count)
                 {                                                                  
                      //Upgrade the array lists.
                        LastFoundTimeAL[iXMLErrorMsgIndex] = dDateTimeCurrent.ToString  
                                       ("s",System.Globalization.DateTimeFormatInfo.InvariantInfo);    
                  }
                  else
                  {
                          xmlError.InnerText =
                                      CONSTANTS.ERRORLOG_INDEX_OUT_OF_RANGE + "LastFoundTimeAL";
                          xmlErrorTime.InnerText = dDateTimeCurrent.ToString  
                                     ("s",System.Globalization.DateTimeFormatInfo.InvariantInfo);  
                   }
             
                   if ((dErrorTime == dToday) && (sErrorDateTime != sLogErrorTime))                            
                   {  
                         //Change from string to datetime to use
                           DateTime dTimeFromLog = Convert.ToDateTime(sPreviousLine);
           
                           //Set the LogErrorTime to what was found in the errlog.txt file for the error.
                            xmlLogErrorTime = xmlNodeErrorLogMessage.SelectSingleNode(TAGS.LogErrorTime);
                            xmlLogErrorTime.InnerText = dTimeFromLog.ToString
                              ("s",System.Globalization.DateTimeFormatInfo.InvariantInfo);
                                                                 
                            //Set LastErrorFoundIn to sCurrentFileName
                            XmlNode xmlLastErrorFoundIn = xmlNodeErrorLogMessage.SelectSingleNode                
                             (TAGS.LastErrorFoundIn);
                            xmlLastErrorFoundIn.InnerText = sCurrentFileName;

                            //Set LastSentTime to current date time.
                            XmlNode xmlLastSentTime = xmlNodeErrorLogMessage.SelectSingleNode  
                             (TAGS.LastSentTime);
                            xmlLastSentTime.InnerText = dDateTimeCurrent.ToString  
                                         ("s",System.Globalization.DateTimeFormatInfo.InvariantInfo);  
                                                                 
                           //Set ErrorLogFileDate in XML
                           XmlNode xmlErrorLogFileDate = xmlNodeErrorLogMessage.SelectSingleNode    
                             (TAGS.ErrorLogFileDate);
                           xmlErrorLogFileDate.InnerText = sCurrentErrorLogFileDate;  

                if(iXMLErrorMsgIndex < LastSentTimeAL.Count)
                 {
                     // Upgrade the array lists.
                       LastSentTimeAL[iXMLErrorMsgIndex] = dDateTimeCurrent.ToString();
                 }                                                  
                 else                                                              
                {                                                                      
                      xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_OUT_OF_RANGE    
                           + "LastSentTimeAL";
                     xmlMessageErrorTime.InnerText = dDateTimeCurrent.ToString                                                  
                         ("s",System.Globalization.DateTimeFormatInfo.InvariantInfo);                                  
                  }
         
                  if(iXMLErrorMsgIndex < LogErrorTimeAL.Count)
                  {
                       // Upgrade the array lists.
                        LogErrorTimeAL[iXMLErrorMsgIndex] = dTimeFromLog.ToString();
                  }
                 else                                                            
                {
              xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_OUT_OF_RANGE + "LogErrorTimeAL";
                                xmlMessageErrorTime.InnerText = dDateTimeCurrent.ToString
                                          ("s",System.Globalization.DateTimeFormatInfo.InvariantInfo);  
                }

                if(iXMLErrorMsgIndex < LastErrorFoundInAL.Count)
                {
                   // Upgrade the array lists.
                     LastErrorFoundInAL[iXMLErrorMsgIndex] = sCurrentFileName;
                }
                           else
                {
                    xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_OUT_OF_RANGE  
                   + "LastErrorFoundInAL";
                     xmlMessageErrorTime.InnerText = dDateTimeCurrent.ToString
                         ("s",System.Globalization.DateTimeFormatInfo.InvariantInfo);  
                }

               if(iXMLErrorMsgIndex < CurrentErrorLogDateAL.Count)
               { // Upgrade the array lists.
                      CurrentErrorLogDateAL[iXMLErrorMsgIndex] = dCurrentErrorLogFileDate.ToString();
                }
                else
                {
                 xmlMessageError.InnerText = CONSTANTS.ERRORLOG_INDEX_OUT_OF_RANGE
                      + "CurrentErrorLogDateAL";
                  xmlMessageErrorTime.InnerText =  dDateTimeCurrent.ToString
                       ("s",System.Globalization.DateTimeFormatInfo.InvariantInfo);  
                }

                sDetails[0] = CONSTANTS.ERRORLOG_ERROR_FOUND;                                        
                sValues[0] =  "Error: " + sCurrentLine + " Error Occured at: " + sPreviousLine;
               iSeverityLevel = SEVERITYLEVELS.WARNING;

                //Update index into the array list.
               iXMLErrorMsgIndex++;
     }
     else if (sErrorDateTime == sLogErrorTime)
     {
          xmlError.InnerText = CONSTANTS.NO_NEW_ERRORLOG_ERRORS;
                    xmlErrorTime.InnerText = dDateTimeCurrent.ToString
                            ("s",System.Globalization.DateTimeFormatInfo.InvariantInfo);  

                     //Sending an empty string so Client does not send a message
           sValues[0] = CONSTANTS.EMPTY_STRING;
     }
   }
     sPreviousLine = sCurrentLine;
     bFound = false;
     sCurrentLine = objReader.ReadLine();
          }//end while loop          
            objReader.Close();
        }
     }
  }
}
else
{
      xmlError.InnerText = ERROR.ERRORLOG_FILE_NOT_FOUND;
      xmlErrorTime.InnerText = dDateTimeCurrent.ToString            
           ("s",System.Globalization.DateTimeFormatInfo.InvariantInfo);  
}
Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
This problem has been solved!
Unlock 2 Answers and 14 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE