Click here to Skip to main content
14,664,400 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to get multiple values from a text file delimited by commas and copy them into a total of 12 textboxes (eg: value '1' goes to textBox1, value '2' goes to textBox2 and so on...)

I am using Windows Forms and I am getting the error below:

System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

The values from my text files are these:

Below is what I tried so far:

namespace WindowsFormsApp18
    public partial class Form1 : Form
        public Form1()

        public class Cliente
            public string DUTA1A { get; set; }
            public string DUTA1B { get; set; }
            public string DUTA1C { get; set; }
            public string DUTA1D { get; set; }
            public string DUTA1E { get; set; }
            public string DUTA1F { get; set; }
            public string DUTA2A { get; set; }
            public string DUTA2B { get; set; }
            public string DUTA2C { get; set; }
            public string DUTA2D { get; set; }
            public string DUTA2E { get; set; }
            public string DUTA2F { get; set; }

        private void button1_Click(object sender, EventArgs e)
            System.IO.StreamReader reader = new System.IO.StreamReader(@"C:\\DUTA\YourFile.txt");
            string line;
            while ((line = reader.ReadLine()) != null)
                string[] dados = line.Split(',');
                int codigo = int.Parse(dados[0]);
                string DUTA1A = dados[0];
                string DUTA1B = dados[1];
                string DUTA1C = dados[2];
                string DUTA1D = dados[3];
                string DUTA1E = dados[4];
                string DUTA1F = dados[5];
                string DUTA2A = dados[6];
                string DUTA2B = dados[7];
                string DUTA2C = dados[8];
                string DUTA2D = dados[9];
                string DUTA2E = dados[10];
                string DUTA2F = dados[11];

                textBox1.Text = DUTA1A;
                textBox2.Text = DUTA1B;
                textBox3.Text = DUTA1C;
                textBox4.Text = DUTA1D;
                textBox5.Text = DUTA1E;
                textBox6.Text = DUTA1F;
                textBox7.Text = DUTA2A;
                textBox8.Text = DUTA2B;
                textBox9.Text = DUTA2C;
                textBox10.Text = DUTA2D;
                textBox11.Text = DUTA2E;
                textBox12.Text = DUTA2F;

Any suggestions?

What I have tried:

Using 6 values there is no errors. Tried to modify the indexes too.
Updated 18-Sep-20 13:36pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

It appears your data has two lines: so your reader.ReadLine while loop is getting called twice. Each time it's called it expects a string array with 12 elements; and it will crash if there are less than 12 elements.

So, why are you using a while loop ?

Fix the data ... remove the line break ... or, use File.ReadAllText.

Also, use the optional StringSplitOptions.RemoveEmptyEntries parameter with Striing.Split.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100