0

I'm having issues with a C# Winforms file IO. The code complies just fine, but then it returns errors on execution.

The output code is here:

private void saveData()
    {
        string fullPath = System.Environment.GetEnvironmentVariable(@"%MyDocuments%\HellsingRPG\");

        StreamWriter writer = new StreamWriter(fullPath + textBox2.Text + ".txt");
        writer.WriteLine(textBox1.Text + "," + textBox2.Text + "," + textBox3.Text + "," + textBox4.Text + "," + comboBox1.SelectedText + "," + 
            numericUpDown25.Value + "," + numericUpDown1.Value + "," + numericUpDown2.Value + "," + numericUpDown3.Value + "," + numericUpDown4.Value + "," +
            numericUpDown5.Value + "," + numericUpDown6.Value + "," + numericUpDown7.Value + "," + numericUpDown8.Value + "," + numericUpDown9.Value + "," +
            numericUpDown10.Value + "," + numericUpDown11.Value + "," + numericUpDown12.Value + "," + numericUpDown13.Value + "," + numericUpDown14.Value
            + "," + numericUpDown15.Value + "," + numericUpDown16.Value + "," + numericUpDown17.Value + "," + numericUpDown18.Value + "," + 
            numericUpDown19.Value + "," + numericUpDown20.Value + "," + numericUpDown21.Value + "," + numericUpDown22.Value);
        writer.Close();
    }

And the code to load the data is here:

private void loadData()
    {
        Stream myStream = null;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        openFileDialog1.InitialDirectory = System.Environment.GetEnvironmentVariable(@"%MyDocuments%\HellsingRPG\");
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.RestoreDirectory = true;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        List<string> myData = parseCSV(System.Convert.ToString(myStream));
                        textBox1.Text = myData[0];
                        textBox2.Text = myData[1];
                        textBox3.Text = myData[3];
                        textBox4.Text = myData[4];
                        comboBox1.SelectedText = myData[5];
                        numericUpDown25.Value = System.Convert.ToDecimal(myData[6]);
                        numericUpDown1.Value = System.Convert.ToDecimal(myData[7]);
                        numericUpDown2.Value = System.Convert.ToDecimal(myData[8]);
                        numericUpDown3.Value = System.Convert.ToDecimal(myData[9]);
                        numericUpDown4.Value = System.Convert.ToDecimal(myData[10]);
                        numericUpDown5.Value = System.Convert.ToDecimal(myData[11]);
                        numericUpDown6.Value = System.Convert.ToDecimal(myData[12]);
                        numericUpDown7.Value = System.Convert.ToDecimal(myData[13]);
                        numericUpDown8.Value = System.Convert.ToDecimal(myData[14]);
                        numericUpDown9.Value = System.Convert.ToDecimal(myData[15]);
                        numericUpDown10.Value = System.Convert.ToDecimal(myData[16]);
                        numericUpDown11.Value = System.Convert.ToDecimal(myData[17]);
                        numericUpDown12.Value = System.Convert.ToDecimal(myData[18]);
                        numericUpDown13.Value = System.Convert.ToDecimal(myData[19]);
                        numericUpDown14.Value = System.Convert.ToDecimal(myData[20]);
                        numericUpDown15.Value = System.Convert.ToDecimal(myData[21]);
                        numericUpDown16.Value = System.Convert.ToDecimal(myData[22]);
                        numericUpDown17.Value = System.Convert.ToDecimal(myData[23]);
                        numericUpDown18.Value = System.Convert.ToDecimal(myData[24]);
                        numericUpDown19.Value = System.Convert.ToDecimal(myData[25]);
                        numericUpDown20.Value = System.Convert.ToDecimal(myData[26]);
                        numericUpDown21.Value = System.Convert.ToDecimal(myData[27]);
                        numericUpDown22.Value = System.Convert.ToDecimal(myData[28]);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }
    }

And that compiles just fine. But when I use it, I get the following errors:

"Could not find file "C:\Users\collmark\Documents\Visual Studio 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Release\System.IO.Filestream".

"Error: Could not read file from disk. Original error: Index out of range. Must be non-negative and less than the size of the collection. Parameter name: index."

Thanks

5
  • 1
    Hi Mark, welcome to P.SE. We're not a debugging service. Commented Jul 30, 2015 at 17:05
  • well as someone trying to teach himself c# I have run only into elitist people who care only about making themselves look good and don't care about helping people trying to learn about programming. you appear to be pretty much like stackoverflow. I was hoping you were different. Commented Jul 30, 2015 at 17:17
  • I'm sorry you've had problems with other people in the past, but you clearly showed no effort to meet our site's posting guidelines. That's not me being elitist. Commented Jul 30, 2015 at 17:26
  • Mark, System.Convert.ToString(myStream)); wouldn't work, you'll get a string like System.IO.Filestream. Use StreamReader or File.ReadAllText to read text file. Commented Jul 30, 2015 at 17:26
  • Theres definitely not enough commas Commented Aug 1, 2015 at 20:22

1 Answer 1

2

your save data seems to save 22 fields while the read expects 28.

I suspect the myData object does not contain the fields index you are trying to read, hence index out of range.

do yourself a favour when printing exception data don't limit yourself to the message but print the whole stack trace, it will tell you which line is faulty giving you a hint at the actual problem.

MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.ToString());

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

3 Comments

well it only appears to save 22 fields but I double checked and there are the same number of fields. also, I don't get this error in Visual Studio 2015, I get it in windows after I have run it. I get zero errors in Visual Studio Express.
you still have an index problem, perhaps the parseCSV does not return the right amount of fields, does it skip empty fields ? does it handle quoting properly ? What line does the stacktrace point you towards ? you can try debugging and step through the code line by line see where it crashes
The top text writing out the csv definitely shows a different number of fields .. which you split it.. you should be able to see that the number of fields you have is different. Also I pray that none of your text has commas in!

Your Answer

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