Results 1 to 7 of 7

Thread: I am getting a carriage return when writing to a text file from a datatable, why???

  1. #1

    Thread Starter
    Lively Member Christhemist's Avatar
    Join Date
    Sep 2016
    Location
    Nevada
    Posts
    116

    I am getting a carriage return when writing to a text file from a datatable, why???

    I am writing to a text file from a datatable using the StreamWriter, but once it gets to a certain point in the data it returns it to the next line. Here is my code:

    Code:
    Dim strFilePath As String
     Dim stm As System.IO.StreamWriter
    
                strFilePath = "C:\WORK\Finished.txt"
                stm = New System.IO.StreamWriter(strFilePath, False)
                Dim i As Integer
    
                For i = 0 To dt.Columns.Count - 2
    
                    stm.Write(dt.Columns(i).ColumnName & ",")
    
                Next i
                stm.Write(dt.Columns(i).ColumnName)
                stm.WriteLine()
    
                For Each row As DataRow In dt.Rows
                    Dim array() As Object = row.ItemArray
    
                    For i = 0 To array.Length - 2
    
                        stm.Write(array(i).ToString() & ",")
    
                    Next i
    
                Next row
    Here is the output you can see after the first address it goes to the next line below???
    Name:  OutputExample.jpg
Views: 1825
Size:  51.2 KB

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: I am getting a carriage return when writing to a text file from a datatable, why?

    Firstly, you aren't writing a line break at the end of a record yourself, which you should be. You should be following your inner loop with a call to WriteLine to put a line break after each record. Alternatively, you could get rid of the inner loop altogether and just do this:
    vb.net Code:
    1. stm.WriteLine(String.Join(",", row.ItemArray))
    As for the issue, if you're getting a line break that you're not writing explicitly then the text in the DataTable must contain a line break.

  3. #3

    Thread Starter
    Lively Member Christhemist's Avatar
    Join Date
    Sep 2016
    Location
    Nevada
    Posts
    116

    Re: I am getting a carriage return when writing to a text file from a datatable, why?

    I have tried moving rows around in the original datasource in an attempt to troubleshoot this issue but for whatever reason it likes to put a line break after the "address" field. Because now, after having implemented your supplied code in place of the inner loop, it now puts a line break after each address.

    As a side note: I am importing a .dbf file into my datatable, not sure if this will effect the output, or having to do with putting line breaks in the datatable.

  4. #4
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    482

    Re: I am getting a carriage return when writing to a text file from a datatable, why?

    Try something like this:
    Code:
                For Each row As DataRow In dt.Rows
                    Dim array() As Object = row.ItemArray
    
                    For i = 0 To array.Length - 2
    
                        stm.Write((array(i).ToString() & ",").Replace(vbCrLf, ""))
    
                    Next i
    
                    stm.WriteLine()
                Next row
    NOTES: The "Replace" function looks for Carriage Return + Line Feed characters and replaces them with "". IF that doesn't work try them separately: .Replace(vbCr, "") and .Replace(vbLf, ""). One of those 3 should work.

    Also I added the extra WriteLine that jmc was referencing to make sure each row is on a different line.
    Last edited by Maverickz; Dec 22nd, 2016 at 12:36 AM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: I am getting a carriage return when writing to a text file from a datatable, why?

    You seem to think that this extra line break is materialising out of nowhere or perhaps as a consequence of your code. It's not. It's in the original data. Line breaks are just text themselves. If the text in your database contains line breaks then, when you write that text out, it will include those line breaks. Why would it not? Most likely the issue is that the data was read incorrectly in the first place.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: I am getting a carriage return when writing to a text file from a datatable, why?

    Quote Originally Posted by Maverickz View Post
    Try something like this:
    Code:
                For Each row As DataRow In dt.Rows
                    Dim array() As Object = row.ItemArray
    
                    For i = 0 To array.Length - 2
    
                        stm.Write((array(i).ToString() & ",").Remove(vbCrLf))
    
                    Next i
    
                    stm.WriteLine()
                Next row
    NOTES: The "Remove" function looks for Carriage Return + Line Feed characters and removes them. IF that doesn't work try them separately: .Remove(vbCr) and .Remove(vbLf). One of those 3 should work.

    Also I added the extra WriteLine that jmc was referencing to make sure each row is on a different line.
    That code can't work. The String.Remove method requires a numeric index at which characters should be removed. You are perhaps confusing it with the String.Replace or String.Trim method.

  7. #7
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    482

    Re: I am getting a carriage return when writing to a text file from a datatable, why?

    Quote Originally Posted by jmcilhinney View Post
    That code can't work. The String.Remove method requires a numeric index at which characters should be removed. You are perhaps confusing it with the String.Replace or String.Trim method.
    Yep, should have been replace. I fixed the other post. Thanks.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width