In C#, having problems with my code when trying to load a file. It displays a er
ID: 3570590 • Letter: I
Question
In C#, having problems with my code when trying to load a file. It displays a error "Index was outside the bounds of the array." Please help if you can.
Here is the assignment:
(File of Student Grades) Create a program that stores student grades in a text file. The file
should contain the name, ID number, class taken and grade of every student. Allow the user to load
a grade file and display its contents in a read-only TextBox. The entries should be displayed in the
following format:
LastName, FirstName: ID# Class Grade
We list some sample data below:
Jones, Bob: 1 "Introduction to Computer Science" "A-"
Johnson, Sarah: 2 "Data Structures" "B+"
Smith, Sam: 3 "Data Structures" "C"
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace StudentGrade
{
public partial class Form1 : Form
{
StreamWriter fileWriter;
FileStream output;
StreamReader fileReader;
public Form1()
{
InitializeComponent();
}
private void btnEnter_Click(object sender, EventArgs e)
{
txtOutput.Text += txtLastName.Text + ", ";
txtOutput.Text += txtFirstName.Text + ", ";
txtOutput.Text += txtID.Text + ", ";
txtOutput.Text += txtClass.Text + ", ";
txtOutput.Text += txtGrade.Text + " ";
}
private void btnSaveAs_Click(object sender, EventArgs e)
{
//create save file dialog
SaveFileDialog fileChooser = new SaveFileDialog();
fileChooser.Filter = "txt files (*.txt)|*.txt";
//get dialog result info
DialogResult result = fileChooser.ShowDialog();
//hold filename
string fileName;
//do not process if they hit cancel
if (result == DialogResult.Cancel)
return;
//get the filename
fileName = fileChooser.FileName;
//error checking on filename
if (fileName == "" || fileName == null)
{
MessageBox.Show("Error in filename");
}
else
{
// process file
try
{
//save the file with filestream
output = new FileStream(fileName,
FileMode.OpenOrCreate, FileAccess.Write);
// the file to where data can be written
fileWriter = new StreamWriter(output);
//put text in file
fileWriter.WriteLine(txtOutput.Text);
}
catch (Exception x)
{
//if an error show message box
MessageBox.Show(x.Message);
}
finally
{
//close resources
fileWriter.Close();
output.Close();
}
}
}
private void btnLoad_Click(object sender, EventArgs e)
{
//create save file dialog
OpenFileDialog fileChooser = new OpenFileDialog();
fileChooser.Filter = "txt files (*.txt)|*.txt";
//get dialog result info
DialogResult result = fileChooser.ShowDialog();
//hold filename
string fileName;
//do not process if they hit cancel
if (result == DialogResult.Cancel)
return;
//get the filename
fileName = fileChooser.FileName;
//simple error checking on filename
if (fileName == "" || fileName == null)
{
MessageBox.Show("Error in filename");
}
else
{
//it is ok to process this file
try
{
//set up filestream object
output = new FileStream(fileName,
FileMode.Open, FileAccess.Read);
//give the filestream to streamreader
fileReader = new StreamReader(output);
//strings to hold read data
string inputLine;
string[] fields;
//loop through the records in the file
while (fileReader.EndOfStream != true)
{
//get each line one at a time
inputLine = fileReader.ReadLine();
fields = inputLine.Split(',');
txtOutput.Text += fields[1] + ", ";
txtOutput.Text += fields[2] + ", ";
txtOutput.Text += fields[3] + ", ";
}
}
catch (Exception ex)
{
//if an error show message box
MessageBox.Show(ex.Message);
}
finally
{
//close resources
fileReader.Close();
output.Close();
}
}
}
private void btn2_Click(object sender, EventArgs e)
{
//create save file dialog
OpenFileDialog fileChooser = new OpenFileDialog();
fileChooser.Filter = "txt files (*.txt)|*.txt";
//get dialog result info
DialogResult result = fileChooser.ShowDialog();
//hold filename
string fileName;
//do not process if they hit cancel
if (result == DialogResult.Cancel)
return;
//get the filename
fileName = fileChooser.FileName;
//simple error checking on filename
if (fileName == "" || fileName == null)
{
MessageBox.Show("Error in filename");
}
else
{
// process file
try
{
//set up filestream object
output = new FileStream(fileName,
FileMode.Open, FileAccess.Read);
//give the filestream to streamreader
fileReader = new StreamReader(output);
//strings to hold read data
string inputLine;
string[] fields;
//loop through the records in the file
while (fileReader.EndOfStream != true)
{
//get each line one at a time
inputLine = fileReader.ReadLine();
fields = inputLine.Split(',');
txtOutput.Text += fields[1] + ", ";
txtOutput.Text += fields[2] + ", ";
txtOutput.Text += fields[3] + ", ";
}
}
catch (Exception x)
{
//if an error show message box
MessageBox.Show(x.Message);
}
finally
{
//close resources
fileReader.Close();
output.Close();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Explanation / Answer
private void btnSaveAs_Click(object sender, EventArgs e)
{
//create save file dialog
SaveFileDialog fileChooser = new SaveFileDialog();
fileChooser.Filter = "txt files (*.txt)|*.txt";
//get dialog result info
DialogResult result = fileChooser.ShowDialog();
//hold filename
string fileName;
//do not process if they hit cancel
if (result == DialogResult.Cancel)
return;
//get the filename
fileName = fileChooser.FileName;
//simple error checking on filename
if (fileName == "" | fileName == null)
{
MessageBox.Show("Error in filename");
}
else
{
//it is ok to process this file
try
{
//save the file via filestream
output = new FileStream(fileName,
FileMode.OpenOrCreate, FileAccess.Write);
//set the file to where data can be written
fileWriter = new StreamWriter(output);
//put text in file
fileWriter.WriteLine(txtOutput.Text);
}
catch (Exception ex)
{
//if an error show message box
MessageBox.Show(ex.Message);
}
finally
{
//close resources
fileWriter.Close();
output.Close();
}
}
}