WriteLine problems with Do Until Loop

lkrterp

Member
Joined
Jul 10, 2007
Messages
21
Programming Experience
Beginner
Hi All!

The following code is not writing to the text file and I was wondering if any one could tell me what I have done wrong. If I take the Do Until Loop off it will write the first line to the file, so I believe the problem is in the Loop.

VB.NET:
'Verify that all ecodes are in cross reference table
        Dim DB As ADODB.Connection
        Dim rs As ADODB.Recordset
        DB = New ADODB.Connection
        DB.Open('left blank here for security reasons')

        sSQL = "SELECT * FROM twgi_pay_accrual "
        sSQL += "WHERE NOT EXISTS(SELECT * FROM twgi_pay_crsref "
        sSQL += "WHERE ecode = ecode_accr)"

        rs = DB.Execute(sSQL)

        If Not rs.EOF Then

            Dim stext As String = "C:\payaccrual_code_" & jedate.Substring(0, 2) & jedate.Substring(6) & ".txt"
            Dim f As System.IO.StreamWriter = System.IO.File.CreateText(stext)

            Do Until rs.EOF
                f.WriteLine(rs(1).Value)
                rs.MoveNext()
            Loop

            MsgBox("Need to add new etime code to crossreference table" & vbCrLf & "New codes located at " & stext)

            f.Close()
            f = Nothing
            DB.Close()
            DB = Nothing

            Exit Sub

        End If
 
The only thing that I noticed is that you aren't flushing your streamwriter anywhere. So right after your loop, add this line of code:

f.Flush

That will make sure that any unwritten data still in the buffer will be written down to the stream. I'm not sure if that will solve your problem, but it might help.
 
How would that help? Close already flushes, there is no difference to adding another Flush or not.
 
hmm,

I never knew that. I guess i've just been very redundant in my coding. Thanks for letting me know.
 
I don't know why it was not working before, but it works now that I added the Flush. Makes no sense to me since I have had this same code and the Do Until Loop work for me in the past with other vb.net projects.

If anyone can shed some light on this, I think we would all be interested.
 
Yes, AutoFlush=False only does a little buffering for better performance, it has no effect otherwise for a file stream that is closed properly (*). AutoFlush=True is used for streams that need immediate output for each write. Explicit Flush also resets the internal encoder, which in some cases you shouldn't, for example with the default utf8 encoding "where certain characters can only be encoded after the encoder receives the adjacent character or characters."

(*) only exception is a file that is read/written by several processes/threads simultaneously in shared mode, but that is rare.
 
Back
Top