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

  • SEARCH
  • Home
  • 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 8916407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:14:07+00:00 2026-06-15T05:14:07+00:00

Will try to post only relevant code as my program is quite large already.

  • 0

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.

  • 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-06-15T05:14:09+00:00Added an answer on June 15, 2026 at 5:14 am

    There are no items in the datalist. Probably the value of the recordCount in the LoadData is zero as well. Try this:

    if(datalist.Count != 0) { /* Get the current record */ }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if the post title wasn't clear, I will try to explain a little
this is my first time to post a question here, I will try to
EDIT2 Looks like I can't read properly, disregard this post, I will try to
Guys i think im lost in my own code, I will try explain exactly
I will try and word this question the best I can, for it is
I will try to explain the situation as it is a little bit more
I will try and illustrate my question with examples, I am attempting to create
I will try to make this as clear as I can, but if you
I will try to be as clear as possible because I can't get anybody
I will try to keep this as simple as possible. I have a rather

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.