Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 831059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:08:39+00:00 2026-05-15T04:08:39+00:00

I am trying to populate combo boxes from a text file using comma as

  • 0

I am trying to populate combo boxes from a text file using comma as a delimiter everything was working fine, but now when I debug I get the “Index out of range exception was unhandled” warning. I guess I need a fresh pair of eyes to see where I went wrong, I commented on the line that gets the error //Fname = fields[1];

    private void xViewFacultyMenuItem_Click(object sender, EventArgs e)
    {
        const string fileStaff = "source\\Staff.txt";
        const char DELIM = ',';
        string Lname, Fname, Depart, Stat, Sex, Salary, cDept, cStat, cSex;
        double Gtotal;
        string recordIn;
        string[] fields;

            cDept = this.xDeptComboBox.SelectedItem.ToString();
            cStat = this.xStatusComboBox.SelectedItem.ToString();
            cSex = this.xSexComboBox.SelectedItem.ToString();
            FileStream inFile = new FileStream(fileStaff, FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(inFile);

            recordIn = reader.ReadLine();

            while (recordIn != null)
            {

                fields = recordIn.Split(DELIM);
                Lname = fields[0];
                Fname = fields[1]; // this is where the error appears
                Depart = fields[2];
                Stat = fields[3];
                Sex = fields[4];
                Salary = fields[5];

                Fname = fields[1].TrimStart(null);
                Depart = fields[2].TrimStart(null);
                Stat = fields[3].TrimStart(null);
                Sex = fields[4].TrimStart(null);
                Salary = fields[5].TrimStart(null);

                Gtotal = double.Parse(Salary);

                if (Depart == cDept && cStat == Stat && cSex == Sex)
                {

                    this.xEmployeeListBox.Items.Add(recordIn);

                }
                recordIn = reader.ReadLine();

            }




Source file --


Anderson, Kristen, Accounting, Assistant, Female, 43155
Ball, Robin, Accounting, Instructor, Female, 42723
Chin, Roger, Accounting, Full, Male,59281
Coats, William, Accounting, Assistant, Male, 45371
Doepke, Cheryl, Accounting, Full, Female, 52105
Downs, Clifton, Accounting, Associate, Male, 46887
Garafano, Karen, Finance, Associate, Female, 49000
Hill, Trevor, Management, Instructor, Male, 38590
Jackson, Carole, Accounting, Instructor, Female, 38781
Jacobson, Andrew, Management, Full, Male, 56281
Lewis, Karl, Management, Associate, Male, 48387
Mack, Kevin, Management, Assistant, Male, 45000
McKaye, Susan, Management, Instructor, Female, 43979
Nelsen, Beth, Finance, Full, Female, 52339
Nelson, Dale, Accounting, Full, Male, 54578
Palermo, Sheryl, Accounting, Associate, Female, 45617
Rais, Mary, Finance, Instructor, Female, 27000
Scheib, Earl, Management, Instructor, Male, 37389
Smith, Tom, Finance, Full, Male, 57167
Smythe, Janice, Management, Associate, Female, 46887
True, David, Accounting, Full, Male, 53181
Young, Jeff, Management, Assistant, Male, 43513
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:08:40+00:00Added an answer on May 15, 2026 at 4:08 am

    For the sake of anyone who doesn’t want to look at the mammoth code you’ve posted, here’s the relevant bit:

    while (recordIn != null)
    {
    
        fields = recordIn.Split(DELIM);
        Lname = fields[0];
        Fname = fields[1]; // this is where the error appears
    

    Given the exception you’ve seen, that basically means that recordIn doesn’t contain the delimiter DELIM (a comma). I suggest you explicitly check for the expected size and throw an exception giving more details if you get an inappropriate line. Or if it’s a blank line, as others have suggested (and which does indeed seem likely) you may want to just skip it.

    Alternatively, here’s a short but complete console application which should help you find the problem:

    using System;
    using System.IO;
    
    class Test
    {
        static void Main()
        {
            string[] lines = File.ReadAllLines("source\\Staff.txt");
            for (int i = 0; i < lines.Length; i++)
            {
                string line = lines[i];
                string[] fields = line.Split(',');
                if (fields.Length != 6)
                {
                    Console.WriteLine("Invalid line ({0}): '{1}'",
                                      i + 1, line);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to populate a combo box from a dataset, then bind it to
I am trying to populate a combo box with data but I cant get
hi i am trying to populate a combo box in struts 1.2 but i
I am trying to populate a string with some information from an include file
I am trying to populate a datagrid from a .txt file. I managed to
I am trying to populate a list box from a list in python using
I am trying to populate an asp:Menu control using an XML file. I want
I'm doing something stupid. I'm trying to populate a combo box in a dialog
I trying to populate the dropdown list when page was loaded.But it is not
I am trying to populate a ListView ....but I cant add 2 string values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.