We help IT Professionals succeed at work.
Get Started

Manipulating a CSV file using VB.net

rmitwiz
rmitwiz asked
on
3,446 Views
Last Modified: 2012-05-10
I'm new to VB.net programming and I'm trying to create a vb.net  console app that reads a csv file, then filter it to only display certain rows, then change values in specific rows if it meets certain criterias, then write out the new modified information in the csv file.

Below is a sample of the csv file I'm working with. The first row are the column headings

Route,Direction,Milepost,Date_Rated,Surface_Type,Bridge,Construction,MRI,MRUT,TRANS23,WHEEL23,NONWHEEL23
AL0001,5,79.589,4/29/2009,F,0,0,108,0.105,0,0,0
AL0001,5,79.599,4/29/2009,F,0,0,109.5,0.075,0,0,0
AL0001,5,79.609,4/29/2009,F,0,0,46,0.07,0,0,0
AL0001,5,79.619,4/29/2009,F,0,0,56,0.085,0,0,0

"This is the code that I have so far, and it just simply reads the file, but I need it to also do the above tasks mentioned above"

Imports System.IO

Module Module1
    Public Sub Main()
        readCSV()
    End Sub
    Public Function readCSV() As DataTable
        Dim dt As New DataTable()
        dt.Columns.Add("Col1")
        dt.Columns.Add("Col2")
        dt.Columns.Add("Col3")
        dt.Columns.Add("Col4")
        dt.Columns.Add("Col5")
        dt.Columns.Add("Col6")
        dt.Columns.Add("Col7")
        dt.Columns.Add("Col8")
        dt.Columns.Add("Col9")
        dt.Columns.Add("Col10")
        dt.Columns.Add("Col11")
        dt.Columns.Add("Col12")

        Dim sr As New StreamReader("C:\Documents and Settings\meansr\My Documents\Visual Studio 2010\Projects\ReadCSV\Third_and_Sixth_District.csv")

        Dim input As String

        ' Read the first line
        input = sr.ReadLine

        While input IsNot Nothing
            Dim s As String() = Input.Split(New Char() {","c})
            dt.Rows.Add(s)
            input = sr.ReadLine()
        End While

        sr.Close()

        Return dt
    End Function

End Module


Thanks for your help!
Comment
Watch Question
Top Expert 2010
Commented:
This problem has been solved!
Unlock 2 Answers and 5 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE