Question

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

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 + "\r\n";
                }
                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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 + "\r\n";
}
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
}

Add a comment
Know the answer?
Add Answer to:
What am I doing wrong on this? Chapter 17, Problem 3E // Michael Cole // 6/28/2017...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What am I doing wrong? I get this error: "The name 'LoginPasswordUserControl' does not exist in...

    What am I doing wrong? I get this error: "The name 'LoginPasswordUserControl' does not exist in the current context" I have a user control, here is the code: using System.Windows.Forms; //Define the namespace to make the user control. //namespace to hold the application namespace LoginPasswordUser { //Define the class to make the user control.   //Class to define the control   public partial class LoginPasswordUserControl : UserControl { //Define the read-only properties to get the username and the password from the user...

  • Hi everyone, For my Assignment I have to use C# in order to create a validation...

    Hi everyone, For my Assignment I have to use C# in order to create a validation form and include the following things to register and validate: Email Address, Username, Password, Retype Password, and a Submit Button. I have figured out some of the code for the Email Address validation: 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; namespace Validation4 {     public partial class Form1 : Form     {        ...

  • Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for...

    Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for writing and reading from console #include <fstream> //this one to deal with files, read and write to text files using namespace std; int main() { //first thing we need to read the data in the text file //let's assume we have the reading in a text file called data.txt //so, we need a stream input variable to hold this data ifstream infile; //now we...

  • C# Windows Form Application (CALCULATOR): (This is the instruction below): Windows Calculator that will going to...

    C# Windows Form Application (CALCULATOR): (This is the instruction below): Windows Calculator that will going to create a Windows Form Application that will mimics a calculator. This is the screenshot how it shouldl look like. Calculator It should have a textbox and the following buttons: + Addition - Subtraction * Multiplication / Division = Equals (Will perform the final calculation) C Clear (Will clear the text box) There will be no maximize or minimize buttons. The rules are these: 2...

  • Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing an...

    Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

  • Part 2: Programming with Exceptions In this part of the lab , you will write a...

    Part 2: Programming with Exceptions In this part of the lab , you will write a program that fetches the information stored at a give URL on the web and saves that data to a file. This will also include networking and file operations and partly an exercise in using exceptions. For doing I/O, Java has a pair of nice abstractions: InputStream and OutputStream. These are abstract classes in the package java.io. An InputStream is a place from which you...

  • Using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; u...

    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 FileWriter { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnCreate_Click(object sender, EventArgs e) { try { //write code that assigns the value in the textbox to an Integer variable if // write code that validates whether or not the data entered in the textbox is greater than or equal to 1 *...

  • Source material: Object-Oriented Data Structures Using Java, 3rd Edition by Nell Dale. What is the answer...

    Source material: Object-Oriented Data Structures Using Java, 3rd Edition by Nell Dale. What is the answer to exercise 13 in chapter 3 (page 232)?? "A. Create a "standard" exception class called ThirteenException. B. Write a program that repeatedly prompts the user to enter a string. After each string is entered the program outputs the length of the string, unless the length of the string is 13, in which case the ThirteenException is thrown with the message "Use thirteen letter words...

  • (Java) Rewrite the following exercise below to read inputs from a file and write the output...

    (Java) Rewrite the following exercise below to read inputs from a file and write the output of your program in a text file. Ask the user to enter the input filename. Use try-catch when reading the file. Ask the user to enter a text file name to write the output in it. You may use the try-with-resources syntax. An example to get an idea but you need to have your own design: try ( // Create input files Scanner input...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT