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(se archThis) > -1)
{
bFound = true;
System.Diagnostics.EventLo g.WriteEnt ry(this.To String(), sDateHappend + " DateHappened");
MessageBox.Show(sCurrentLi ne, "InSearchthis", MessageBoxButtons.OK,
MessageBoxIcon.Error, MessageBoxDefaultButton.Bu tton1,
MessageBoxOptions.ServiceN otificatio n);
MessageBox.Show(sPreviousL ine, "InSearchthisPerviousLine" , MessageBoxButtons.OK,
MessageBoxIcon.Error, MessageBoxDefaultButton.Bu tton1,
MessageBoxOptions.ServiceN otificatio n);
if (bFound)
{
System.Diagnostics.EventLo g.WriteEnt ry("Test", "Found");
MessageBox.Show(sCurrentLi ne, "FollowFoundError", MessageBoxButtons.OK,
MessageBoxIcon.Error, MessageBoxDefaultButton.Bu tton1,
MessageBoxOptions.ServiceN otificatio n);
MessageBox.Show(sPreviousL ine, "ErrorPreviousline", MessageBoxButtons.OK,
MessageBoxIcon.Error, MessageBoxDefaultButton.Bu tton1,
MessageBoxOptions.ServiceN otificatio n
}
}
sCurrentLine = objReader.ReadLine();
}
objReader.Close();
}
}
}
catch (Exception e)
{
System.Diagnostics.EventLo g.WriteEnt ry(this.To String(),
ERROR.FAILED_TO_READ_ERROR _LOG + e.ToString(),
System.Diagnostics.EventLo gEntryType .Error);
}
}
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
(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(se
{
bFound = true;
System.Diagnostics.EventLo
MessageBox.Show(sCurrentLi
MessageBoxIcon.Error, MessageBoxDefaultButton.Bu
MessageBoxOptions.ServiceN
MessageBox.Show(sPreviousL
MessageBoxIcon.Error, MessageBoxDefaultButton.Bu
MessageBoxOptions.ServiceN
if (bFound)
{
System.Diagnostics.EventLo
MessageBox.Show(sCurrentLi
MessageBoxIcon.Error, MessageBoxDefaultButton.Bu
MessageBoxOptions.ServiceN
MessageBox.Show(sPreviousL
MessageBoxIcon.Error, MessageBoxDefaultButton.Bu
MessageBoxOptions.ServiceN
}
}
sCurrentLine = objReader.ReadLine();
}
objReader.Close();
}
}
}
catch (Exception e)
{
System.Diagnostics.EventLo
ERROR.FAILED_TO_READ_ERROR
System.Diagnostics.EventLo
}
}
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!
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.
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();
}
In other words move it to ...
...
sPreviousLine = sCurrentLine; <---- here ...
sCurrentLine = objReader.ReadLine();
}
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.
then I get a use of unassigned local variable error for the messageboxes inside the if statements.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
YAAAA it worked...Thank you so much!!!
You are most welcome - glad to have been of assistence