Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Cut off when using StreamWriter

Posted on 2007-12-04
5
740 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Optimize the query 5 43
Finding Events logs for IIS website that restarts 2 14
SSIS Start 2 20
PowerShell:  Column widths won't expand 3 17
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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