Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Cut off when using StreamWriter

Posted on 2007-12-04
5
Medium Priority
?
816 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 2000 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:Joel Coehoorn
ID: 20406897
Show us the real code that uses the datareader- how do you iterate through it?
0
 
LVL 86

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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

886 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