Solved

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

Posted on 2013-01-15
10
467 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
  • 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now