Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cut off when using StreamWriter

Posted on 2007-12-04
5
Medium Priority
?
798 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
[X]
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
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:jcoehoorn
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

704 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