private void addButton_Click(object sender, EventArgs e) { if (ValidInput()) { string[] scheduleValues = new string[7]; for (int i = 0; i < 7; i++) { scheduleValues[i] = scheduleComboBoxes[i].Text; } int id = random.Next(int.MaxValue); bool exists = true; while (exists) { exists = DatabaseWorker.IdExists(id); if (!exists) { DatabaseWorker.tempId = id; DatabaseWorker.AddEmployee(id, nameTextBox.Text, jobTextBox.Text, addressTextBox.Text, phoneTextBox.Text, scheduleValues); } id = random.Next(int.MaxValue); } DialogResult = DialogResult.OK; Close(); } else { PrintErrorMessage(); } }
/// <summary> /// Compact the database. /// </summary> private void CompactDatabaseButton_Click(object sender, EventArgs e) { if (GetConfirmation("This process will restructure the database and could take some time \n" + " if the database is large.\n" + " Proceed?")) { try { // Do not allow the form to be closed. allowClose = false; // Show progress bar. progressBar.Visible = true; DatabaseWorker.CompactDatabase(); // Hide progress bar. progressBar.Visible = false; // Allow the form to be closed. allowClose = true; MessageBoxAdv.Show(this, "Successfully compacted the database.", "Success"); } catch (Exception) { MessageBoxAdv.Show(this, "Failed to compact the database.", "Failed"); // Hide progress bar. progressBar.Visible = false; // Allow the form to be closed. allowClose = true; } } }
private void addButton_Click(object sender, System.EventArgs e) { DatabaseWorker.AddEmployee(nameTextBox.Text, jobTextBox.Text, addressTextBox.Text, phoneTextBox.Text); DialogResult = System.Windows.Forms.DialogResult.OK; Close(); }
private void addButton_Click(object sender, EventArgs e) { if (ValidInput()) { string[] attendanceValues = new string[7]; for (int i = 0; i < 7; i++) { attendanceValues[i] = attendanceComboBoxes[i].Text; } int id = random.Next(Int32.MaxValue); bool exists = true; while (exists) { exists = DatabaseWorker.IdExists(id); if (!exists) { DatabaseWorker.tempId = id; DatabaseWorker.AddCustomer(id, nameTextBox.Text, membershipComboBox.SelectedItem.ToString(), phoneTextBox.Text, attendanceValues); } id = random.Next(Int32.MaxValue); } DialogResult = DialogResult.OK; Close(); } else { PrintErrorMessage(); } }
private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < 40000; i++) { DatabaseWorker.AddEmployee(RandomString(5), RandomString(5), RandomString(5), RandomString(5), true); } fec_Main.xmlDoc.Save("fec.xmldb"); UpdateEmployeeGrid(); }
private void removeEmployeeButton_Click(object sender, EventArgs e) { GridRangeInfoList range = employeeGrid.TableModel.Selections.GetSelectedRows(true, true); foreach (GridRangeInfo info in range) { Element element = employeeGrid.TableModel.GetDisplayElementAt(info.Top); DatabaseWorker.RemoveEmployee(element.GetRecord()["Name"].ToString()); } UpdateEmployeeGrid(); }
/// <summary> /// Reset the values of the selected database table. /// </summary> private void ResetButton_Click(object sender, System.EventArgs e) { if (tableComboBox.SelectedIndex != 0) { // Do not allow the form to be closed. allowClose = false; string confirmMessage = ""; // Set a confirmation message according to the selected table. switch (tableComboBox.Text) { case "Employee Schedule": confirmMessage = "This will reset the values of all employee schedules to \"OFF\".\nProceed?"; break; case "Customer Attendance": confirmMessage = "This will reset the values of all customer attendances to \"N/A\".\nProceed?"; break; } try { if (GetConfirmation(confirmMessage)) { switch (tableComboBox.Text) { case "Employee Schedule": DatabaseWorker.ResetEmployeeSchedule(); break; case "Customer Attendance": DatabaseWorker.ResetCustomerAttendance(); break; } // Refresh the reset table. ReloadTable(); MessageBoxAdv.Show(this, "Table was successfully reset.", "Success."); } } catch (Exception) { MessageBoxAdv.Show(this, "Table reset failed.", "Failed."); } } else { MessageBoxAdv.Show(this, "Please select a table.", "Error."); allowClose = false; } }
private void employeeGrid_RecordValueChanged(object sender, RecordValueChangedEventArgs e) { DataRowView drv = e.Record.GetData() as DataRowView; XmlNode node = DatabaseWorker.FindEmployeeNode(temp); node["Name"].InnerText = drv[0].ToString(); node["Job"].InnerText = drv[1].ToString(); node["Address"].InnerText = drv[2].ToString(); node["Phone"].InnerText = drv[3].ToString(); xmlDoc.Save("fec.xmldb"); UpdateEmployeeGrid(); }
/// <summary> /// The main form constructor. Called when the application is opened. /// Creates a new database if one does not already exist, and setups the /// database grid and other UI components. /// </summary> public fec_Main(SplashScreenForm splashForm) { InitializeComponent(); // Set the form icon. this.Icon = Properties.Resources.fecIcon; // Set the minimum size of the form. this.MinimumSize = new Size(820, 561); oldSize = this.MinimumSize; // Set global message box style. MessageBoxAdv.MessageBoxStyle = MessageBoxAdv.Style.Metro; // Open a stream to the database file to prevent it from being deleted. DB_LOCK = File.Open(DatabaseProperties.DATABASE_NAME, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); // Verify that the database is not corrupted. try { if (DB_LOCK.Length == 0) { throw new Exception(); } DatabaseWorker.CheckIntegrity(); } catch (Exception) { // Display error message and force-exit application when message is dismissed. if (MessageBoxAdv.Show(this, "Unable to open database file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK) { DB_LOCK.Close(); System.Diagnostics.Process.GetCurrentProcess().Kill(); } } // Setup and format the grid. SetupDatabaseGrid(); // Setup some UI components and bind some events. SetupComponents(); splashForm.Close(); }
/// <summary> /// Add a new customer in the database. /// </summary> private void AddButton_Click(object sender, EventArgs e) { if (ValidInput()) { string[] attendanceValues = new string[7]; // Get combo box attendance values. for (int i = 0; i < 7; i++) { attendanceValues[i] = attendanceComboBoxes[i].Text; } string name = BuildName(); // Generate a random id. int id = random.Next(Int32.MaxValue); // Used to check whether the id alredy exists in the database. bool exists = true; // Loop until a non-existent id is generated. while (exists) { exists = DatabaseWorker.IdExists(id); if (!exists) { DatabaseWorker.tempId = id; DatabaseWorker.AddCustomer(id, name, membershipComboBox.SelectedItem.ToString(), phoneTextBox.Text, attendanceValues); } // Generate a new id. id = random.Next(Int32.MaxValue); } DialogResult = DialogResult.OK; Close(); } else { PrintErrorMessage(); } }
/// <summary> /// Add a new employee in the database. /// </summary> private void AddButton_Click(object sender, EventArgs e) { if (ValidInput()) { string[] scheduleValues = new string[7]; // Get combo box schedule values. for (int i = 0; i < 7; i++) { scheduleValues[i] = scheduleComboBoxes[i].Text; } string name = BuildName(); string address = BuildAddress(); // Generate a random id. int id = random.Next(int.MaxValue); // Used to check whether the id alredy exists in the database. bool exists = true; // Loop until a non-existent id is generated. while (exists) { exists = DatabaseWorker.IdExists(id); if (!exists) { DatabaseWorker.tempId = id; DatabaseWorker.AddEmployee(id, name, jobTextBox.Text.Trim(' '), address, phoneTextBox.Text, scheduleValues); } id = random.Next(int.MaxValue); } DialogResult = DialogResult.OK; Close(); } else { PrintErrorMessage(); } }
/// <summary> /// When the "Add Employee" button is clicked, prompt the user to enter the information of the new /// employee, and then add the values to the database. Update the grid after the employee was added. /// </summary> private void addEmployeeButton_Click(object sender, EventArgs e) { // Create and display an "Add Employee" form for the user to enter employee information. // This form will also insert the new entries to the database. AddEmployeeForm prompt = new AddEmployeeForm(); prompt.ShowDialog(); // If the form was fully filled in and the entry was successfully inserted in the database, // create new rows and update the grid. if (prompt.DialogResult == DialogResult.OK) { // Hold the employee's basic information and schedule values that are retrieved from the database, // from the newly added employee entries. string[] employeeData = DatabaseWorker.GetEmployeeEntry(DatabaseWorker.tempId); string[] scheduleData = DatabaseWorker.GetEmployeeScheduleEntry(DatabaseWorker.tempId); // Create new rows that will hold the newly added employee's basic information // and schedule values. DataRow employeeEntry = database.Tables["Employees"].NewRow(); DataRow employeeScheduleEntry = database.Tables["Employee Schedule"].NewRow(); // Populate each column of the new Employee and Employe Schedule rows with data from the database. for (int i = 0; i < database.Tables["Employees"].Columns.Count; i++) { employeeEntry[i] = employeeData[i]; } for (int i = 0; i < database.Tables["Employee Schedule"].Columns.Count; i++) { employeeScheduleEntry[i] = scheduleData[i]; } // Add the newly created Employee and Employee Schedule rows to the DataSet database.Tables["Employees"].Rows.Add(employeeEntry); database.Tables["Employee Schedule"].Rows.Add(employeeScheduleEntry); } }
/// <summary> /// When the "Add Customer" button is clicked, prompt the user to enter the information of the new /// customer, and then add the values to the database. Update the grid after the customer was added. /// </summary> private void addCustomerButton_Click(object sender, EventArgs e) { // Create and display an "Add Customer" form for the user to enter customer information. // This form will also insert the new entries to the database. AddCustomerForm prompt = new AddCustomerForm(); prompt.ShowDialog(); // If the form was fully filled in and the entry was successfully inserted in the database, // create new rows and update the grid. if (prompt.DialogResult == DialogResult.OK) { // Hold the customer's basic information and attendance values that are retrieved from the database, // from the newly added customer entries. string[] customerData = DatabaseWorker.GetCustomerEntry(DatabaseWorker.tempId); string[] customerAttendanceData = DatabaseWorker.GetCustomerAttendanceEntry(DatabaseWorker.tempId); // Create new rows that will hold the newly added customer's basic information // and attendance values. DataRow customerEntry = database.Tables["Customers"].NewRow(); DataRow customerAttendanceEntry = database.Tables["Customer Attendance"].NewRow(); // Populate each column of the new Customer and Customer Attendance rows with data from the database. for (int i = 0; i < database.Tables["Customers"].Columns.Count; i++) { customerEntry[i] = customerData[i]; } for (int i = 0; i < database.Tables["Customer Attendance"].Columns.Count; i++) { customerAttendanceEntry[i] = customerAttendanceData[i]; } // Add the newly created Customer and Customer Attendance rows to the DataSet database.Tables["Customers"].Rows.Add(customerEntry); database.Tables["Customer Attendance"].Rows.Add(customerAttendanceEntry); } }
/// <summary> /// **************** TODO: COMPLETE DOCUMENTATION ***************** /// /// When the "Remove Entry" button is clicked, remove the selected employee and customer entries /// along with the related schedule and attendance entries respectively, from the database and from the /// DataSet. Update the grid to the reflect these changes. /// </summary> private void removeButton_Click(object sender, EventArgs e) { try { GridRangeInfoList range = databaseGrid.TableModel.Selections.GetSelectedRows(true, true); foreach (GridRangeInfo info in range) { Element element = databaseGrid.TableModel.GetDisplayElementAt(info.Top); GridNestedTable gnt = element as GridNestedTable; GridNestedTable gnt1 = gnt; while (gnt1 != null && gnt1.ChildTable != null) { gnt = gnt1; gnt1 = gnt.ChildTable.ParentTable.CurrentElement as GridNestedTable; } DataRowView drv = gnt.ChildTable.ParentTable.CurrentElement.GetData() as DataRowView; string table = gnt.ChildTable.ParentTable.CurrentElement.GetRecord().ParentChildTable.Name; if (table == "Employees") { DatabaseWorker.RemoveEmployee(Convert.ToInt32(drv[0])); Record record = gnt.ChildTable.ParentTable.CurrentElement.GetRecord(); int index = databaseGrid.GetTable("Employees").UnsortedRecords.IndexOf(record); databaseGrid.GetTable("Employee Schedule").UnsortedRecords[index].Delete(); record.Delete(); database.Tables["Employees"].Rows[index].Delete(); database.Tables["Employee Schedule"].Rows[index].Delete(); database.AcceptChanges(); } else if (table == "Customers") { DatabaseWorker.RemoveCustomer(Convert.ToInt32(drv[0])); Record record = gnt.ChildTable.ParentTable.CurrentElement.GetRecord(); int index = databaseGrid.GetTable("Customers").UnsortedRecords.IndexOf(record); databaseGrid.GetTable("Customer Attendance").UnsortedRecords[index].Delete(); record.Delete(); database.Tables["Customers"].Rows[index].Delete(); database.Tables["Customer Attendance"].Rows[index].Delete(); database.AcceptChanges(); } else if (table == "Employee Schedule") { MessageBoxAdv.Show(this, "Cannot remove employee schedule entry directly. \n" + "To remove it, delete the corresponding employee entry.", "Error"); } else if (table == "Customer Attendance") { MessageBoxAdv.Show(this, "Cannot remove customer attendance entry directly. \n" + "To remove it, delete the corresponding customer entry.", "Error"); } } } catch (Exception) { MessageBoxAdv.Show(this, "Not a valid entry", "Error"); } }
/// <summary> /// Whenever the value of a cell is edited, update the database entry and the corresponding DataSet value. /// Update the grid to reflect these changes. /// </summary> private void DatabaseGrid_RecordValueChanged(object sender, RecordValueChangedEventArgs e) { try { // Object that holds the values of the record that the edited cell belongs to. DataRowView drv = e.Record.GetData() as DataRowView; // If the cell's record belongs to the Employee table. if (e.Record.ParentChildTable.Name == "Employees") { // Queue a 'Edit Employee' task to the Thread pool. ThreadPool.QueueUserWorkItem(state => { // Increments the job counter in a thread-safe manner. Interlocked.Increment(ref jobCount); string[] updatedValues = { drv[1].ToString(), drv[2].ToString(), drv[3].ToString(), drv[4].ToString() }; // Edit the database employee entry. DatabaseWorker.EditEmployee(Convert.ToInt32(drv[0]), updatedValues[0], updatedValues[1], updatedValues[2], updatedValues[3]); // Get the index of the grid entry. int index = databaseGrid.GetTable("Employees").UnsortedRecords.IndexOf(e.Record); // Set the 'Name' field in the employee schedule entry that has the same index. databaseGrid.GetTable("Employee Schedule").UnsortedRecords[index].SetValue("Name", drv[1].ToString()); // Decrements the job counter in a thread-safe manner. Interlocked.Decrement(ref jobCount); } ); } // If the cell's record belongs to the Employee Schedule table else if (e.Record.ParentChildTable.Name == "Employee Schedule") { string[] updatedScheduleValues = new string[7]; // Get updated schedule values. for (int i = 2; i < 9; i++) { updatedScheduleValues[i - 2] = drv[i].ToString(); } // Queue a 'Edit Employee Schedule' task to the thread pool. ThreadPool.QueueUserWorkItem(state => { // Increments the job counter in a thread-safe manner. Interlocked.Increment(ref jobCount); DatabaseWorker.EditEmployeeSchedule(Convert.ToInt32(drv[0]), updatedScheduleValues); // Decrements the job counter in a thread-safe manner. Interlocked.Decrement(ref jobCount); } ); } // If the cell's record belongs to the Customer table else if (e.Record.ParentChildTable.Name == "Customers") { // Queue a 'Edit Customer' task to the thread pool. ThreadPool.QueueUserWorkItem(state => { // Increments the job counter in a thread-safe manner. Interlocked.Increment(ref jobCount); string[] updatedValues = { drv[1].ToString(), drv[2].ToString(), drv[3].ToString() }; // Edit the customer database entry. DatabaseWorker.EditCustomer(Convert.ToInt32(drv[0]), updatedValues[0], updatedValues[1], updatedValues[2]); // Get the index of the grid entry. int index = databaseGrid.GetTable("Customers").UnsortedRecords.IndexOf(e.Record); // Set the 'Name' field in the customer attendance entry that has the same index. databaseGrid.GetTable("Customer Attendance").UnsortedRecords[index].SetValue("Name", drv[1].ToString()); // Decrements the job counter in a thread-safe manner. Interlocked.Decrement(ref jobCount); } ); } // If the cell's record belongs to the Customer Attendance table. else if (e.Record.ParentChildTable.Name == "Customer Attendance") { string[] updatedAttendanceValues = new string[7]; for (int i = 2; i < 9; i++) { updatedAttendanceValues[i - 2] = drv[i].ToString(); } // Queue a 'Edit Customer Attendance' task to the thread pool ThreadPool.QueueUserWorkItem(state => { // Increments the job counter in a thread-safe manner. Interlocked.Increment(ref jobCount); DatabaseWorker.EditCustomerAttendance(Convert.ToInt32(drv[0]), updatedAttendanceValues); // Decrements the job counter in a thread-safe manner. Interlocked.Decrement(ref jobCount); } ); } } catch (Exception) { MessageBoxAdv.Show(this, "Error editing value.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// When the "Remove Entry" button is clicked, remove the selected employee and customer entries, /// along with the related schedule and attendance entries respectively, from the database and from the /// DataSet. Update the grid to reflect these changes. /// </summary> private void RemoveButton_Click(object sender, EventArgs e) { try { // Get selected row. GridRangeInfo info = databaseGrid.TableModel.Selections.GetSelectedRows(true, true)[0]; // Get an element fromt the row. Element element = databaseGrid.TableModel.GetDisplayElementAt(info.Top); // Get the table the element belongs to. GridNestedTable gnt = element as GridNestedTable; // Get the data of the row. DataRowView drv = gnt.ChildTable.ParentTable.CurrentElement.GetData() as DataRowView; // Get the name of the table the row belongs to. string table = gnt.ChildTable.Name; if (table == "Employees") { // Remove the employee and corresponding employee schedule entry from the database. DatabaseWorker.RemoveEmployee(Convert.ToInt32(drv[0])); // Get the grid employee entry. Record record = gnt.ChildTable.ParentTable.CurrentElement.GetRecord(); // Get the index of the employee entry. // This index is the same as the index of the corresponding employee schedule entry. int index = databaseGrid.GetTable("Employees").UnsortedRecords.IndexOf(record); // Delete the corresponding employee schedule entry from the grid. databaseGrid.GetTable("Employee Schedule").UnsortedRecords[index].Delete(); // Delete the employee entry from the grid. record.Delete(); // Delete the employee and employee schedule entries from the DataSet. database.Tables["Employees"].Rows[index].Delete(); database.Tables["Employee Schedule"].Rows[index].Delete(); // Commit changes to the DataSet. database.AcceptChanges(); } else if (table == "Customers") { // Remove the customer and corresponding customer attendance entry from the database. DatabaseWorker.RemoveCustomer(Convert.ToInt32(drv[0])); // Get the grid customer entry. Record record = gnt.ChildTable.ParentTable.CurrentElement.GetRecord(); // Get the index of the customer entry. // This index is the same as the index of the corresponding customer attendance entry. int index = databaseGrid.GetTable("Customers").UnsortedRecords.IndexOf(record); // Delete the corresponding customer attendance entry from the grid. databaseGrid.GetTable("Customer Attendance").UnsortedRecords[index].Delete(); // Delete the customer entry from the grid. record.Delete(); // Delete the customer and customer attendance entries from the DataSet. database.Tables["Customers"].Rows[index].Delete(); database.Tables["Customer Attendance"].Rows[index].Delete(); // Commit changes to the DataSet. database.AcceptChanges(); } // If the table is neither of the above, display the appropriate error message. else if (table == "Employee Schedule") { MessageBoxAdv.Show(this, "Cannot remove employee schedule entry directly. \n" + "To remove it, delete the corresponding employee entry.", "Error"); } else if (table == "Customer Attendance") { MessageBoxAdv.Show(this, "Cannot remove customer attendance entry directly. \n" + "To remove it, delete the corresponding customer entry.", "Error"); } } catch (Exception) { } }
private void compactDatabaseButton_Click(object sender, EventArgs e) { DatabaseWorker.CompactDatabase(); }
/// <summary> /// **************** TODO: COMPLETE DOCUMENTATION ***************** /// /// Whenever the value of a cell is edited, update the database entry and the corresponding DataSet value. /// Updat the grid to reflect these changes. /// </summary> private void databaseGrid_RecordValueChanged(object sender, RecordValueChangedEventArgs e) { // Object that holds the values of the record that the edited cell belongs to. DataRowView drv = e.Record.GetData() as DataRowView; // If the cell's record belongs to the Employee table if (e.Record.ParentChildTable.Name == "Employees") { ThreadPool.QueueUserWorkItem(state => { DatabaseWorker.EditEmployee(Convert.ToInt32(drv[0]), drv[1].ToString(), drv[2].ToString(), drv[3].ToString(), drv[4].ToString()); string[] updatedEmployeeValues = DatabaseWorker.GetEmployeeEntryNoId(Convert.ToInt32(drv[0])); int index = databaseGrid.GetTable("Employees").UnsortedRecords.IndexOf(e.Record); e.Record.UpdateValues(updatedEmployeeValues); databaseGrid.GetTable("Employee Schedule").UnsortedRecords[index].SetValue("Name", drv[1].ToString()); } ); } // If the cell's record belongs to the Employee Schedule table else if (e.Record.ParentChildTable.Name == "Employee Schedule") { string[] scheduleValues = new string[7]; for (int i = 2; i < 9; i++) { scheduleValues[i - 2] = drv[i].ToString(); } ThreadPool.QueueUserWorkItem(state => { DatabaseWorker.EditEmployeeSchedule(Convert.ToInt32(drv[0]), scheduleValues); string[] updatedScheduleValues = DatabaseWorker.GetEmployeeScheduleEntry(Convert.ToInt32(drv[0])); e.Record.UpdateValues(updatedScheduleValues); } ); } // If the cell's record belongs to the Customer table else if (e.Record.ParentChildTable.Name == "Customers") { ThreadPool.QueueUserWorkItem(state => { DatabaseWorker.EditCustomer(Convert.ToInt32(drv[0]), drv[1].ToString(), drv[2].ToString(), drv[3].ToString()); string[] updatedCustomerValues = DatabaseWorker.GetCustomerEntryNoId(Convert.ToInt32(drv[0])); int index = databaseGrid.GetTable("Customers").UnsortedRecords.IndexOf(e.Record); e.Record.UpdateValues(updatedCustomerValues); databaseGrid.GetTable("Customer Attendance").UnsortedRecords[index].SetValue("Name", drv[1].ToString()); } ); } // If the cell's record belongs the Customer Attendance table. else if (e.Record.ParentChildTable.Name == "Customer Attendance") { string[] attendanceValues = new string[7]; for (int i = 2; i < 9; i++) { attendanceValues[i - 2] = drv[i].ToString(); } ThreadPool.QueueUserWorkItem(state => { DatabaseWorker.EditCustomerAttendance(Convert.ToInt32(drv[0]), attendanceValues); string[] updatedAttendanceValues = DatabaseWorker.GetCustomerAttendanceEntry(Convert.ToInt32(drv[0])); e.Record.UpdateValues(updatedAttendanceValues); } ); } }