Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-01-15
10
Medium Priority
?
484 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 1336 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 1336 total points
ID: 38780532
What happens if you change

writer.WriteLine(txtSQL.Text)

to

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

?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 664 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

Industry Leaders: 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!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

721 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