?
Solved

Streamwriter problem. It wont write anything to the file

Posted on 2006-11-18
1
Medium Priority
?
166 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

765 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