Link to home
Start Free TrialLog in
Avatar of healthmanagement
healthmanagement

asked on

reading CSV file to datatable

Hi I'm trying to read a CSV into a datatable - I'm using the streamreader method I have so code that is working more or less right but it doesnt read ever line on the csv file - I'm stumped

Here is the code and i have uploaded a sample of the CSV data file

my code doesnt read all the lines of code - It read five of the 11 or so lines in the file

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        Dim csvDataTable As DataTable
        csvDataTable = readCSV()
 
        GridView1.DataSource = csvDataTable
        GridView1.DataBind()
    End Sub
    Public Function readCSV()
        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:\lefpdEmployees.txt")
        Dim input As String
        While sr.ReadLine() IsNot Nothing
            input = sr.ReadLine()
            Dim s As String() = input.Split(New Char() {"|"c})
            dt.Rows.Add(s)
        End While
        sr.Close()
        Return dt
    End Function

Open in new window

lefpdEmployees.txt
ASKER CERTIFIED SOLUTION
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial