0

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]; } } } 
1
  • 5
    You should append to the richTextBox1.Text. At the moment you overwrite what is in there. Commented Apr 19, 2016 at 8:53

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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) 

Comments

1

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.

Comments

1

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. 

1 Comment

File.ReadLines is a better style (you don't want ReadAllLines in the context); richTextBox1.Text += can lead to constant re-painting and blinking; collect all data in StringBuilder and then (once!) assign the data collected to richTextBox1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.