Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Below is my CSV content

Number of managed system,
80, compliance
7, non_colpliance

I need only these values (80 and 7). I need to skip the rest of the data

Below is my code

C#
StreamReader sr = new StreamReader(filepath);
            string line = sr.ReadLine();
            string[] value = line.Split(',');
            DataTable dt = new DataTable();
            DataRow row;
            foreach (string dc in value)
            {
                dt.Columns.Add(new DataColumn(dc));
            }
            while ( !sr.EndOfStream )
            {
                value = sr.ReadLine().Split(',');
                if(value.Length == dt.Columns.Count)
                {
                    row = dt.NewRow();
                    row.ItemArray = value;
                    dt.Rows.Add(row);
                }
            }


Please help me to achive this.

Thanks in advance.
Posted
Comments
Dholakiya Ankit 13-Sep-13 4:28am    
you need values from column 80 nd 7 or whole column 80 and 7?

Add another ReadLine in the very beginning; it will skip first line. When you use result of Split, use only the element of the resulting array at index 0. That's all.

Better yet, as your file is small, instead of using String.Reader, use System.IO.File.ReadAllLines. It will return you the array of lines, so you can ignore the line with the index 0. Please see:
http://msdn.microsoft.com/en-us/library/system.io.file.readalllines.aspx[^].

—SA
 
Share this answer
 
v2
Comments
[no name] 12-Sep-13 0:57am    
+5..
Sergey Alexandrovich Kryukov 12-Sep-13 0:58am    
Thank you.
—SA
 
Share this answer
 
Why to read CSV file using Reader[^] if it possible to use OLEDB[^] to import data[^]?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900