Is there any way to split the text file data using delimiters ans show it in textboxes?

Here is the original data saved in text file

zeeshan
adnan
shams
jawaid

and I want this data in separate 4 textboxes by searching the first name.

Here is my code:

private void button1_Click(object sender, EventArgs e)
{
    FileStream file = new FileStream(@"C:\Users\Zeeshan\Downloads\a.txt", FileMode.Open, FileAccess.Read);
    StreamReader sr = new StreamReader(file);
    sr.ReadLine();
    var textLines = File.ReadAllLines(@"C:\Users\Zeeshan\Downloads\a.txt");

    foreach (var Line in textLines)
    {
        // (str, "\n\\s*")
        //string[] dataArray = Regex.Split('n');
        string[] dataArray = Line.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
        dataArray[0].CompareTo(comboBox1.SelectedItem);
        if (dataArray[0] == comboBox1.SelectedItem.ToString())
        {
            textBox1.Text = (dataArray[0]);
            textBox2.Text = (dataArray[1]);
            textBox3.Text = (dataArray[2]);
            textBox4.Text = (dataArray[3]);
        }
    }
}

The problem with this code is that when I press the button it shows an exception at index[1].

Error is:

"Index was outside the bounds of the array."

Answers


You have several problems in your code:

// FileStream file = new FileStream(@"C:\Users\Zeeshan\Downloads\a.txt", FileMode.Open, FileAccess.Read);
// StreamReader sr = new StreamReader(file);
// sr.ReadLine();
// Above three lines are redundant, because you read all of the text inside next line (ReadAllLines)
var textLines = File.ReadAllLines(@"C:\Users\Zeeshan\Downloads\a.txt");

// This foreach loop is wrong, since textLines already is array of lines from text file (each element is already single line without Environment.NewLine characters)
/*foreach (var Line in textLines)
{
    // (str, "\n\\s*")
    //string[] dataArray = Regex.Split('n');
    string[] dataArray = Line.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
    // This CompareTo is redundant also, since you are not using its result
    dataArray[0].CompareTo(comboBox1.SelectedItem);
    if (dataArray[0] == comboBox1.SelectedItem.ToString())
    {
        textBox1.Text = (dataArray[0]);
        textBox2.Text = (dataArray[1]);
        textBox3.Text = (dataArray[2]);
        textBox4.Text = (dataArray[3]);
    }
}*/

// Instead of foreachloop above, you just need to put values inside textboxes:
if (textLines[0] == comboBox1.SelectedItem.ToString())
{
    textBox1.Text = (textLines[0]);
    textBox2.Text = (textLines[1]);
    textBox3.Text = (textLines[2]);
    textBox4.Text = (textLines[3]);
}

File.ReadAllLines(...) returns a string for each "new line". That means there will be no System.Environment.NewLine in each line. Which correctly causes an "index out of range" exception when you try to access the line parts.

// I'll walk you though your code...
private void button1_Click(object sender, EventArgs e)
{
    /* this is not needed
    FileStream file = new FileStream(@"C:\Users\Zeeshan\Downloads\a.txt", FileMode.Open, FileAccess.Read);
    StreamReader sr = new StreamReader(file);
    sr.ReadLine();
    */

    // this returns an array of all of the lines you have in your text file 
    //   each line is a new element in the array
    var textLines = File.ReadAllLines(@"C:\Users\Zeeshan\Downloads\a.txt");

    // here you try to loop the lines in the text file.
    foreach (var Line in textLines)
    {
        // (str, "\n\\s*")
        //string[] dataArray = Regex.Split('n');

        // here you try to split the line on a new line CR + LF (which doesn't exist) so dataArray only has one element
        string[] dataArray = Line.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

        // this line is useless
        dataArray[0].CompareTo(comboBox1.SelectedItem);

        // this will be true if the current line is the same value as your drop down.
        if (dataArray[0] == comboBox1.SelectedItem.ToString())
        {
            // this line will work
            textBox1.Text = (dataArray[0]);

            // these will always fail.
            textBox2.Text = (dataArray[1]);
            textBox3.Text = (dataArray[2]);
            textBox4.Text = (dataArray[3]);
        }
    }
}

... if you want the text boxes to contain the 4 values from the text files then just do this...

private void button1_Click(object sender, EventArgs e)
{
    var textLines = File.ReadAllLines(@"C:\Users\Zeeshan\Downloads\a.txt");
    textBox1.Text = (textLines[0]);
    textBox2.Text = (textLines[1]);
    textBox3.Text = (textLines[2]);
    textBox4.Text = (textLines[3]);
}

Need Your Help

Zend Framework on shared hosting with PHP as CGI

php zend-framework configuration cgi zend-autoloader

I have been reading a lot before coming to this situation of asking for help.

css image hover link

html internet-explorer firefox google-chrome css3

I have an image, and then a series of links. The hover works as intended on the text links, but when I place it on an image, in chrome and safari it only changes the background color for a portion ...