Click here to Skip to main content
13,903,424 members
Rate this:
Please Sign up or sign in to vote.
See more:

I am developing mobile application using and want to read from csv file. I have written a code as below but still not getting the individual values.

private void readtext()
            var reader = new StreamReader(File.OpenRead("\\Application\\Master.csv"));
            var  listA = new List<String>();
            var  listB = new List<String>();
            var  listC = new List<String>();
            var  listD = new List<String>();
            string vara1, vara2, vara3, vara4;
            while (!reader.EndOfStream)
                var line = reader.ReadLine();
                var values = line.Split(',');

            var firstlistA = listA.ToArray();
            var firstlistB = listB.ToArray();
            var firstlistC = listC.ToArray();
            var firstlistD = listD.ToArray();

Can anyone help me
Updated 18-May-18 0:00am
Manas Bhardwaj 14-May-12 6:11am
Depends more on the data you have in csv. Did you try to debug?
sjelen 14-May-12 6:11am
I don't see a problem here. Can you provide a sample of csv file? A bit more details on the problem?
Saumyavemula 14-May-12 6:41am
The problem is I am getting the value of firstlistA as 'String[9]' but I want the value which is in my CSV file
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

With a couple of error checks in place, it works for me on a test CSV file:
StreamReader reader = new StreamReader(File.OpenRead(@"D:\Temp\ContactsShort.csv"));
List<string> listA = new List<String>();
List<string> listB = new List<String>();
List<string> listC = new List<String>();
List<string> listD = new List<String>();
//string vara1, vara2, vara3, vara4;
while (!reader.EndOfStream)
    string line = reader.ReadLine();
    if (!String.IsNullOrWhiteSpace(line))
        string[] values = line.Split(',');
        if (values.Length >= 4)
string[] firstlistA = listA.ToArray();
string[] firstlistB = listB.ToArray();
string[] firstlistC = listC.ToArray();
string[] firstlistD = listD.ToArray();
So, I would start looking at your CSV file, and at the output you actually get.

BTW: Don't use var as a declaration all the time - it just shows you are too lazy to think about your datatypes. Particularly as Intellisense will fill out the right hand side for you in most case if you do the left...
Saumyavemula 14-May-12 6:37am
Hey with this I am getting the value of firstlistA as 'String[9]' but I want the value which is in my CSV file
OriginalGriff 14-May-12 6:48am
Then you need to put a breakpoint in your code and look at what value is loaded into "line", and then into "values" each time round the loop.
Saumyavemula 14-May-12 6:53am
the entire row which is in csv file (i.e. col1,col2,col3,col4) is loaded in 'line' and in 'values' value is 'string[9]' but i want as 'col1'
OriginalGriff 14-May-12 7:08am
Yes? That is what I would expect. "values" is an array, so it's value is "an array of 9 strings" which is what the debugger is telling you. If you press the "+" symbol beside the name "values" in the debugger, it will expand the actual string entries.
Maciej Los 14-May-12 7:30am
Good answer, 5!
Saumyavemula 15-May-12 3:22am
@OriginalGriff: Can you please help me with what has to be done, I didn't got you as you said press '+' symbol beside the name "values" in the debugger

I tried this but i think i am wrong somewhere
OriginalGriff 15-May-12 3:29am
Put a breakpoint on the line:
When the debugger stops there, hover the mouse over the word "values"
A tool-tip like line will appear, with the variable name (in this case "values") and the current value: "string[9]"
To the left of the name "values" will be a plus sing in a box. hover the mouse over it, or click it. The tool-tip like box will expand and show you the value of the strings in the array.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Try this:

Using System.IO;

StreamReader sr = new StreamReader(@"file.csv");
// for set encoding
// StreamReader sr = new StreamReader(@"file.csv", Encoding.GetEncoding(1250));

string strline = "";
string[] _values = null;
int x = 0;
while (!sr.EndOfStream)
    strline = sr.ReadLine();
    _values = strline.Split(';');
    if (_values.Length >= 6 && _values[0].Trim().Length>0)

also refer some threads:

A Fast CSV Reader[^]

Building a Simple CSV Parser in C#[^]
C#.NET- Reading CSV file[^]
Maciej Los 14-May-12 7:25am
Good answer, 5!
Prasad_Kulkarni 14-May-12 7:31am
Thank you Isomac!
BobJanova 14-May-12 8:37am
I use the LumenWorks one (the first link you posted), it is awesome and has a nice permissive licence. Reading a CSV properly is hard (the spec is not particularly clear and quotes are awkward) and something ideally suited to farming out to a third party library.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190306.1 | Last Updated 18 May 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100