• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 867
  • Last Modified:

Cut off when using StreamWriter

I am trying to write a bunch of records returned from a SqlDataReader query to a text file.  When I use  StreamWriter the output stops at the same place every time.  I am using VB.Net 2005.

I can illustrate it using the following code.  Rather than returning a file with 2000 rows, it stops at 1550:

    Public Sub Main()
        Dim x As Integer
        Dim sFileName As String
        sFileName = "C:\Test.txt"
        Dim fs As New FileStream(sFileName, FileMode.Create, FileAccess.Write)
        Dim s As New StreamWriter(fs)
        s.BaseStream.Seek(0, SeekOrigin.End)
        For x = 1 To 2000
            s.WriteLine(x)
        Next
        Dts.TaskResult = Dts.Results.Success
    End Sub

What am I doing wrong?
0
jamesrbarnes
Asked:
jamesrbarnes
1 Solution
 
Jaime OlivaresSoftware ArchitectCommented:
why this?:
s.BaseStream.Seek(0, SeekOrigin.End)
       
also try to close the stream before returning from Sub:
fs.Close();
0
 
Joel CoehoornDirector of Information TechnologyCommented:
Show us the real code that uses the datareader- how do you iterate through it?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I agree with jaimie..

Adding:

        s.Close()
        fs.Close()

Fixed the problem for me...
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now