private void btnUpdate_Click(object sender, EventArgs e)
 {
     if (dataGood())
     {
         int    acct = Convert.ToInt32(txtAccountNum.Text);
         string fn   = txtFirstName.Text;
         string ln   = txtLastName.Text;
         string sbal = txtAccountBalance.Text;
         if (sbal[0] == '$')
         {
             sbal = sbal.Remove(0, 1);
         }
         else if (sbal[0] == '-' && sbal[1] == '$')
         {
             sbal = sbal.Remove(1, 1);
         }
         double          bal = Convert.ToDouble(sbal);
         AccountRecordRA ra  = new AccountRecordRA(acct, fn, ln, bal);
         try
         {
             file.Seek(ra.FileIndex, SeekOrigin.Begin);
             ra.write(file);
             readFile();
         }
         catch (IOException io)
         {
             MessageBox.Show(io.Message, "Error updating record");
         }
         setControlState("i");
     }
 }
 private void btnInsert_Click(object sender, EventArgs e)
 {
     if (btnInsert.Text == "Cancel")
     {
         setControlState("i");
         return;
     }
     if (dataGood())
     {
         int accountNumber = Convert.ToInt32(txtAccountNum.Text);
         if (isValidAccount(accountNumber))
         {
             string          firstName = txtFirstName.Text;
             string          lastName  = txtLastName.Text;
             double          balance   = Convert.ToDouble(txtAccountBalance.Text);
             AccountRecordRA ra        = new AccountRecordRA(accountNumber, firstName, lastName, balance);
             try
             {
                 //position file pointer
                 file.Seek(ra.FileIndex, SeekOrigin.Begin);
                 ra.write(file);
                 readFile();
                 clearText();
             }
             catch (IOException io)
             {
                 MessageBox.Show(io.Message, "Error writing to File");
             }
         }
     }
 }
        private void readFile()
        {
            //listbxClients.Items.Clear();
            myTable.Rows.Clear();
            AccountRecordRA ra = new AccountRecordRA();

            try
            {
                file.Seek(0, SeekOrigin.Begin);
                for (int i = 0; i < MAX_RECORDS; i++)
                {
                    ra.read(file);
                    if (ra.AccountNumber > 0)
                    {
                        //listbxClients.Items.Add(formatRecordToString(ra));
                        myTable.Rows.Add(formatRecordToDataRow(ra));
                    }
                }
                dg1.ClearSelection();
            }
            catch (IOException io)
            {
                MessageBox.Show(io.Message, "Error reading from File");
            }
        }
        private DataRow formatRecordToDataRow(AccountRecordRA ra)
        {
            DataRow row = myTable.NewRow();

            row["AccountNumber"] = ra.AccountNumber;
            row["FirstName"]     = ra.FirstName;
            row["LastName"]      = ra.LastName;
            row["Balance"]       = ra.AccountBalance.ToString("c");
            return(row);
        }
        private string formatRecordToString(AccountRecordRA ra)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(ra.AccountNumber);
            sb.Append("|");
            sb.Append(ra.FirstName);
            sb.Append("|");
            sb.Append(ra.LastName);
            sb.Append("|");
            sb.Append(ra.AccountBalance.ToString("c"));
            return(sb.ToString());
        }
 private void btnDelete_Click(object sender, EventArgs e)
 {
     if (MessageBox.Show("Are you sure you want to delete this record?", "Record Delete",
                         MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                         MessageBoxDefaultButton.Button2) ==
         System.Windows.Forms.DialogResult.Yes)
     {
         AccountRecordRA ra = new AccountRecordRA();
         ra.AccountNumber = Convert.ToInt32(txtAccountNum.Text);
         try
         {
             file.Seek(ra.FileIndex, SeekOrigin.Begin);
             ra.delete(file);
             readFile();
         }
         catch (IOException io)
         {
             MessageBox.Show(io.Message, "Error deleting record");
         }
         setControlState("i");
     }
 }
        private void initData()
        {
            AccountRecordRA ra = new AccountRecordRA();

            try
            {
                //clear the file incase it contains corrupt data
                file.SetLength(0);
                //position file pointer:
                file.Seek(0, SeekOrigin.Begin);
                for (int i = 0; i < MAX_RECORDS; i++)
                {
                    ra.write(file);
                }
                readFile();
                MessageBox.Show("Data file has been initialized");
            }
            catch (IOException io)
            {
                MessageBox.Show(io.Message, "Error initializing File");
            }
        }