Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm reading a file placed in somewhere in the directory, If I get something like "Connection reset" or "Exception" I'm storing that entire line with next 3 lines.


VB
Function SearchException(ByVal filePath As String, ByVal fileName As String)
      Dim strTextFile As StreamReader
      Dim rowLength As Integer = 0
      Dim eachLine As String
      Dim strFilePath = filePath & "\" & fileName
      Dim flagChk As Boolean

      strTextFile = File.OpenText(filePath & "\" & fileName)
      txtVewLogDetail.Text = strTextFile.ReadToEnd

      ''TextBox1.Text = fileName.Remove(10)

      Dim strStartupLine As String
      strStartupLine = getStartupDate(fileName.Remove(10))

      Dim r As StreamReader = New StreamReader(strFilePath)
      Dim aryShowString() As String
      Dim intLine As Integer = 0
      'rowLength = File.ReadAllLines(strFilePath).Length

      While (r.ReadLine() IsNot Nothing)
          rowLength = rowLength + 1
          eachLine = r.ReadLine()
          If (eachLine.Contains("Connection reset")) Then
              aryShowString(0) = eachLine.ToString()
              intLine = intLine + 1
              MsgBox("Exception Found at Row = " & rowLength)
              'flagChk = True
          End If
      End While
END SUB


Here aryShowString is an array, when I'm trying to store the content of eachLine which is a string itself, is not working.

Please Help me on this...
Posted

1 solution

Change it from an Array to a List of strings, then use the Add method to include another string in the list. At the moment, you load each line into the same array element, which means you end up with just the last value.
VB
Dim r As StreamReader = New StreamReader(strFilePath)
Dim lines As New List(Of String)()
Dim intLine As Integer = 0
'rowLength = File.ReadAllLines(strFilePath).Length

While (r.ReadLine() IsNot Nothing)
    rowLength = rowLength + 1
    eachLine = r.ReadLine()
    If (eachLine.Contains("Connection reset")) Then
        lines.Add(eachLine)
        intLine = intLine + 1
        MsgBox("Exception Found at Row = " & rowLength)
        'flagChk = True
    End If
End While
If you really need the list as an array at the end, you can use the ToArray method.
 
Share this answer
 
Comments
santosh.giri 19-May-13 6:55am    
Thanks for helping but,

There is one more problem is going on, in the statement eachline=r.ReadLine()
it is reading the line but skipping 1 or 2 lines.

assume my "Connection Reset" Error is at line 5 then if skips the line wont go for comparison.
OriginalGriff 19-May-13 7:11am    
Probably it isn't - it can't or that would have been noticed by now! :laugh:

If your code is exactly what is looks like above, then the most likely problem is that the data it not quite what you expect.
Have a closer look - it may be worth using a hex editor - and check that the string you are looking for "Connection reset" is exactly that way in all cases - no case changes ("Connection reset" is not the same as Connection Reset" for example) and no tabs instead of spaces.
If you can't see anything, then post a section of the file here, and indicate which it sees and doesn't see, and I'll try it locally.
santosh.giri 19-May-13 7:26am    
You were right Boss! :D
I was running in a debugging mode so may be it was skipping few of the lines, but now in without debugging mode it is showing me that record.

Thank you so much for your help.
OriginalGriff 19-May-13 7:33am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900