Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What am I doing wrong on this? Chapter 17, Problem 3E // Michael Cole // 6/28/20

ID: 3852872 • Letter: W

Question

What am I doing wrong on this? Chapter 17, Problem 3E

// Michael Cole
// 6/28/2017

// Name spaces for application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Week8
{
    public partial class Form1: Form
    {
        // Get the location where the data file would be created
        string fileLoc = Application.StartupPath +
            "\Datafile.txt";
        /* Constructor of the class to create the GUI of the form */
        public Form1()
        {
            IntializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //form the desired string of the text boxes' text
            string stu_rec = txtLastName.Text + "," +
                txtFirstName.Text + ":" +
                txtIDNumber.Text + ""
                + txtClass.Text + "" + txtGrade.Text;
            try
            {
                //Create an Object of the SteamWriter
                // class to write the data to the file
                SteamWriter dat_file =
                    new SteamWriter(fileLoc, true);
                //Write the data to the file
                dat_file.WriteLine(stu_rec);
                //clear the text of all the textbox controls
                foreach (Control t in this.Controls)
                {
                    if (t is TextBox)
                        t.Text = "";
                }
                //close the stream
                dat_file.Close();
            }
            //throw an exception if the exception occurs
            catch (IOException)
            {
                // show the error message
                MessageBox.Show("Error Occurred. Please try again");
            }
            try
            {
                //create a new object of the SteamReader class to read the data from the same file
                SteamReaderread_file =
                    new SteamReader(fileLoc);
                //variable to read the data
                string read_rec = "";
                //control where the data is to be shown
                txtData.Text = "";
                //read the data till the end of the file
                do
                {
                    //read the data and show it in the text box
                    read_rec = read_file.ReadLine();
                    txtData.Text += read_rec + " ";
                }
                while (read_rec != null);
                //close the stream
                read_file.Close();
            }
            //throw an exception if exception occurs
            catch (IOException)
            {
                //show the message error
                MessageBox.Show("Problem while reading the file. Please try again.");
            }
        }
    }//End class
} // End Namespace

Explanation / Answer

Error: class, interface, not defined properly,

* data type not defined for TextBox

modified code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Week8
{
public Partial class Form1: Form
{
// Get the location where the data file would be created
string fileLoc = Application.StartupPath +
"\Datafile.txt";
/* Constructor of the class to create the GUI of the form */
public Form1()
{
IntializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//form the desired string of the text boxes' text
string stu_rec = txtLastName.Text + "," +
txtFirstName.Text + ":" +
txtIDNumber.Text + ""
+ txtClass.Text + "" + txtGrade.Text;
try
{
               char TextBox;
//Create an Object of the SteamWriter
// class to write the data to the file
SteamWriter dat_file =
new SteamWriter(fileLoc, true);
//Write the data to the file
dat_file.WriteLine(stu_rec);
//clear the text of all the textbox controls
foreach (Control t in this.Controls)
{
          
if (t is TextBox)
t.Text = "";
}
//close the stream
dat_file.Close();
}
//throw an exception if the exception occurs
catch (IOException)
{
// show the error message
MessageBox.Show("Error Occurred. Please try again");
}
try
{
//create a new object of the SteamReader class to read the data from the same file
SteamReaderread_file =
new SteamReader(fileLoc);
//variable to read the data
string read_rec = "";
//control where the data is to be shown
txtData.Text = "";
//read the data till the end of the file
do
{
//read the data and show it in the text box
read_rec = read_file.ReadLine();
txtData.Text += read_rec + " ";
}
while (read_rec != null);
//close the stream
read_file.Close();
}
//throw an exception if exception occurs
catch (IOException)
{
//show the message error
MessageBox.Show("Problem while reading the file. Please try again.");
}
}
}//End class
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote