Solved

Streamwriter problem. It wont write anything to the file

Posted on 2006-11-18
1
155 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
1 Comment
 
LVL 142

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

12 Experts available now in Live!

Get 1:1 Help Now