Solved

Streamwriter problem. It wont write anything to the file

Posted on 2006-11-18
1
161 Views
Last Modified: 2012-05-05
It keeps telling me my file is being already being used, but it is creating it.

Here is my code:

 Sub Main()

        Dim sr = IO.File.OpenText("earnings.txt")
        Dim dataline() As String

        Do
            dataline = sr.readline.split(","c)

            Dim Plantcode = dataline(0).Trim
            Dim Deptnum = dataline(1).Trim
            Dim Empnum = dataline(2).Trim
            Dim Lname = dataline(3).Trim
            Dim Fname = dataline(4).Trim
            Dim Earnings = CDbl(dataline(5))
            Dim Ytdearnings = CDbl(dataline(6))
            Dim currentplant As String = Plantcode
            Dim plantearnings As Double
            Dim plantytdearnings As Double
            Dim sw As IO.StreamWriter = System.IO.File.CreateText("outputfile.txt")
            Dim reportearnings As Double
            Dim reportytdearnings As Double

            If Plantcode <> currentplant Then
                sw.WriteLine(plantearnings, plantytdearnings)
                currentplant = Plantcode
                plantearnings = Earnings
                plantytdearnings = Ytdearnings
                Printheadings(Plantcode)
            Else
                plantearnings += Earnings
                plantytdearnings += Ytdearnings
            End If
            reportearnings += Earnings
            reportytdearnings += Ytdearnings
            sw.WriteLine(" ")
            sw.WriteLine(String.Format("{0,-45}{1,11:C}{2,12:C}", _
            "PLANT TOTALS FOR " & currentplant, plantearnings, plantytdearnings))
            sw.WriteLine(" ")
            sw.WriteLine(" ")
        Loop Until sr.peek = -1

        sr.close()

    End Sub
    Sub Printheadings(ByVal plant As String)
        Dim sw As IO.StreamWriter

        sw.WriteLine("{0,-40}{1,15}", "MT MANUFACTURING", Now.ToShortDateString())
        Sw.writeline("PLANT EARNINGS REPORT")
        Sw.Writeline()
        If plant = "ATL" Then
            Sw.writeline("ATLANTIC PLANT")
        ElseIf plant = "CTL" Then
            Sw.writeline("CENTRAL PLANT")
        ElseIf plant = "MTN" Then
            Sw.Writeline("MOUNTAIN PLANT")
        Else
            Sw.writeline("PACIFIC PLANT")
        End If
        sw.WriteLine("")
        sw.WriteLine(String.Format("{0,-7}{1,-7}{2,-20}{3,-12}{4,11}{5,11}", _
        "PLANT", "DEPT", "EMP NUM", "NAME", "CUR EARN", "YEAR-T-D"))
        sw.WriteLine("")

    End Sub

End Module

Thanks
0
Comment
Question by:websuperman
[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
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 17972370
wou have several issues:

>            Dim sw As IO.StreamWriter = System.IO.File.CreateText("outputfile.txt")
you define the variable sw in the  sub main function, but try to use it in the PrintHeadins using a local variable (which is not initialized)

also, you do not close the streamwriter...

finally, you initialize the output in the do loop, simply the wrong place..


this should do better:

 Sub Main()

        Dim sr = IO.File.OpenText("earnings.txt")
        Dim sw As IO.StreamWriter = System.IO.File.CreateText("outputfile.txt")

        Dim dataline() As String

        Do
            dataline = sr.readline.split(","c)

            Dim Plantcode = dataline(0).Trim
            Dim Deptnum = dataline(1).Trim
            Dim Empnum = dataline(2).Trim
            Dim Lname = dataline(3).Trim
            Dim Fname = dataline(4).Trim
            Dim Earnings = CDbl(dataline(5))
            Dim Ytdearnings = CDbl(dataline(6))
            Dim currentplant As String = Plantcode
            Dim plantearnings As Double
            Dim plantytdearnings As Double
            Dim reportearnings As Double
            Dim reportytdearnings As Double

            If Plantcode <> currentplant Then
                sw.WriteLine(plantearnings, plantytdearnings)
                currentplant = Plantcode
                plantearnings = Earnings
                plantytdearnings = Ytdearnings
                Printheadings(Plantcode, sw)
            Else
                plantearnings += Earnings
                plantytdearnings += Ytdearnings
            End If
            reportearnings += Earnings
            reportytdearnings += Ytdearnings
            sw.WriteLine(" ")
            sw.WriteLine(String.Format("{0,-45}{1,11:C}{2,12:C}", _
            "PLANT TOTALS FOR " & currentplant, plantearnings, plantytdearnings))
            sw.WriteLine(" ")
            sw.WriteLine(" ")
        Loop Until sr.peek = -1

        sr.close()
        sw.close()

    End Sub
    Sub Printheadings(ByVal plant As String, sw As IO.StreamWriter)

        sw.WriteLine("{0,-40}{1,15}", "MT MANUFACTURING", Now.ToShortDateString())
        Sw.writeline("PLANT EARNINGS REPORT")
        Sw.Writeline()
        If plant = "ATL" Then
            Sw.writeline("ATLANTIC PLANT")
        ElseIf plant = "CTL" Then
            Sw.writeline("CENTRAL PLANT")
        ElseIf plant = "MTN" Then
            Sw.Writeline("MOUNTAIN PLANT")
        Else
            Sw.writeline("PACIFIC PLANT")
        End If
        sw.WriteLine("")
        sw.WriteLine(String.Format("{0,-7}{1,-7}{2,-20}{3,-12}{4,11}{5,11}", _
        "PLANT", "DEPT", "EMP NUM", "NAME", "CUR EARN", "YEAR-T-D"))
        sw.WriteLine("")

    End Sub

End Module
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…

735 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