EDN Admin
Well-known member
Dear C# Programmers,
I want to create a project which writes record into file, but the program throws a FormatException when I clickEnter button... Catch hadles SerializationException and shows my specified message. Now I wanna know what happen during serialization???
save as button normally works as well as Exit button. So first I create file with (.dat) extension and then I want to write records into file, but shows "Error Writing to File" Message
in any case, my OS is win 7 and using VS 2010
Pls. help me
The Following is my cod
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
using BankUIForm;
namespace CreateFileForm3
{
public partial class Form2 : BankUIForm.BankUIForm
{
// serializes Record in binary format
BinaryFormatter formatter = new BinaryFormatter();
// stream through which serializable data is written to file
private FileStream output;
public Form2()
{
InitializeComponent();
}
private void saveButton_Click(object sender, EventArgs e)
{
SaveFileDialog fileChooser = new SaveFileDialog();
DialogResult result = fileChooser.ShowDialog();
string fileName; // name of file to save data
// allow user to create file
fileChooser.CheckFileExists = false;
// exit event handler if user clicked "Cancel"
if (result == DialogResult.Cancel)
return;
// get specified file name
fileName = fileChooser.FileName;
if (fileName == "" || fileName == null)
{
MessageBox.Show("invalid file Name", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
// save file via FileStream if user specified valid file
try
{
output = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
// disable Save button and enable Enter button
saveButton.Enabled = false;
enterButton.Enabled = true;
}
// handle exception if file does not exist
catch (FileNotFoundException)
{
MessageBox.Show("File Does Not Exist", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void enterButton_Click(object sender, EventArgs e)
{
string[] values = GetTextBoxValues();
// Record containing TextBox values to serialize
Record record = new Record();
// determine whether TextBox account field is empty
if (values[(int)textBoxIndices.Account] != "")
{
// store TextBox values in Record and serialize Record
try
{
// get account number value from TextBox
int AccountNumber = Int32.Parse(
values[(int)textBoxIndices.Account]);
// determine whether accountNumber is valid
if (AccountNumber > 0)
{
// store TextBox fields in Record
record.Account = AccountNumber;
record.FirstName = values[(int)textBoxIndices.First];
record.LastName = values[(int)textBoxIndices.Last];
record.Balance = double.Parse(values[(int)textBoxIndices.Balance]);
// write Record to FileStream (serialize object)
formatter.Serialize(output, record);
}
else
{
// Notify user if invalid number
MessageBox.Show("Invalid Account Number", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// notify user if error occurs in serialization
catch (SerializationException)
{
MessageBox.Show("Error Writing to File", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
// notify user if error occurs regarding parameter format
catch (FormatException)
{
MessageBox.Show("Invalid Format", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
ClearTextBox(); // clear TextBox values
}
// invoked when user clicks Exit button
private void exitButton_Click(object sender, EventArgs e)
{
// determine whether file exists
if (output != null)
{
try
{
output.Close();
}
// notify user of error closing file
catch (IOException)
{
MessageBox.Show("Cannot close file", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Application.Exit();
} // end method exitButton_Click
}
}
View the full article
I want to create a project which writes record into file, but the program throws a FormatException when I clickEnter button... Catch hadles SerializationException and shows my specified message. Now I wanna know what happen during serialization???
save as button normally works as well as Exit button. So first I create file with (.dat) extension and then I want to write records into file, but shows "Error Writing to File" Message
in any case, my OS is win 7 and using VS 2010
Pls. help me
The Following is my cod
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
using BankUIForm;
namespace CreateFileForm3
{
public partial class Form2 : BankUIForm.BankUIForm
{
// serializes Record in binary format
BinaryFormatter formatter = new BinaryFormatter();
// stream through which serializable data is written to file
private FileStream output;
public Form2()
{
InitializeComponent();
}
private void saveButton_Click(object sender, EventArgs e)
{
SaveFileDialog fileChooser = new SaveFileDialog();
DialogResult result = fileChooser.ShowDialog();
string fileName; // name of file to save data
// allow user to create file
fileChooser.CheckFileExists = false;
// exit event handler if user clicked "Cancel"
if (result == DialogResult.Cancel)
return;
// get specified file name
fileName = fileChooser.FileName;
if (fileName == "" || fileName == null)
{
MessageBox.Show("invalid file Name", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
// save file via FileStream if user specified valid file
try
{
output = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
// disable Save button and enable Enter button
saveButton.Enabled = false;
enterButton.Enabled = true;
}
// handle exception if file does not exist
catch (FileNotFoundException)
{
MessageBox.Show("File Does Not Exist", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void enterButton_Click(object sender, EventArgs e)
{
string[] values = GetTextBoxValues();
// Record containing TextBox values to serialize
Record record = new Record();
// determine whether TextBox account field is empty
if (values[(int)textBoxIndices.Account] != "")
{
// store TextBox values in Record and serialize Record
try
{
// get account number value from TextBox
int AccountNumber = Int32.Parse(
values[(int)textBoxIndices.Account]);
// determine whether accountNumber is valid
if (AccountNumber > 0)
{
// store TextBox fields in Record
record.Account = AccountNumber;
record.FirstName = values[(int)textBoxIndices.First];
record.LastName = values[(int)textBoxIndices.Last];
record.Balance = double.Parse(values[(int)textBoxIndices.Balance]);
// write Record to FileStream (serialize object)
formatter.Serialize(output, record);
}
else
{
// Notify user if invalid number
MessageBox.Show("Invalid Account Number", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// notify user if error occurs in serialization
catch (SerializationException)
{
MessageBox.Show("Error Writing to File", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
// notify user if error occurs regarding parameter format
catch (FormatException)
{
MessageBox.Show("Invalid Format", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
ClearTextBox(); // clear TextBox values
}
// invoked when user clicks Exit button
private void exitButton_Click(object sender, EventArgs e)
{
// determine whether file exists
if (output != null)
{
try
{
output.Close();
}
// notify user of error closing file
catch (IOException)
{
MessageBox.Show("Cannot close file", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Application.Exit();
} // end method exitButton_Click
}
}
View the full article