How do I read a CSV into a HTML Table?

This is a discussion on How do I read a CSV into a HTML Table? within the C# Programming forums, part of the General Programming Boards category; *** SOLVED *** I have a file that I want to read called "links". It's a csv along the lines ...

  1. #1
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90

    Question How do I read a CSV into a HTML Table?

    *** SOLVED ***


    I have a file that I want to read called "links". It's a csv along the lines of:
    Name of Site, Link

    All I want to do is read the file and use each line to create a table element. I've no clue what to do,could someone point me to a tutorial? Here's what I have:
    musicians.aspx.cs
    Code:
    using System;
    using System.IO;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class musicians : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            StreamReader SR;
            string S;
            SR = File.OpenText("c:\\inetpub\\wwwroot\\links");
            S=SR.ReadLine();
            while(S!=null)
            {
    
                S=SR.ReadLine();
            }
            SR.Close();
        }
    }
    musicians.aspx
    Code:
    <&#37;@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="musicians.aspx.cs" Inherits="musicians" Title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:Repeater ID="myTable" runat="server">
        <HeaderTemplate>
            <table>
                <tr>
                    <th>Music Group</th>
                    <th>Website</th>
                </tr>
        </HeaderTemplate>
        
        <ItemTemplate>
            <tr>
                <td>
                    
                </td>
            </tr>
        </ItemTemplate>
        
        <FooterTemplate></table></FooterTemplate>
        </asp:Repeater>
    </asp:Content>
    Last edited by Dragoon_42; 01-29-2008 at 02:47 PM. Reason: solved

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    1) Load CSV into [2, N] array, or if you're more experienced, an ArrayList of custom structs containing your loaded fields.
    2) Build the top of page and opening table.
    3) For N, add a row to the table containing the loaded variables (In your array or ArrayList).
    4) End the table and build the rest of your page.

  3. #3
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    I just got it working so I thought I'd share my code.

    musicians.aspx
    Code:
    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="musicians.aspx.cs" Inherits="musicians" Title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:DataGrid ID="dg" runat="server"></asp:DataGrid>
    </asp:Content>
    musicians.aspx.cs
    Code:
    using System;
    using System.IO;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class musicians : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            StreamReader SR;
            string S;
            string[] tokens;
            int count = 0;
            SR = File.OpenText("c:\\inetpub\\wwwroot\\musicians");
            DataSet myDataSet = new DataSet();
            DataTable myDataTable = new DataTable();
            
            // Get a Column object
            DataColumn myDataColumn = new DataColumn();
            myDataColumn.DataType = System.Type.GetType("System.String");
            myDataColumn.ColumnName = "Musician";
            myDataColumn.ReadOnly = true;
            myDataColumn.Unique = true;
    
            // Now add the column to the column collection
            myDataTable.Columns.Add(myDataColumn);
    
            // Get a Column object
            DataColumn myDataColumn1 = new DataColumn();
            myDataColumn1.DataType = System.Type.GetType("System.String");
            myDataColumn1.ColumnName = "Website";
            myDataColumn1.ReadOnly = true;
            myDataColumn1.Unique = true;
    
            // Now add the column to the column collection
            myDataTable.Columns.Add(myDataColumn1);
    
            S=SR.ReadLine();
            while(S!=null)
            {
                tokens = S.Split(new char[] { ',' });
                count++;
                // Instantiate a DataRow object and add it to the Rows collection
                DataRow myDataRow = myDataTable.NewRow();
                myDataRow["Musician"] = tokens[0];
                myDataRow["Website"] = "<a href=\""+tokens[1]+"\" target=\"_blank\">"+tokens[1]+"<a>";
    
                myDataTable.Rows.Add(myDataRow);
                S=SR.ReadLine();
            }
            myDataSet.Tables.Add(myDataTable);
            dg.DataSource = myDataSet;
            dg.DataBind();
            SR.Close();
        }
    }

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Im wondering would:


    myDataRow["Website"] = "<a href=\""+tokens[1]+"\" target=\"_blank\">"+tokens[1]+"<a>";
    Also work by creating a link / linkbutton object, setting all the properties and then call tostring of the link / linkbutton object? At least it would get rid of writing pure html inside your C# code.

  5. #5
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Quote Originally Posted by GanglyLamb View Post
    Im wondering would:


    myDataRow["Website"] = "<a href=\""+tokens[1]+"\" target=\"_blank\">"+tokens[1]+"<a>";
    Also work by creating a link / linkbutton object, setting all the properties and then call tostring of the link / linkbutton object? At least it would get rid of writing pure html inside your C# code.
    I may have done it wrong, but it didn't work correctly for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Group Project Help/Volunteer
    By DarkDot in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2007, 12:36 AM
  2. html read
    By Ron in forum C Programming
    Replies: 8
    Last Post: 06-12-2006, 06:24 PM
  3. MUD Concept Question
    By mrpickle in forum Game Programming
    Replies: 3
    Last Post: 12-01-2003, 12:45 PM
  4. how to read csv file ?
    By kosong in forum C Programming
    Replies: 5
    Last Post: 11-13-2003, 08:14 PM
  5. progarm doesnt compile
    By kashifk in forum Linux Programming
    Replies: 2
    Last Post: 10-25-2003, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21