1 Attachment(s)
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???
Attachment 143517
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:
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.
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.
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.
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.
Re: I am getting a carriage return when writing to a text file from a datatable, why?
Quote:
Originally Posted by
Maverickz
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.
Re: I am getting a carriage return when writing to a text file from a datatable, why?
Quote:
Originally Posted by
jmcilhinney
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.