Solved

Cut off when using StreamWriter

Posted on 2007-12-04
5
709 Views
Last Modified: 2013-11-26
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
Comment
Question by:jamesrbarnes
5 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20406790
why this?:
s.BaseStream.Seek(0, SeekOrigin.End)
       
also try to close the stream before returning from Sub:
fs.Close();
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20406897
Show us the real code that uses the datareader- how do you iterate through it?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20407297
I agree with jaimie..

Adding:

        s.Close()
        fs.Close()

Fixed the problem for me...
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20632925
Forced accept.

Computer101
EE Admin
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

15 Experts available now in Live!

Get 1:1 Help Now