nik
nik

Reputation: 65

display specific line in text file into textboxes c#

I need to display the partial contents of the ProductData.txt in specific textboxes. The user must first input the product key to check if the product exists and if it does, it would display partial information about the product such as its name, type, and brand. If the product key does not exist, it would display messagebox: item key not found. my problem is that the data being displayed is always the first line. how can i display the line where the product key has been found?

CODE SNIPPET

        string plist = @"ProductData.txt";
        string s_temp = @"stock_temp.txt";

        string[] dataline = File.ReadAllLines(plist);
        if (File.ReadAllText(plist).Contains(txt_pk.Text))
        {
            using (StreamWriter w = File.AppendText(s_temp))
            {
                foreach (var line in dataline)
                {
                    if (line.Contains(txt_pk.Text))
                    {
                        txt_pk.ReadOnly = true;

                        string fileData = File.ReadAllText(plist);
                        string[] parts = fileData.Split(',');

                        txt_pn.Text = parts[2];
                        cmb_brand.Text = parts[3];
                        cmb_type.Text = parts[4];
                    }
                }
                w.Close();
            }
        }
        else
            MessageBox.Show("Item Key not found!");

SAMPLE DATA

ITEM KEY,DATE ADDED,PRODUCT NAME,BRAND,TYPE

0001,10/08/2017,5s,Apple,Phone

0002,10/08/2017,S5,Samsung,Phone

Upvotes: 0

Views: 391

Answers (1)

Jeremy Thompson
Jeremy Thompson

Reputation: 65534

string[] parts = line.Split(','); 

You need to split the line, not read the text file again.

Pro Tip: give Debugging a go and step through your code (pressing F11)

Upvotes: 2

Related Questions