Solved

Streamwriter to remove blank line

Posted on 2014-03-18
7
934 Views
Last Modified: 2014-03-18
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
Comment
Question by:holemania
  • 4
  • 2
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39937116
For intX As Integer = 0 To dgPP.Rows.Count - 2

?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39937234
You could probably just do:
'write row to file
If strRowData <> "" Then
        sr.WriteLine(strRowData)
End If

Open in new window

0
 

Author Comment

by:holemania
ID: 39937504
Tried both and still adding a blank line at the end of my text file.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:holemania
ID: 39937509
Also tried the following without much luck.

If Not String.IsNullOrEmpty(strRowData) Then
   sr.Writeline(strRowData)
End If
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39937597
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
 

Author Comment

by:holemania
ID: 39937664
Ah that works.  Hmmm that's strange.  I thought it was a blank line, but guess not.
0
 

Author Closing Comment

by:holemania
ID: 39937667
Thank you.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now