Here is fixed link that you can download the zip file.: https://drive.google.com
ID: 3878919 • Letter: H
Question
Here is fixed link that you can download the zip file.:
https://drive.google.com/file/d/17g41MxQFwOZc0ONr1WDov0Rk3BMuegFL/view?usp=sharing
I'm also adding the code. I need help because of all the errors I getting.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _17._4SerializingandDeserializing
{
public partial class Serializing : Form
{
BinaryFormatter gtDeSerial = new BinaryFormatter();
FileStream getRec;
int incClose = 0;
int incOpen = 0;
int numberofrec = 0;
public Serializing()
{
InitializeComponent();
}
protected int TextBoxCount = 5;
public enum TxtIndices
{
TXTFNAME,
TXTLNAME,
TXTID,
TXTCLASS,
TXTGRADE
}
public string[] GetTextBoxValues()
{
string[] getVals = new string[TextBoxCount];
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 getVals;
}
public void CleanTxt()
{
foreach(Control myCtrl in Controls)
{
if (myCtrl is TextBox)
{
TextBox txtBox = (TextBox)myCtrl;
txtBox.Clear();
}
}
}
private void label3_Click(object sender, EventArgs e)
{
}
private void btnOpen_Click(object sender, EventArgs e)
{
if (incClose > 0)
{
if (incOpen > 0)
{
btnOpen.Enabled = false;
try
{
for (int l = 0; l < numberofrec; l++)
{
SerializeFile record = (SerializeFile)gtDeSerial.Deserialize(setRec);
string[] values = new string[]
{
record.fn.ToString(),
record.ln.ToString(),
record.ID.ToString(),
record.cn.ToStaring(),
record.grade.ToString()
};
for (int k = 0; k < TextBoxCount; k++)
{
textData.Text += values[k].ToString();
if (k == 0)
textData.Text += ",";
else if (k == 1) textData.Text += ",";
else TextData.Text += "";
}
textData.Text += " ";
}
btnAdd.Enabled = true;
btnClose.Enabled = true;
btnOpen.Enabled = false;
}
catch (SerializationException)
{
setRec.Close();
btnAdd.Enabled = true;
btnClose.Enabled = true;
btnOpen.Enabled = false;
CleanTxt();
MessageBox.Show("No more records", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
DialogResult getRes;
string myFile;
using (OpenFileDialog findFile = new OpenFileDialog())
{
getRec = findFile.ShowDialog();
myFile = findFile.FileName;
{
if(getRec==DialogResult.OK)
{
CleanTxt();
textData.Text = "";
if (myFile == string.Empty) MessageBox.Show("Invalid Faile Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
setRec = new FileStream(myFile, FileMode.Open, FileAccess.Read);
btnOpen.Text = "View Records";
btnAdd.Enabled = false;
btnCrate.Enabled = false;
btnClose.Enabled = false;
}
}
incOpen++;
}
}
else
MessageBox.Show("First click on close file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void btnCrate_Click(object sender, EventArgs e)
{
DialogResult result;
string myFile;
using (SaveFileDialog gtFile = new SaveFileDialog())
{
gtFile.CheckFileExists = false;
result = gtFile.ShowDialog();
myFile = gtFile.FileName;
}
if (result==DialogResult.OK)
{
if (myFile == string.Empty) MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
try
{
if (incClose == 0)
{
getRec = new FileStream(myFile, FileMode.Create, FileAccess.Write); incClose++;
}
else
getRec = new FileStream(myFile, FileMode.OpenOrCreate, FileAccess.Write);
btnCrateFile.Enable = false;
btnAdd.Enabled = true;
}
catch(IOException)
{
MessageBox.Show("Error opening file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
string[] myData = GetTextBoxValues();
SerializeFile sRec = new SerializeFile();
if (myData[(int)TxtIndices.TXTID]!=string.Empty)
{
try
{
int IDNumber = Int32.Parse(myData[(int)TxtIndices.TXTID]);
if(IDNumber > 0)
{
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];
mkSerial.Serialize(getRec, sRec);
numberofrec++;
MessageBox.Show("File Created and record Added");
}
else
{
MessageBox.Show("Invalid ID Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (SerializationException)
{
MessageBox.Show("Error Writing to File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
CleanTxt();
}
private void btnClose_Click(object sender, EventArgs e)
{
textData.Text = "";
btnAdd.Enabled = false;
btnOpen.Enabled = true;
btnCrate.Enabled = false;
if(getRec!= null)
{
try
{
getRec.Close();
incClose++;
}
catch(IOException)
{
MessageBox.Show("Cannot close file", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
MessageBox.Show("The file can be viewed now", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnClose.Enabled = false;
incClose++;
}
private void btnAdd_Click(object sender, EventArgs e)
{
string[] values = GetTextBoxValues();
SerializeFile record = new SerializeFile();
if(values[(int)TxtIndices.TXTID]!=string.Empty)
{
try
{
int IDNumber = Int32.Parse(values[(int)TxtIndices.TXTID]);
if (IDNumber > 0)
{
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];
mkSerial.Serialize(getRec, record);
numberofrec++;
MessageBox.Show("Record Added");
}
else
{
MessageBox.Show("invalid ID Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch(SerializationException)
{
MessageBox.Show("Error writing to file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch(FormatException)
{
MessageBox.Show("Invaild format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
CleanTxt();
}
}
}
All
I'm trying to do my assignment of Serializing and Deserializing but my coding went south quick. Would you please look at my code and let me know what i'm doing wong. I have the file zipped on the link below. Please help.
GOD Bless Craig
https://drive.google.com/open?id=17g41MxQFwOZc0ONr1WDov0Rk3BMuegFL
Explanation / Answer
Ans: I have updated the code and here is the drive link for it:
https://drive.google.com/file/d/1gCCuqGMy0bUznNhxImgPI1DJR7YaKT_P/view?usp=sharing
SerializeFile is not the perdefined type in C# so you have to create a class with this name and write the following code in that file.
SerializeFile.cs file code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
[Serializable()]
class SerializeFile
{
public int ID { get; set; }
public string ln{get;set;}
public string fn { get; set; }
public string cn{get;set;}
public string grade{get;set;}
}
}
This is the Form1 code behind code, when you are testing it your system at that time you have to change the namespace according to your project name.I have tested it properly it is working fine.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using WindowsFormsApplication1;
namespace _17._4SerializingandDeserializing
{
public partial class Serializing : Form
{
BinaryFormatter gtDeSerial = new BinaryFormatter();
FileStream getRec;
int incClose = 0;
int incOpen = 0;
int numberofrec = 0;
public Serializing()
{
InitializeComponent();
}
protected int TextBoxCount = 5;
public enum TxtIndices
{
TXTFNAME,
TXTLNAME,
TXTID,
TXTCLASS,
TXTGRADE
}
public string[] GetTextBoxValues()
{
string[] getVals = new string[TextBoxCount];
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 getVals;
}
public void CleanTxt()
{
foreach (Control myCtrl in Controls)
{
if (myCtrl is TextBox)
{
TextBox txtBox = (TextBox)myCtrl;
txtBox.Clear();
}
}
}
private void label3_Click(object sender, EventArgs e)
{
}
private void btnOpen_Click(object sender, EventArgs e)
{
if (incClose > 0)
{
if (incOpen > 0)
{
btnOpen.Enabled = false;
try
{
for (int l = 0; l < numberofrec; l++)
{
SerializeFile record = (SerializeFile)gtDeSerial.Deserialize(getRec);
string[] values = new string[]
{
record.fn.ToString(),
record.ln.ToString(),
record.ID.ToString(),
record.cn.ToString(),
record.grade.ToString()
};
for (int k = 0; k < TextBoxCount; k++)
{
textData.Text += values[k].ToString();
if (k == 0)
textData.Text += ",";
else if (k == 1) textData.Text += ",";
else textData.Text += "";
}
textData.Text += " ";
}
btnAdd.Enabled = true;
btnClose.Enabled = true;
btnOpen.Enabled = false;
}
catch (SerializationException)
{
getRec.Close();
btnAdd.Enabled = true;
btnClose.Enabled = true;
btnOpen.Enabled = false;
CleanTxt();
MessageBox.Show("No more records", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
DialogResult getRes;
string myFile;
using (OpenFileDialog findFile = new OpenFileDialog())
{
getRes = findFile.ShowDialog();
myFile = findFile.FileName;
{
if(getRes==DialogResult.OK)
{
CleanTxt();
textData.Text = "";
if (myFile == string.Empty) MessageBox.Show("Invalid Faile Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
getRec = new FileStream(myFile, FileMode.Open, FileAccess.Read);
btnOpen.Text = "View Records";
btnAdd.Enabled = false;
btnCrate.Enabled = false;
btnClose.Enabled = false;
}
}
incOpen++;
}
}
//MessageBox.Show("First click on close file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void btnCrate_Click(object sender, EventArgs e)
{
DialogResult result;
string myFile;
using (SaveFileDialog gtFile = new SaveFileDialog())
{
gtFile.CheckFileExists = false;
result = gtFile.ShowDialog();
myFile = gtFile.FileName;
}
if (result == DialogResult.OK)
{
if (myFile == string.Empty) MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
try
{
if (incClose == 0)
{
getRec = new FileStream(myFile, FileMode.Create, FileAccess.Write); incClose++;
}
else
getRec = new FileStream(myFile, FileMode.OpenOrCreate, FileAccess.Write);
btnCrate.Enabled = false;
btnAdd.Enabled = true;
}
catch (IOException)
{
MessageBox.Show("Error opening file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
string[] myData = GetTextBoxValues();
SerializeFile sRec = new SerializeFile();
if (myData[(int)TxtIndices.TXTID] != string.Empty)
{
try
{
int IDNumber = Int32.Parse(myData[(int)TxtIndices.TXTID]);
if (IDNumber > 0)
{
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];
gtDeSerial.Serialize(getRec, sRec);
numberofrec++;
MessageBox.Show("File Created and record Added");
}
else
{
MessageBox.Show("Invalid ID Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (SerializationException)
{
MessageBox.Show("Error Writing to File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
CleanTxt();
}
private void btnClose_Click(object sender, EventArgs e)
{
textData.Text = "";
btnAdd.Enabled = false;
btnOpen.Enabled = true;
btnCrate.Enabled = false;
if (getRec != null)
{
try
{
getRec.Close();
incClose++;
}
catch (IOException)
{
MessageBox.Show("Cannot close file", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
MessageBox.Show("The file can be viewed now", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnClose.Enabled = false;
incClose++;
}
private void btnAdd_Click(object sender, EventArgs e)
{
string[] values = GetTextBoxValues();
SerializeFile record = new SerializeFile();
if (values[(int)TxtIndices.TXTID] != string.Empty)
{
try
{
int IDNumber = Int32.Parse(values[(int)TxtIndices.TXTID]);
if (IDNumber > 0)
{
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];
gtDeSerial.Serialize(getRec, record);
numberofrec++;
MessageBox.Show("Record Added");
}
else
{
MessageBox.Show("invalid ID Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (SerializationException)
{
MessageBox.Show("Error writing to file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (FormatException)
{
MessageBox.Show("Invaild format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
CleanTxt();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Hope it help you