0

Im trying to read some data from a csv file and display it in c# this works fine but i have 2 different rows in my csv file which i'll be adding too.

I want them to be accessible if say someonet types '1' into the ukNumber field it will pull all of their data.

atm no matter what i type it always displays the last line in my csv file.

namespace Appraisal
{


public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }
    private void ukNumber_TextChanged(object sender, EventArgs e)
    {

    }

    public void search_Click(object sender, EventArgs e)
    {
        using (var reader = new StreamReader(File.OpenRead("C:\\Users\\hughesa3\\Desktop\\details.csv"),
                                 Encoding.GetEncoding("iso-8859-1")))
        {
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(',');

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

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

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void apprasialCode_TextChanged(object sender, EventArgs e)
    {
    }

    private void apprasialBtn_Click(object sender, EventArgs e)
    {
    }

    private void ukNumberLabel_Click(object sender, EventArgs e)
    {

    }
}

}
Ash
  • 85
  • 2
  • 11
  • why csv? why don't you want to use xml or json? – Alexej Sommer Apr 18 '16 at 13:05
  • you want read or write csv file? – Amine Apr 18 '16 at 13:06
  • Im trying to read from csv, when user types the ID into the field and clicks search it will load up the rest of the info in the textbox. and im using csv just as someone I work with set me a task to try as I am learning c# for my apprenticeship – Ash Apr 18 '16 at 13:08

2 Answers2

0

There is a CsvHelper available via Nuget

https://www.nuget.org/packages/CsvHelper/

See following question: using csvhelper (nuGET) with C# MVC to import CSV files

Community
  • 1
  • 1
Sebastian Siemens
  • 2,302
  • 1
  • 17
  • 24
0

to read data from csv file:

using (var reader = new StreamReader(File.OpenRead("c:/yourfile.csv"), 
                                     Encoding.GetEncoding("iso-8859-1"))) 
{
    while (!reader.EndOfStream)
    {
        var line = reader.ReadLine();
        var values = line.Split(';'); // replace ';' by the your separator

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

        //convert string
    }
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Amine
  • 1,198
  • 11
  • 19