Solved

Streamreader in vb.net

Posted on 2009-04-06
1
827 Views
Last Modified: 2013-11-27
Hi,
I am reading a file from richtextbox with streamreader.While reading the file if certain condition is met I am jumping out of the sub by exit sub.i need to again go back to the sub and begin reading the file from that line.Can anybody help me in that code attached.
Don't know how to declare streamreader
Public Sub Get_SQL_Line(ByRef SQL As String)
        Dim EOFile As Boolean
        Dim CreateStatement As Boolean
        Dim CommentBlock As Boolean
        Dim StoredProcedure As Boolean
        Dim strreader As New IO.StreamReader(TempSQLPath)
        Try
 
              If NextLine = String.Empty Then
 
                NextLine = strreader.ReadLine()
 
                LineNo += 1
            End If
            Do
 
                CurrLine = NextLine
                If strreader.EndOfStream Then
                      If NextLine = String.Empty Then Exit Sub
                    If NextLine = vbNewLine Then Exit Sub
                    If NumLines = LineNo Then EOFile = True
                    NextLine = String.Empty
                Else
                    NextLine = strreader.ReadLine
                    LineNo += 1
 
 
                End If
 
 
                'Create statement block
                If Trim(UCase(Left(CurrLine, 16))) = "CREATE PROCEDURE" OrElse Trim(UCase(Left(CurrLine, 14))) = "CREATE TRIGGER" OrElse Trim(UCase(Left(CurrLine, 11))) = "CREATE VIEW" Then
                    CreateStatement = True
                End If
                If Trim(UCase(Left(CurrLine, 15))) = "ALTER PROCEDURE" OrElse Trim(UCase(Left(CurrLine, 7))) = "DECLARE" Then
                    StoredProcedure = True
                End If
                If Trim(UCase(Left(CurrLine, 2))) = "GO" Then
                    StoredProcedure = False
                End If
                'Comment block
                ' added OrElse Right(Trim(CurrLine), 2) = "*/"
                If Left(Trim(CurrLine), 2) = "/*" OrElse Right(Trim(CurrLine), 2) = "*/" Then
                    If Not StoredProcedure Then
                        CommentBlock = True 'Note that SQL Server/Oracle scripts file may have comments formatted as /*  comment */
                    End If
                End If
                If CommentBlock Then
                    'If InStr(CurrLine, "*/") > 0 Then
                    If CurrLine.IndexOf("*/") > 0 Then
 
                        CurrLine = Trim(Right(CurrLine, Len(CurrLine) - (InStr(CurrLine, "*/") + 1)))
                        CommentBlock = False
                    End If
                End If
                If Not CommentBlock Then
                    If Right(CurrLine, 1) = ";" Then
                        SQL = CurrLine
                    ElseIf NextLine.Trim = ";" Then
                        SQL = CurrLine
                        NextLine = String.Empty
                    ElseIf (NextLine.Trim = "" AndAlso Not StoredProcedure) Then
                        SQL = CurrLine
                        NextLine = String.Empty
                    ElseIf (NextLine.Trim).ToUpper = "GO" Then
                        SQL = CurrLine
                        NextLine = String.Empty
                    ElseIf (Not CreateStatement AndAlso CheckNextLine(CurrLine, NextLine) AndAlso Not StoredProcedure) Then
                        SQL = CurrLine
                    ElseIf Not EOFile Then
                        If CurrLine <> "" Then
                            arrNextLine.Add(CurrLine)
                        End If
                        If NextLine <> "" Then
                            arrNextLine.Add(NextLine)
                        End If
                        NextLine = CurrLine & vbNewLine & NextLine
                        Get_SQL_Line(SQL)
                    Else
                        SQL = CurrLine
                    End If
                End If
                If (arrNextLine.Count = 0 AndAlso CurrLine <> "") Then
                    arrNextLine.Add(CurrLine)
                End If
                SQL = CleanSQL(SQL)
                If EOFile Then Exit Sub
                          Loop While SQL = String.Empty
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

Open in new window

0
Comment
Question by:RIAS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 15

Accepted Solution

by:
oobayly earned 500 total points
ID: 24079339
You could open the streamreader before entering your subroutine, then when you leave it because the condition is met, you can call the subroutine with the same streamreader which will remain at the position when you left it.


Sub DoStuff()
  Dim filePath As String
  Dim reader As New StreamReader(filePath)
 
  While (DoRead(reader))
    ' Exited DoRead due to condition found
    ' More logic here
    ' DoRead will be called until the condition isn't met, and the
    ' file has been fully read
  End While
 
  reader.Close()
 
End Sub
 
Function DoRead(reader As StreamReader) As Boolean
  ' You reader logic goes here
  ' If the condition is met, return True to leave the function
 
  Return False
End Function

Open in new window

0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question