Solved

Streamwriter problem. It wont write anything to the file

Posted on 2006-11-18
1
164 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

731 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