Question: Why am I getting this error 6 times: Error 1 The type or namespace name 'SerializeFile' could not be found (are you missing a using
Why am I getting this error 6 times:
Error 1 The type or namespace name 'SerializeFile' could not be found (are you missing a using directive or an assembly reference?)
Please show me what to do to correct this.
This is C# code, any help is great appreciated!
Thanks in advance.
using System;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
//Define the namespace for holding the application.
//namespace for holding the application
namespace test {
//Create the class to define the application.
//class to create the application
public partial class SerialFile : Form {
//Make the object of the BinaryFormatter class so that the data can be stored in the binary format.
//Make another object for getting the De-serialized form of the data. Also create the streams for
//reading data from and writing data into the file.
// object for serializing the data in binary //format
BinaryFormatter mkSerial = new BinaryFormatter();
// stream for writing the data to a file
FileStream getRec;
// object for Deserializing from binary to normal
// format
BinaryFormatter gtDeSerial = new BinaryFormatter(); FileStream setRec;
//Define the variables to keep the track of number of times the file has been saved and read. Also
//define the variables for keeping the track of number of records.
//value of incClose will be //increased on closing of
//output file
int incClose = 0;
//value of incOpen will be increased on opening of
//reader file
int incOpen = 0;
//variable which records the entries
int numberofrec = 0;
//Define the constructor for initializing the GUI of the form.
//Constructor of the class to create //the GUI of the
//form
public SerialFile()
{
InitializeComponent();
}
//number of text boxes in the form protected
int TextBoxCount = 5;
//Make an enumeration for keeping the track of the TextBoxes.
// enumeration for fetching TextBox indices
public enum TxtIndices { TXTFNAME, TXTLNAME, TXTID, TXTCLASS, TXTGRADE }
//Define the method to get the string collection of the entries in the TextBoxes. This method
//returns the array holding the records entered in the TextBoxes.
/* Method to return TextBox values as string bunch * @return the array with details */
public string[] GetTextBoxValues() {
//Create an array to accumulate the string values.
string[] getVals = new string[TextBoxCount];
//Get the values from the TextBoxes and accumulate them in the array one by one.
// copy TextBox fields in array
getVals[(int)TxtIndices.TXTFNAME] = textFirst.Text;
getVals[(int)TxtIndices.TXTLNAME] = textLast.Text; getVals[(int)TxtIndices.TXTID] = textID.Text;
getVals[(int)TxtIndices.TXTCLASS] = textClass.Text; getVals[(int)TxtIndices.TXTGRADE] = textGrade.Text;
//Return the collection of values.
return getVals; }
//Define the method to clear the data of the TextBoxes after the record has been processed.
/* //Method to clean the TextBoxes */
public void CleanTxt() {
//Run a loop to access the controls on the form one by one and check if that is a TextBox.
//If that //is, clean the TextBox.
// walk through each control in the form
foreach (Control myCtrl in Controls) {
// check if the Control is TextBox
if (myCtrl is TextBox) {
//clean the TextBox
TextBox txtBox = (TextBox)myCtrl; txtBox.Clear(); } } }
//Define the method to handle the Click of the button to open the file.
/* Method to handle the Click of the button to open * file */
private void cmdOpen_Click(object sender, EventArgs e)
{
//Check if the file is open or not.
//if output file has been closed
if (incClose > 0)
{
//Check if the file is open and up for processing. //if record file has been opened and next
//records are to be appended
if (incOpen > 0)
{
//Disable the button to open the file.
cmdOpen.Enabled = false; try
{
//Run a loop to access all of the records accumulated in the file. //for viewing all records file
//contains
for (int l = 0; l < numberofrec; l++)
{
//Get the de-serialized form of the record.
//serialize the record
SerializeFile record =
(SerializeFile)
gtDeSerial.Deserialize(setRec);
//Store the de-serialized values in the string array. //store the values in the array
string[] values = new string[] { record.fn.ToString(), record.ln.ToString(), record.ID.ToString(),
record.cn.ToString(), record.grade.ToString() };
//Get the data on the form and show that in the TextBox that is there for showing the records.
//After each record is appended to the TextBox, put in a space.
//for viewing records of all //text boxes into textData
for (int k = 0; k < TextBoxCount; k++)
{
//Presentation of records
textData.Text += values[k].ToString(); if (k == 0) textData.Text += ", ";
else if (k == 1) textData.Text += ": "; else textData.Text += " ";
}
//Line Change textData.Text += " "; }
// Set the accessibility of the buttons appropriately.
//Set Buttons
cmdAdd.Enabled = true;
cmdClose.Enabled = true;
cmdOpen.Enabled = false;
}
}
//Catch if any exception in serialization occurs during the above process.
catch (SerializationException)
{
//Enable the control subtly and close the serialized file stream. Clear the text from the TextBoxes
//and show the error message to the user.
setRec.Close(); cmdAdd.Enabled = true;
cmdClose.Enabled = true;
cmdOpen.Enabled = false;
CleanTxt(); MessageBox.Show("No more records", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
//Show a dialog box to the user. The user will select a file to be opened in the application. Make
//appropriate objects for this purpose. // object to facilitate the user with // opening the file
DialogResult getRes;
// name of file containing data
string myFile; //Get the selection of the user stored in a variable.
using (OpenFileDialog findFile = new OpenFileDialog())
{
getRes = findFile.ShowDialog();
// get specified name
myFile = findFile.FileName;
}
//Check if the user has selected to open the file. //if user selects OK
if (getRes == DialogResult.OK)
{
//Clear the text of the TextBoxes.
CleanTxt(); textData.Text = "";
//Show an error message if the file name was not a valid one.
// show error message if user has
// chosen invalid file
if (myFile == string.Empty) MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
//If everything was fine, open the file in the read mode. Set the accessibility of the controls appropriately.
//open file in read mode setRec = new FileStream( myFile, FileMode.Open, FileAccess.Read); cmdOpen.Text = "View Records";
// disable all buttons
cmdAdd.Enabled = false;
cmdCreateFile.Enabled = false;
cmdClose.Enabled = false;
}
}
//Increase the number of processed files by 1.
incOpen++;
}
}
//Show an error message, if the procedure was not followed step by step.
else MessageBox.Show("First click on close file ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); { }
}
//Define the method to handle the Click of the control to create the file of records.
/* Method to handle the Click of the button to make * the file */
private void cmdCreateFile_Click(object sender, EventArgs e) {
//Make an object to facilitate user with searching a location for the file and a variable for accumulating the file name.
DialogResult result;
// name of file
string myFile;
//Use the dialog box to open the file.
using (SaveFileDialog gtFile = new SaveFileDialog()) {
//Create the file and get the name of the file stored in a variable.
// make the user create file
gtFile.CheckFileExists = false;
// fetch the result
result = gtFile.ShowDialog();
// fetch name of the file
myFile = gtFile.FileName; }
//Make sure that user want to save the file.
// confirm that user chose OK
if (result == DialogResult.OK) { //Make sure that the user enters a valid name for the file.
// show error message if user named invalid // file
if (myFile == string.Empty) MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); else {
//If everything is fine, open the file in the write mode. If the file does not exist already, create it. try {
// open file in write mode
if (incClose == 0) { getRec = new FileStream(myFile, FileMode.Create, FileAccess.Write); incClose++; }
else getRec = new FileStream(myFile, FileMode.OpenOrCreate, FileAccess.Write);
//Set the accessibilities of the button controls subtly.
// disable Save button
cmdCreateFile.Enabled = false;
//enable Enter button
cmdAdd.Enabled = true;
}
} try { }
// catch exception if file cannot be
// opened subtly
catch (IOException)
{
//Show an error message if an error happened to occur during the above process.
// show error //message
MessageBox.Show("Error opening file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} { }
{
//Invoke the method and get the values of the TextBoxes in an array.
string[] myData = GetTextBoxValues();
// create an object to serialize the data
SerializeFile sRec = new SerializeFile();
//Check if all of the Textboxes have the valid data.
//form the desired string of the text boxes' //text
if (myData[(int)TxtIndices.TXTID] != string.Empty) { try {
//Get the ID number from the TextBox.
// fetch ID
int IDNumber = Int32.Parse(myData[(int)TxtIndices.TXTID]);
//Check if the ID is a valid value.
// check for valid account number
if (IDNumber > 0) {
// prepare data for serialization
sRec.ID = IDNumber; sRec.fn = myData[(int) TxtIndices.TXTFNAME]; sRec.ln = myData[(int)
TxtIndices.TXTLNAME]; sRec.cn = myData[(int) TxtIndices.TXTCLASS];
sRec.grade = myData[(int) TxtIndices.TXTGRADE];
//Serialize the data gotten and accumulate it in the file. show the success message when this is
//done. //write serialized data to file
mkSerial.Serialize(getRec, sRec); numberofrec++; MessageBox.Show("File Created and Record Added"); } else {
//If the data could not be saved or any other problem happens to come, show an error message to the user.
//show the error message if the ID //is invalid
MessageBox.Show("Invalid ID Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
// show the error message if serialization
// fails
catch (SerializationException) {
//If an exception occurs during serialization, catch it and show the error message to the user.
MessageBox.Show("Error Writing to File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
//Handle the format exception if that occurs due to the bad format of the data to be inputted.
// show error message if the format is not // right
catch (FormatException) { MessageBox.Show("Invalid Format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
//Invoke the method to clean the text of the TextBoxes.
CleanTxt(); } }
//Define the method to handle the Click event of the button to stop writing the data to the file.
/* //Method to define the Click of the button to
* * close the file */
private void cmdClose_Click(object sender, EventArgs e) {
//Clean the TextBox that would show the data from the file.
//update the controls status
textData.Text = "";
//Set the accessibility of the buttons appropriately.
cmdAdd.Enabled = false;
cmdOpen.Enabled = true;
cmdCreateFile.Enabled = false;
//Check if there is a record to show.
if (getRec != null) { try {
//Close the previous file that was open for writing. ///close the file
getRec.Close(); incClose++; } catch (IOException) {
//Get an exception handles if that occurs during the process shown above.
//show error message if the file could
//not be closed
MessageBox.Show("cannot close file", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
//Show a success message of everything goes fine.
MessageBox.Show("The file can be viewed Now", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Information); cmdClose.Enabled = false; incClose++; }
//Define the method to handle the Click of the Button to add the record.
private void cmdAdd_Click(object sender, EventArgs e) {
//Get the values of the TextBoxes stored in an array of strings.
string[] values = GetTextBoxValues();
//Make an object of the class to serialize the data.
// object to serialize the data
SerializeFile record = new SerializeFile();
//form the desired string of the text boxes'
//text Check if the textboxes have valid values.
if (values[(int)TxtIndices.TXTID] != string.Empty) { try {
//Get the ID number form the TextBox. // fetch ID number
int IDNumber = Int32.Parse( values[(int)TxtIndices.TXTID]);
//Check if the value of ID is a valid number.
// check if the ID is valid
if (IDNumber > 0) {
//Get the values of the TextBoxes stored in the object that will serialize the values.
// store TextBox fields in // RecordSerializable
record.ID = IDNumber; record.fn = values[(int) TxtIndices.TXTFNAME];
record.ln = values[(int) TxtIndices.TXTLNAME]; record.cn = values[(int) TxtIndices.TXTCLASS];
record.grade = values[(int) TxtIndices.TXTGRADE];
//Write the data to the mentioned file. // write data to file
mkSerial.Serialize(getRec, record); numberofrec++; MessageBox.Show("Record Added"); } else {
//If an error occurs in this, show the error message to the user. // show error message if the
// account number was invalid
MessageBox.Show("Invalid ID Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
catch (SerializationException) {
//Check if an error occurs during the serialization of the record.
//show the error message if the //serialization could not be done
MessageBox.Show("Error Writing to File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
catch (FormatException) {
//Catch an exception if that occurs due to the bad format of the input. Show the error message.
//show error message if the format of //the input was improper
MessageBox.Show("Invalid Format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
//Invoke the method to clean the TextBoxes.
CleanTxt(); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
