Ash
Ash

Reputation: 85

Unable to read values from csv file

I am trying to load a persons data from a csv file, If they type "2" in the ukNumber textbox it will bring the data from the file which starts with ID 2, im having some issues as it will only load the last line in the csv file.

public void search_Click(object sender, EventArgs e)
    {
        string answer = ukNumber.Text;

        string idStr;
        string firstnameStr;
        string surnameStr;
        string jobroleStr;
        string salaryStr;

        using (var reader = new StreamReader(File.OpenRead("C:\\Users\\hughesa3\\Desktop\\details.csv"),
                                 Encoding.GetEncoding("iso-8859-1")))
        {
            while (!reader.EndOfStream || answer == idStr)
            {
                var line = reader.ReadLine();
                var values = line.Split(',');

                idStr = values[0];
                firstnameStr = values[0];
                surnameStr = values[0];
                jobroleStr = values[0];
                salaryStr = values[0];

                richTextBox1.Text = "Name: " + values[1] + "\nSurname: " + values[2] + "\nJob Role: " + values[3] + "\nSalary: £" + values[4];
            }
        }
    }

Upvotes: 0

Views: 163

Answers (4)

Vladimir
Vladimir

Reputation: 1390

Why not using:

  string idYouSearched = "2";
  var encoding = Encoding.GetEncoding("iso-8859-1");
  var csvLines = File.ReadAllLines(fileName,encoding);

  foreach (var line in csvLines )
     {
        var values = line.Split(',');
        if(values[0].Contains(idYouSearched))
           {
              richTextBox1.Text += "Name: " + values[1] + 
                              "\nSurname: " + values[2] + 
                             "\nJob Role: " + values[3] + 
                              "\nSalary: £" + values[4];
           }
      }

  It`s much simpler and you not holding open stream when working with the UI.

Upvotes: 1

Sergey L
Sergey L

Reputation: 1492

As I understand you need only values from the line with the specified ID. You need to break your loop as you reached the correct ID.

            while (!reader.EndOfStream )
            {
                var line = reader.ReadLine();
                var values = line.Split(',');

                idStr = values[0];
                if (answer == idStr) {

                    richTextBox1.Text = "Name: " + values[1] + "\nSurname: " + values[2] + "\nJob Role: " + values[3] + "\nSalary: £" + values[4];

                    break;
                }
            }

Side notes:

1) Reading (or writing) CVS files is not simple task by itself as it might appear. It is better to use a libraries like CsvHelper and A Fast CSV Reader

2) your code misses error handling: you might get empty "line", not all values might be set in this like (so, values[4], for example, will throw out of range exception, and etc.

Upvotes: 1

Giorgi Nakeuri
Giorgi Nakeuri

Reputation: 35780

If you are in the middle of the file this:

!reader.EndOfStream will be true so you have:

while (true || answer == idStr) => while (true)

And it will go to the end of the file of course.

I think you want the following check:

while (!reader.EndOfStream && answer != idStr)

Upvotes: 1

Fang
Fang

Reputation: 2419

Change this line:

richTextBox1.Text = "Name: " + values[1] + "\nSurname: " + values[2] + 
                    "\nJob Role: " + values[3] + "\nSalary: £" + values[4];

to

richTextBox1.Text += "Name: " + values[1] + "\nSurname: " + values[2] + 
                     "\nJob Role: " + values[3] + "\nSalary: £" + values[4];

Alternatively, build the whole text first using a StringBuilder and assign it to richTextBox1.Text in one go.

Upvotes: 1

Related Questions