• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1189
  • Last Modified:

Streamwriter to remove blank line

Trying to write from a datagridview to a text file.  I need to remove the blank line at the end of the text file.  Anything I can do with the following code?

Dim sr As StreamWriter
    Dim strDelimiterType As String
    Sub Export()

        strFileName = "test.txt"

        strDelimiterType = ","

        'create streamwriter to open and write contents from datagrid
      
        sr = File.CreateText(txtFilePath.Text & "\" & strFileName)


        'creat variable to hold delimiter
        Dim strDelimiter As String = strDelimiterType

        'create variable to hold total number of columns
        Dim intColumnCount As Integer = dgPP.Columns.Count - 1

        'create a variable to hold the row data
        Dim strRowData As String = ""

        'get rest of data to file
        For intX As Integer = 0 To dgPP.Rows.Count - 1
            'reset strRowData
            strRowData = ""

            For intRowData As Integer = 0 To intColumnCount
                strRowData += Replace(dgPP.Rows(intX).Cells(intRowData).Value, strDelimiter, "") & _
                IIf(intRowData < intColumnCount, strDelimiter, "")
            Next intRowData

            'write row to file
            sr.WriteLine(strRowData)
        Next intX

        'close streamwriter
        sr.Close()
    End Sub

Open in new window

0
holemania
Asked:
holemania
  • 4
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
For intX As Integer = 0 To dgPP.Rows.Count - 2

?
0
 
Carl TawnSystems and Integration DeveloperCommented:
You could probably just do:
'write row to file
If strRowData <> "" Then
        sr.WriteLine(strRowData)
End If

Open in new window

0
 
holemaniaAuthor Commented:
Tried both and still adding a blank line at the end of my text file.
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
holemaniaAuthor Commented:
Also tried the following without much luck.

If Not String.IsNullOrEmpty(strRowData) Then
   sr.Writeline(strRowData)
End If
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
then I think you are wanting to skip the WriteLine for the last line, and make that a Write instead?
maybe you are misleaded that the file has an "empty" line at the end...
Dim sr As StreamWriter
    Dim strDelimiterType As String
    Sub Export()

        strFileName = "test.txt"

        strDelimiterType = ","

        'create streamwriter to open and write contents from datagrid
      
        sr = File.CreateText(txtFilePath.Text & "\" & strFileName)


        'creat variable to hold delimiter
        Dim strDelimiter As String = strDelimiterType

        'create variable to hold total number of columns
        Dim intColumnCount As Integer = dgPP.Columns.Count - 1

        'create a variable to hold the row data
        Dim strRowData As String = ""

        'get rest of data to file
        For intX As Integer = 0 To dgPP.Rows.Count - 1
            'reset strRowData
            strRowData = ""

            For intRowData As Integer = 0 To intColumnCount
                strRowData += Replace(dgPP.Rows(intX).Cells(intRowData).Value, strDelimiter, "") & _
                IIf(intRowData < intColumnCount, strDelimiter, "")
            Next intRowData

            'write row to file
            if intX = dgPP.Rows.Count - 1 then
            sr.Write(strRowData)
            else 
            sr.WriteLine(strRowData)
           end if

        Next intX

        'close streamwriter
        sr.Close()
    End Sub
                                  

Open in new window

0
 
holemaniaAuthor Commented:
Ah that works.  Hmmm that's strange.  I thought it was a blank line, but guess not.
0
 
holemaniaAuthor Commented:
Thank you.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now