Home > Software engineering >  StreamWriter stops writing in the meddle of writing to a text file halfway
StreamWriter stops writing in the meddle of writing to a text file halfway

Time:11-06

I have a list that holds 14 values coming from SQL server table, and I am looping to pass values to the StreamWriter object with WriteLine to write the values in the required format on a text file. My issue is, while on debug I can see all values being added to my print variable; however, when the loop completes it only prints 9 values and the 9th value gets cut on the text file. Please refer to my screenshots.

Using resource As New DevelopmentEntities
            Dim sw As StreamWriter
            Dim list As New List(Of ContactU)
            list = resource.ContactUs.ToList()
            sw = My.Computer.FileSystem.OpenTextFileWriter(outputpath & "Web_ContactUs_" & countValue & ".txt", True)
            For Each item In list
                list = list.OrderBy(Function(x) item.Submitters_First_Name_First).ToList()
                countValue = countValue   1
                If Not item.Entry_Id.ToString Is "" Then
                    Dim valueItem = list
                    Dim ssnValue = "UANPF" & item.Last_4_of_SSN.TrimStart.TrimEnd
                    Dim raw_date As Date = item.Entry_Date.TrimEnd
                    Dim entry_date As Date = raw_date.ToString("MM/dd/yyyy")
                    Dim concatinatedFilepath = textfilepath & item.Submitters_First_Name_First.Replace(" ", "_").TrimStart.TrimEnd _
                        & "_" & item.Submitters_Last_Name_Last.Replace(" ", "_").TrimStart.TrimEnd _
                        & "_" & item.Last_4_of_SSN.TrimStart _
                        & "_" & countValue
                    Dim print = filetype &
                        "|" & concatinatedFilepath &
                        ".txt" &
                        "|" & ssnValue &
                        "|" & incoming &
                        "|" & addresschange &
                        "|" & entry_date &
                        "|" & ITP
                    sw.WriteLine(print)
                Else
                    'Do nothing will clear invalid data
                End If
            Next

        End Using

Visual Studio on Debug modeWhat it prints on the text file

CodePudding user response:

Try this:

Using resource As New DevelopmentEntities
        Dim sw As StreamWriter
        Dim list As New List(Of ContactU)
        list = resource.ContactUs.ToList()
        sw = My.Computer.FileSystem.OpenTextFileWriter(outputpath & "Web_ContactUs_" & countValue & ".txt", True)
        For Each item In list
            list = list.OrderBy(Function(x) item.Submitters_First_Name_First).ToList()
            countValue = countValue   1
            If Not item.Entry_Id.ToString Is "" Then
                Dim valueItem = list
                Dim ssnValue = "UANPF" & item.Last_4_of_SSN.TrimStart.TrimEnd
                Dim raw_date As Date = item.Entry_Date.TrimEnd
                Dim entry_date As Date = raw_date.ToString("MM/dd/yyyy")
                Dim concatinatedFilepath = textfilepath & item.Submitters_First_Name_First.Replace(" ", "_").TrimStart.TrimEnd _
                    & "_" & item.Submitters_Last_Name_Last.Replace(" ", "_").TrimStart.TrimEnd _
                    & "_" & item.Last_4_of_SSN.TrimStart _
                    & "_" & countValue
                Dim print = filetype &
                    "|" & concatinatedFilepath &
                    ".txt" &
                    "|" & ssnValue &
                    "|" & incoming &
                    "|" & addresschange &
                    "|" & entry_date &
                    "|" & ITP
                sw.WriteLine(print)
            Else
                'Do nothing will clear invalid data
            End If
        Next
        sw.Close()
    End Using
  • Related