Solved

Streamwriter to remove blank line

Posted on 2014-03-18
7
1,043 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 143

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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 143

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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