how to read csv file from url

asked 2023-01-25T16:47:47.4733333+00:00
Ramesh Sunderasan 20 Reputation points

Hi

I want to read csv file from the following URL and bind with DataGrid

http://www.votrecourse.com/medias/xml/?C=M;O=D/timetrial.csv"

pls help me in this

.NET
.NET
Microsoft Technologies based on the .NET software framework.
609 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
1,828 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. answered 2023-01-25T16:52:50.9433333+00:00
    Tasadduq Burney 7,181 Reputation points Microsoft MVP

    Here is an example of how you can read a CSV file from a URL and bind it to a DataGrid in .NET using VB:

    First, you'll need to add a reference to the System.Web assembly in your project.

    Next, use the WebClient class to download the CSV file from the URL:

    Dim client As New WebClient()
    Dim data As String = client.DownloadString("http://www.votrecourse.com/medias/xml/?C=M;O=D/timetrial.csv")
    

    Next, you can use the TextFieldParser class to parse the CSV file and load it into a DataTable:

    Dim table As New DataTable()
    Using parser As New TextFieldParser(New StringReader(data))
        parser.TextFieldType = FieldType.Delimited
        parser.SetDelimiters(",")
        Dim headers() As String = parser.ReadFields()
        For Each header As String In headers
            table.Columns.Add(header)
        Next
        While Not parser.EndOfData
            Dim fields() As String = parser.ReadFields()
            table.Rows.Add(fields)
        End While
    End Using
    

    Finally, you can bind the DataTable to the DataGrid:

    DataGrid1.DataSource = table
    DataGrid1.DataBind()
    

    This is a simple example, you may need to add some error handling and validation depending on your needs.