This document contains code for saving user-entered data to a file and appending new records to an existing file. It uses file dialogs to select and set file paths, writes data using StreamWriter, and handles errors when reading or writing files.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
14 views2 pages
Saveandenter
This document contains code for saving user-entered data to a file and appending new records to an existing file. It uses file dialogs to select and set file paths, writes data using StreamWriter, and handles errors when reading or writing files.
{ // create and show dialog box enabling user to save file DialogResult result; // result of SaveFileDialog string fileName; // name of file containing data
using (var fileChooser = new SaveFileDialog())
{ fileChooser.CheckFileExists = false; // let user create file result = fileChooser.ShowDialog(); fileName = fileChooser.FileName; // name of file to save data this.filenamelb.Text = fileName; }
// ensure that user clicked "OK"
if (result == DialogResult.OK) { // show error if user specified invalid file if (string.IsNullOrEmpty(fileName)) { MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { // save file via FileStream if user specified valid file try { using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) using (var fileWriter = new StreamWriter(fileStream)) { // File operations can be performed here // ...
// disable Save button and enable Enter button
this.savebtn.Enabled = false; this.enterbtn.Enabled = true; } } catch (IOException ex) { // notify user if file cannot be opened MessageBox.Show($"Error opening file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }
// determine whether TextBox account field is empty
if (!string.IsNullOrEmpty(values[(int)TextBoxIndices.Account])) { try { // get account-number value from TextBox int accountNumber = int.Parse(values[(int)TextBoxIndices.Account]);
// determine whether accountNumber is valid
if (accountNumber > 0) { // Record containing TextBox values to output var record = new Record(accountNumber, values[(int)TextBoxIndices.First], values[(int)TextBoxIndices.Last], decimal.Parse(values[(int)TextBoxIndices.Balance]));
// Write data to the file
string filePath = this.filenamelb.Text; using (var output = new FileStream(filePath, FileMode.Append, FileAccess.Write)) using (var fileWriter = new StreamWriter(output)) { fileWriter.WriteLine($"{record.Account}, {record.FirstName},{record.LastName},{record.Balance}"); } } else { // notify user if invalid account number MessageBox.Show("Invalid Account Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (IOException ex) { // Log the exception details or display an error message MessageBox.Show($"Error Writing to File: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (FormatException ex) { // Log the exception details or display an error message MessageBox.Show($"Invalid Format: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { // Log the exception details or display a generic error message MessageBox.Show($"An error occurred: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }