Solved

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

Posted on 2013-01-15
10
474 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:murbro
[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:murbro
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:murbro
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:murbro
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:murbro
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:murbro
ID: 38783215
Thanks very much for the help.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - Not Sure if Multi-threading is What I Need 4 55
Groupbox Control ? 2 39
.NET Redemption/Outlook Examples 3 33
XML and Linq - reading fields in repeating nodes 3 24
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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