Question

I just want to say that I only joined stackoverflow a few weeks ago and everyone has been very helpful, thank you. I am down to my last two homework assignments for the semester. What I am trying to do right now is import the data I save to a text file from a program I wrote earlier. Basically the text file is named "client.txt" and the data in it looks like this

account#,firstname,lastname,balance

what I am trying to do now is write a windows form that will read the data in that text file and place it into appropriate corresponding text boxes in the form. Here is my code so far, I believe I am on the right track but I am having trouble because I need the program to do a openfiledialog so I can manually choose the client.txt file and then import the data. Right now I am getting an error "System.IO.StreamReader does not contain a constructor that takes 0 arguments"

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Chapter_17_Ex.Sample_2
{
    public partial class Form1 : Form
    {
        OpenFileDialog filechooser = new OpenFileDialog();
        public Form1()
        {
            InitializeComponent();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            StreamReader fileReader = new StreamReader();

            string inputrecord = fileReader.ReadLine();
            //string filename;
            string[] inputfields;

            if (inputrecord != null)
            {
                inputfields = inputrecord.Split(',');
                txtAccount.Text = inputfields[0];
                txtFirstName.Text = inputfields[1];
                txtLastName.Text = inputfields[2];
                txtBalance.Text = inputfields[3];

            }
            else
            {
                MessageBox.Show("End of File");
            }



        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
Was it helpful?

Solution

To use a StreamReader you need to construct it passing, at least, the filename to read.
This could be accomplished using the OpenFileDialog variable declared at the form global level

  // Show the OpenFileDialog and wait for user to close with OK 
  if(filechooser.ShowDialog() == DialogResult.OK)
  {
       // Check if the file exists before trying to open it 
       if(File.Exists(filechooser.FileName))
       {
           // Enclose the streamreader in a using block to ensure proper closing and disposing
           // of the file resource....
           using(StreamReader fileReader = new StreamReader(filechooser.FileName))
           {
                string inputrecord = fileReader.ReadLine();
                string[] inputfields;
                ....
                // The remainder of your code seems to be correct, but a check on the actual
                // length of the array resulting from the Split call should be added for safety
           }
       }
  }

Notice that both the OpenFileDialog and the StreamReader are complex objects that have many properties and different ways to work. You really need to look at their constructors and properties list in the MSDN documentation to exploit their full functionality

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top