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)
    {

    }
}

}
3
  • why csv? why don't you want to use xml or json? Commented Apr 18, 2016 at 13:05
  • you want read or write csv file? Commented Apr 18, 2016 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 Commented Apr 18, 2016 at 13:08

2 Answers 2

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

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

Comments

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
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.