Load csv file to DataTable (VB) : CSV « ADO.net Database « ASP.NET Tutorial






<%@ Page Language="VB" Debug="true" %>
<script runat="server">

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
</script>
<html>
  <head>
  </head>
  <body>
    <form runat="server">
      <asp:Label id="Label1" runat="server"></asp:Label>
      <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
    </form>
  </body>
</html>








18.43.CSV
18.43.1.Create connection to csv text based database (C#)
18.43.2.Load csv file to DataTable (C#)
18.43.3.Load csv file to DataTable (VB)