Will try to post only relevant code as my program is quite large already. Basically the program adds customer information into an arraylist-struct. I’ve got the storing and saving and file loading working flawlessly, but when I’m trying to show the data I’m getting exceptions.
Most of the main code is on a class seperate to the forms, and this particular call comes from “frmViewRecords”.
public void ViewData(int currentRecord)
{
string fn = ((custDetails)datalist[currentRecord]).firstName;
frmViewRecords viewRecords = new frmViewRecords();
viewRecords.WriteData(fn);
}
The above code is what causes the exception, but the code for the messagebox below works fine.
public void LoadData()
{
bool fileLoaded = false;
//Load the database
try
{
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); //Create the filestream
try
{
BinaryFormatter bf = new BinaryFormatter(); //New binary formatter for deserialization
datalist = (ArrayList)bf.Deserialize(fs);
fileLoaded = true; //Update the fileLoaded bool so that it doesn't open the file dialog instance
recordCount = datalist.Count;
MessageBox.Show("" + filename + " loaded successfully."); //Inform the user that the file was automatically loaded
MessageBox.Show("Test: " + ((custDetails)datalist[0]).firstName);
}
catch
{
MessageBox.Show("Could not de-serialise from " + filename, "FILE LOADING PROBLEM", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
fs.Close();
}
catch
{
if (MessageBox.Show("File isn't in the right location, this is normal if a dataset does not yet exist.\n\n If the file exists elsewhere click no and you will be prompted to find the database file, else click yes to create a default file.", "FILE LOADING PROBLEM", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
fileLoaded = true;
CreateDefaultData();
}
}
I have tried ‘string fn = ((custDetails)datalist[0]).firstName;’ to ensure it isn’t the variable causing the problems, and the exception still happens. I am pretty much out of ideas. The problem cannot be with struct or arraylist definition as the the messagebox in LoadData() works fine and outputs the correct information. I tried moving the messagebox to the ViewData method and that also began giving an exception so I can only assume something is wrong with my method?
These methods are on “MainClass.cs” and below is how I called the method from frmViewRecords:
MainClass mainClass = new MainClass();
int currentRecord = 0;
private void LoadData()
{
mainClass.ViewData(currentRecord);
}
It might be worth mentioning that previously, I was calling the data straight from frmViewRecords like so:
txtFirstName.Text = ((MainClass.custDetails)mainClass.datalist[currentRecord].firstName;
But after getting the same exception while the messagebox prompt worked, I rewrote it to the above and still I get the problem so I have no idea what is causing it.
There are no items in the
datalist. Probably the value of therecordCountin theLoadDatais zero as well. Try this: