Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

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
SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland 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
Avatar of Murray Brown

ASKER

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.
ASKER CERTIFIED SOLUTION
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
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
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]
But space should not translate to line break.
SOLUTION
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
opps..

writer.Close()
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]
Thanks very much for the help.