Public Function GetCsvData(ByVal strFolderPath As String, ByVal strFileName As String) As DataTable
Dim strConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFolderPath & ";Extended Properties=Text;"
Dim conn As New OleDbConnection(strConnString)
Try
conn.Open()
Dim cmd As New OleDbCommand("SELECT * FROM [" & strFileName & "]", conn)
Dim da As New OleDbDataAdapter()
da.SelectCommand = cmd
Dim ds As New DataSet()
da.Fill(ds)
da.Dispose()
Return ds.Tables(0)
Catch
Return Nothing
Finally
conn.Close()
End Try
End Function
Second Solution:Convert CSV File to data table[
^]
Third Solution:
]]>
Sub Page_Load (Sender As Object, E As EventArgs)
If Not IsPostBack Then
Dim MyFileName as String
Dim ObjectStreamReader as System.IO.StreamReader
Dim ColorTable As New System.Data.DataTable("Colors")
Dim fileRow() As String
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
dc = new System.Data.DataColumn("Hex",GetType(String))
ColorTable.Columns.Add(dc)
dc = new System.Data.DataColumn("String",GetType(String))
ColorTable.Columns.Add(dc)
MyFileName = Page.MapPath("Data.csv")
Try
ObjectStreamReader = new System.IO.StreamReader (MyFileName)
While ObjectStreamReader.Peek() > -1
dr = ColorTable.NewRow()
fileRow = ObjectStreamReader.ReadLine().Split(",")
dr(0) = fileRow(0)
dr(1) = fileRow(1)
ColorTable.Rows.Add(dr)
End While
Label1.Text = "Select a color:"
DropDownList1.DataSource = ColorTable
DropDownList1.DataTextField = "String"
DropDownList1.DataValueField = "Hex"
DropDownList1.DataBind()
Catch ObjectError as Exception
Label1.Text = ObjectError.Message
DropDownList1.Visible = False
Finally
If Not ObjectStreamReader Is nothing Then
ObjectStreamReader.Close()
End If
End Try
End If
End Sub