Solved

VB.net Reading text files not working as expected (RichTextBox)

Posted on 2013-01-15
10
478 Views
Last Modified: 2013-01-16
Hi
I am using the following two procedures to write 4 lines to a text file and read the file.
A SQL statement is held in a RichTextBox. Once the files are written using the first procedure, they are read when needed. In the attached file Access.txt it works as normal and in the text file SQL.txt it seems to skip the fourth line. Does this have something to do with the RichTextBox


    Public Sub oSaveQuery(ByVal oFolder As String, ByVal oTextFileName As String)
            Dim lblDataBaseType As Label = Globals.ThisAddIn.oRIGHT.lblDataBaseType
            Dim lblConnectionString As Label = Globals.ThisAddIn.oRIGHT.lblConnectionString
            Dim txtSQL As RichTextBox = Globals.ThisAddIn.oBOTTOM.txtSQL

            Using writer As StreamWriter = New StreamWriter(oFolder & "\" & oTextFileName)
                writer.WriteLine("ELEMENTS Query loading. Please wait a moment...")
                writer.WriteLine(lblDataBaseType.Text)
                writer.WriteLine(lblConnectionString.Text)
                writer.WriteLine(txtSQL.Text)
            End Using
    End Sub

    Public Function oGetQuickRunQueryInfo(ByVal oFolder As String, ByVal oTextFileName As String) As String

            Dim oReader As System.IO.StreamReader
            oReader = System.IO.File.OpenText(oFolder & "\" & oTextFileName & ".eleq")
            Dim DBType As String
            Dim oDataBaseType As String
            Dim oConnectionString As String
            Dim oSQL As String
            Dim oTest As String

            With oReader

                Dim sELEMENTS_TAG As String = .ReadLine 'this is used when the file is double clicked on for file association
                oDataBaseType = .ReadLine
                oConnectionString = .ReadLine
                oSQL = .ReadLine
                oTest = .ReadLine


                Globals.ThisAddIn.oRIGHT.lblDataBaseType.Text = oDataBaseType
                Globals.ThisAddIn.oRIGHT.lblConnectionString.Text = oConnectionString
                Globals.ThisAddIn.oBOTTOM.txtSQL.Text = oSQL
                DBType = oDataBaseType
                .Close()
            End With
            oReader = Nothing

            oGetQuickRunQueryInfo = oDataBaseType & "|" & oConnectionString & "|" & oSQL

    End Function
SQL.txt
Access.txt
0
Comment
Question by:Murray Brown
[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
  • 5
  • 3
  • 2
10 Comments
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 334 total points
ID: 38779979
Do you mean following line

oSQL = .ReadLine

is not working?

Or is

oTest = .ReadLine
?

The last line you seem to be writing is oSQL.
0
 

Author Comment

by:Murray Brown
ID: 38780497
The last line I had written was SQL. The oSQL variable gathers a blank and the oTest variable gathers the last written line. Somehow in the writing process it gathers the RichTextBox text and writes it to two lines. I have spent hours testing different parts os the code and that seems to be the problem.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 334 total points
ID: 38780532
What happens if you change

writer.WriteLine(txtSQL.Text)

to

writer.WriteLine(txtSQL.Text.Replace(VBCRLF, ""))

?
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:Murray Brown
ID: 38781654
I thought that that would have definitely done the trick, but sadly it didn't. I am perplexed as to why  a blank line gets written in like that
0
 

Author Comment

by:Murray Brown
ID: 38781704
I think it has something to do with the line Server=41.72.128.100;Database=MagicBox_MicrosSandBox;Uid= MicrosDBN;Pwd= MBN
There is a space in the connection string
The following shows the first text file (first paragraph) and the second text file, which is fragmented because of the connection string I wrote


ELEMENTS Query loading. Please wait a moment...
Access
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\murbro\Documents\MagicBox.accdb;
SELECT [Transactions].[Amount], [Transactions].[ID] FROM [Transactions] WHERE [Transactions].[ID]>105 And [Transactions].[ID] < 1000

ELEMENTS Query loading. Please wait a moment...
SQL
Server=41.72.128.100;Database=MagicBox_MicrosSandBox;Uid= MicrosDBN;Pwd= MBN

SELECT [Periods].[Co_ID], [Periods].[End Date], [Periods].[Period], [Periods].[Start Date] FROM [Periods]
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38782130
But space should not translate to line break.
0
 
LVL 9

Assisted Solution

by:shorvath
shorvath earned 166 total points
ID: 38782260
try Closing the file before you read it...

Public Sub oSaveQuery(ByVal oFolder As String, ByVal oTextFileName As String)
            Dim lblDataBaseType As Label = Globals.ThisAddIn.oRIGHT.lblDataBaseType
            Dim lblConnectionString As Label = Globals.ThisAddIn.oRIGHT.lblConnectionString
            Dim txtSQL As RichTextBox = Globals.ThisAddIn.oBOTTOM.txtSQL

            Using writer As StreamWriter = New StreamWriter(oFolder & "\" & oTextFileName)
                writer.WriteLine("ELEMENTS Query loading. Please wait a moment...")
                writer.WriteLine(lblDataBaseType.Text)
                writer.WriteLine(lblConnectionString.Text)
                writer.WriteLine(txtSQL.Text)
                writer.Close
            End Using
    End Sub
0
 
LVL 9

Expert Comment

by:shorvath
ID: 38782265
opps..

writer.Close()
0
 

Author Comment

by:Murray Brown
ID: 38783208
Interestingly the .close put the blank line in both

ELEMENTS Query loading. Please wait a moment...
SQL
Server=41.72.123.100;Database=MagicB;Uid=MB_Doppio; Pwd=D0xxxxx0

SELECT [Accounting].[Account Type], [Accounting].[Co_ID], [Accounting].[Display Name], [Accounting].[FinCatCode], [Accounting].[GL ACCOUNT], [Accounting].[ID], [Accounting].[Report Category], [Accounting].[Segment 1], [Accounting].[Segment 1 Desc], [Accounting].[Segment 2], [Accounting].[Segment 2 Desc], [Accounting].[Segment 3], [Accounting].[Segment 3 Desc], [Accounting].[Segment 4], [Accounting].[Type] FROM [Accounting]
‘--------------------------------------------------------------------------------------------------------------------------------
ELEMENTS Query loading. Please wait a moment...
SQL
Server=41.72.123.100;Database=MagicB;Uid=MB_Doppio; Pwd=D0xxxx0

SELECT [Accounting].[Account Type], [Accounting].[Co_ID], [Accounting].[Display Name], [Accounting].[FinCatCode], [Accounting].[GL ACCOUNT], [Accounting].[ID], [Accounting].[Report Category], [Accounting].[Segment 1], [Accounting].[Segment 1 Desc], [Accounting].[Segment 2], [Accounting].[Segment 2 Desc], [Accounting].[Segment 3], [Accounting].[Segment 3 Desc], [Accounting].[Segment 4], [Accounting].[Type] FROM [Accounting]
0
 

Author Closing Comment

by:Murray Brown
ID: 38783215
Thanks very much for the help.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

617 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