Hi all, I'm trying to display the first index of each line in my text file, but i'm slow on success.
My code is doing 2 things that is undesirable to me.
1: it replaces the line instead of adding a new line when I add the data, and 2: It duplicates the first line 6 times when it loads. I need help to ensure that it saves the data right and then displays it correctly. Here is my current code:
private void butaddsup_Click(object sender, EventArgs e)//This is the button that adds my data { if (textsupname.Text != "" || textnumb.Text != "") { string path = @"data\Suppliers.txt"; if (!File.Exists(path)) { File.Create(path); TextWriter supWriter = new StreamWriter(path); supWriter.Write(textsupname.Text + ","); supWriter.Write(textnumb.Text + ","); if (checdiscr1.Checked == true) { supWriter.Write(textdescr1.Text + ","); } else { supWriter.Write("0" + ","); } if (textdescr1.Enabled == true && textdescr1.Text == "") { supWriter.Write("0" + ","); } if (checdiscr2.Checked == true) { supWriter.Write(textdescr2.Text + ","); } else { supWriter.Write("0" + ","); } if (textdescr2.Enabled == true && textdescr2.Text == "") { supWriter.Write("0" + ","); } if (checdiscr3.Checked == true) { supWriter.Write(textdescr3.Text + ","); } else { supWriter.Write("0" + ","); } if (textdescr3.Enabled == true && textdescr3.Text == "") { supWriter.Write("0" + ","); } if (checdiscr4.Checked == true) { supWriter.Write(textdescr4.Text + ","); } else { supWriter.Write("0" + ","); } if (textdescr4.Enabled == true && textdescr4.Text == "") { supWriter.Write("0" + ","); } if (checdiscr5.Checked == true) { supWriter.Write(textdescr5.Text + ","); } else { supWriter.Write("0"); } if (textdescr5.Enabled == true && textdescr5.Text == "") { supWriter.Write("0" + ","); } MessageBox.Show("Details have been saved"); supWriter.Close(); } else if (File.Exists(path)) { using (var supWriter = new StreamWriter(path, true)) { supWriter.Write(textsupname.Text + ","); supWriter.Write(textnumb.Text + ","); if (checdiscr1.Checked == true) { supWriter.Write(textdescr1.Text + ","); } else { supWriter.Write("0" + ","); } if (textdescr1.Enabled == true && textdescr1.Text == "") { supWriter.Write("0" + ","); } if (checdiscr2.Checked == true) { supWriter.Write(textdescr2.Text + ","); } else { supWriter.Write("0" + ","); } if (textdescr2.Enabled == true && textdescr2.Text == "") { supWriter.Write("0" + ","); } if (checdiscr3.Checked == true) { supWriter.Write(textdescr3.Text + ","); } else { supWriter.Write("0" + ","); } if (textdescr3.Enabled == true && textdescr3.Text == "") { supWriter.Write("0" + ","); } if (checdiscr4.Checked == true) { supWriter.Write(textdescr4.Text + ","); } else { supWriter.Write("0" + ","); } if (textdescr4.Enabled == true && textdescr4.Text == "") { supWriter.Write("0" + ","); } if (checdiscr5.Checked == true) { supWriter.Write(textdescr5.Text + ","); } else { supWriter.Write("0"); } if (textdescr5.Enabled == true && textdescr5.Text == "") { supWriter.Write("0" + ","); } MessageBox.Show("Details have been saved"); supWriter.Close(); } } } else { MessageBox.Show("Please enter All Required Fields\n marked with a *"); } textsupname.Text = ""; textnumb.Text = ""; textdescr1.Text = ""; textdescr2.Text = ""; textdescr3.Text = ""; textdescr4.Text = ""; textdescr5.Text = ""; if (checdiscr1.Checked == true) { checdiscr1.Checked = false; } if (checdiscr2.Checked == true) { checdiscr2.Checked = false; } if (checdiscr3.Checked == true) { checdiscr3.Checked = false; } if (checdiscr4.Checked == true) { checdiscr4.Checked = false; } if (checdiscr5.Checked == true) { checdiscr5.Checked = false; } }
private void suppliersToolStripMenuItem_Click(object sender, EventArgs e)//Click event to populate listbox listsup { listsup.Text = ""; FormControl(); SupGB.Visible = true; SupGB.BringToFront(); Optionchc.Visible = false; mainFormHandler.Size = new System.Drawing.Size(310, 367); int Counter = 0; StreamReader supReader = new StreamReader(@"data\Suppliers.txt", true); while (!supReader.EndOfStream) { string line = supReader.ReadLine(); string[] tokens = line.Split(new char[] { ',' }, StringSplitOptions.None); foreach (string s in tokens) { if (listsup.Items.Contains(tokens) == false) { listsup.Items.Add(tokens[0]); Counter++; } } } supReader.Close(); }
any help will be great, been googling for days now, not getting it to work.