Link to home
Start Free TrialLog in
Avatar of NewMom2Brandon
NewMom2Brandon

asked on

Not holding previous line read from text file

This is what would be in the text file
Text File
May 3, 2005 12:48:50 PM
my message

What I have showing up is the line "my message" in all the messageboxes that would display the currentline and the previous line. What I need in the previous line is the date.


try
{
   if (File.Exists(sDirectory + sCurrentFileName))
   {
          if ((dCurrentErrorLogFileDate == dDayBefore && dHour >= dRunAtGMTHour ) ||
      (dCurrentErrorLogFileDate == dToday && dHour < dRunAtGMTHour))
          {
      bool bFound = false;

      //Get string to search for
      string sMsgLookingFor = "my message";
                                          
      //open the file for reading
      StreamReader objReader = new StreamReader(sDirectory + sCurrentFileName);
      
      string sCurrentLine = objReader.ReadLine();
      string sPreviousLine;

                string searchThis = sMsgLookingFor;

                while(sCurrentLine != null && sCurrentLine != searchThis)
      {
               //get the line before date and time value for comparison
                sPreviousLine = sCurrentLine;

                        if(sCurrentLine.IndexOf(searchThis) > -1)
              {
            bFound = true;
            
                          System.Diagnostics.EventLog.WriteEntry(this.ToString(), sDateHappend + " DateHappened");

                 MessageBox.Show(sCurrentLine, "InSearchthis", MessageBoxButtons.OK,
                                MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                                MessageBoxOptions.ServiceNotification);

                           MessageBox.Show(sPreviousLine, "InSearchthisPerviousLine", MessageBoxButtons.OK,
            MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
            MessageBoxOptions.ServiceNotification);
                                          
                 if (bFound)
                 {
                                 System.Diagnostics.EventLog.WriteEntry("Test", "Found");
            MessageBox.Show(sCurrentLine, "FollowFoundError", MessageBoxButtons.OK,
                       MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                       MessageBoxOptions.ServiceNotification);

            MessageBox.Show(sPreviousLine, "ErrorPreviousline", MessageBoxButtons.OK,
                       MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                        MessageBoxOptions.ServiceNotification
                             }
                 }
            sCurrentLine = objReader.ReadLine();
            }
                objReader.Close();
            }
       }
 }

 catch (Exception e)
{
System.Diagnostics.EventLog.WriteEntry(this.ToString(),
ERROR.FAILED_TO_READ_ERROR_LOG + e.ToString(),
System.Diagnostics.EventLogEntryType.Error);
}

}
Avatar of TheMCSE
TheMCSE

Can you elaborate on the format of your text file?  It's not clear to me what the format is, or what exactly you are trying to retrieve.  Without copying and pasting this into something where the indentation looks a little more natural :), this sounds as if it could be a problem of variable scope.  If you display the variable at known locations, do they all return the same unexpected result?  Is your text file format date, followed by a load of messages, or alternating dates and messages?  Good luck!
Avatar of Julian Hansen
There are a few things I can see off the bat - the most obvious one however is the one that is causign the problem

while(sCurrentLine != null && sCurrentLine != searchThis)
{
      //get the line before date and time value for comparison
      sPreviousLine = sCurrentLine; <--- After this line sPreviousLine = sCurrentLine - so obviously they will be the same

You need to move the line above to the end of the loop.

Additional comment

you are setting bFound = true and then checking within the same block if it is true - it will always evaluate true
secondly you are never setting bFound to False again - maybe this is what you want tho.
>> You need to move the line above to the end of the loop.

In other words move it to ...

...
  sPreviousLine = sCurrentLine;  <---- here ...
  sCurrentLine = objReader.ReadLine();
  }
Avatar of NewMom2Brandon

ASKER

when I move sPrevious line to where you are mentioning....

then I get a use of unassigned local variable error for the messageboxes inside the if statements.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
YAAAA it worked...Thank you so much!!!
You are most welcome - glad to have been of assistence