Solved

Read CSV with tab delimited data and first two lines/rows empty

Posted on 2011-03-21
2
514 Views
Last Modified: 2013-12-17
This code works for CSV delimited by (;) but I have a CSV file that is delimited by TAB and first two lines are NULL or empty. Can u please help me modify this code to read my CSV TAB delimited.

Thank you


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GridView1.DataSource = GetDataTable(@"c:\fans.csv");
            GridView1.DataBind();
        }
    }


private DataTable GetDataTable(string textFile)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("Age");
        dt.Columns.Add("State");

        if (File.Exists(textFile))
        {
            using (StreamReader sr = new StreamReader(textFile))
            {
                sr.ReadLine();

                while (!sr.EndOfStream)
                {

                    string value = sr.ReadLine();
                 

                    if (!string.IsNullOrEmpty(value))
                    {
                        string[] values = value.Split(new char[] { ';' });


                        if (values.Length > -1)
                        {

                            DataRow row = dt.NewRow();
                            row["Name"] = values[0];
                            row["Age"] = values[1];
                            row["State"] = values[2];
                            dt.Rows.Add(row);
                        }
                    }
                }
            }
        }

        return dt;
    }
0
Comment
Question by:portalvale
2 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 35186054
change ths line

string[] values = value.Split(new char[] { ';' });

Open in new window



to

string[] values = value.Split(new char[] { vbTab });

Open in new window



0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 35186072
Assuming that your file uses only one delimeter per line (either ";" or tab), you can replace:
string[] values = value.Split(new char[] { ';' });

with:
string[] values = value.Split(new char[] { ';' });
if (values == null || values.length == 0)
  values =value.Split(new char[] { '\t' });
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now