Cleaned up the table trimming methods. Group creation now proper marks rows as dirty when row attribute changes. Projections table processes successfully on the new modify record form.

This commit is contained in:
2017-01-20 22:46:48 -06:00
parent 9fa2e3c44a
commit 43bf07c212
10 changed files with 651 additions and 245 deletions
+604 -19
View File
@@ -878,7 +878,7 @@ namespace AdvertsingProfitControl
return;
}
//Otherwise, show an error.
dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Ad Item needed";
dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "This column can't be blank!";
break;
case (int)SalesTableColumns.Sold: //Sold
//This Reg-ex pattern will match any number followed by the word bin(s), to allow specifying the number of bins of product were ordered.
@@ -1329,11 +1329,16 @@ namespace AdvertsingProfitControl
return;
}
//If column one (1) is blank then cancel cell validating.
dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Ad Item needed";
dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "This column can't be blank!";
break;
default:
//Purely here for protection against parsing the Boolean columns by mistake.
if (e.ColumnIndex == (int)InventoryTableColumns.Id || e.ColumnIndex >= (int)InventoryTableColumns.IsHeaderRow) { return; }
if (userInput == string.Empty)
{
dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = userInput;
return;
}
//This Reg-ex pattern will match any number followed by the word bin(s), to allow specifying the number of bins of product were ordered.
var inventoryStringCheck = new Regex(@"^[0-9]{1,2} \bbin(s){0,1}\b", RegexOptions.IgnoreCase);
@@ -2525,6 +2530,12 @@ namespace AdvertsingProfitControl
}
}
}
//Set the is dirty cell to false.
var isDirtyCell = new DataGridViewCheckBoxCell
{
Value = false
};
newRow.Cells.Add(isDirtyCell);
projectionsDataGridView.Rows.Add(newRow);
}
//Manually write the row number to the new row.
@@ -2619,7 +2630,13 @@ namespace AdvertsingProfitControl
}
}
}
inventoryDataGridView.Rows.Add(newRow);
//Set the is dirty cell to false.
var isDirtyCell = new DataGridViewCheckBoxCell
{
Value = false
};
newRow.Cells.Add(isDirtyCell);
inventoryDataGridView.Rows.Add(newRow);
}
//Manually write the row number to the new row.
inventoryDataGridView.Rows[inventoryDataGridView.Rows.Count - 1].HeaderCell.Value = inventoryDataGridView.Rows.Count.ToString();
@@ -2713,6 +2730,12 @@ namespace AdvertsingProfitControl
}
}
}
//Set the is dirty cell to false.
var isDirtyCell = new DataGridViewCheckBoxCell
{
Value = false
};
newRow.Cells.Add(isDirtyCell);
actualSalesDataGridView.Rows.Add(newRow);
}
//Manually write the row number to the new row.
@@ -2755,6 +2778,12 @@ namespace AdvertsingProfitControl
cell.Value = invoices.Rows[rowIndex].ItemArray[cellIndex].ToString();
row.Cells.Add(cell);
}
//Set the is dirty cell to false.
var isDirtyCell = new DataGridViewCheckBoxCell
{
Value = false
};
row.Cells.Add(isDirtyCell);
invoicesDataGridView.Rows.Add(row);
}
}
@@ -3067,12 +3096,69 @@ namespace AdvertsingProfitControl
#region Database Update/Insertion Methods
private bool SaveRecords()
/// <summary>
/// Saves all changes made to the form to the database.
/// </summary>
/// <param name="displayInformation">Whether or not to update the information label with the status.</param>
/// <returns>True on success otherwise, false.</returns>
private bool SaveRecords(bool displayInformation = true)
{
var success = false;
//Get the date ID for the current active date.
var dateId = GetDateId(_currentActiveDate.ToString("d"));
if (dateId == 0)
{
MessageBox.Show(@"Failed to get the date ID number.", @"Invalid ID Number", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
//Create the database interaction objects.
var dbT = new DatabaseTracker();
var dbW = new DatabaseWriter(dbT.DatabaseConnectionString);
DataTable newInsertProjectionsTable;
DataTable updateProjectionsTable;
var projectionsTrimmingStatus = ConstructCleanedSalesTable("Projections", dateId, out newInsertProjectionsTable, out updateProjectionsTable);
if (projectionsTrimmingStatus != TrimmingOperationResult.NoChangesRequired && projectionsTrimmingStatus != TrimmingOperationResult.FailedToTrim)
{
//Check to see if the insert table has any items.
if (newInsertProjectionsTable.Rows.Count > 0)
{
var writerResult = dbW.InsertIntoSalesTable(newInsertProjectionsTable, dbT.DatabaseConnectionString);
if (writerResult.GetWritingOperationStatus() != WritingOperationStatus.Failed)
{
//Spin through the collection and update the affected rows.
foreach (var rowIndex in writerResult.GetRowCollection())
{
projectionsDataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.Id].Value = rowIndex.Value;
projectionsDataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.IsDirty].Value = false;
projectionsDataGridView.Rows[rowIndex.Key - 1].HeaderCell.Style.BackColor = ApplicationColors.EditingSaved;
}
}
}
//Next check to see if the update table has anything.
if (updateProjectionsTable.Rows.Count > 0)
{
var writerResult = dbW.UpdateSalesTable(updateProjectionsTable, dbT.DatabaseConnectionString);
if (writerResult.GetWritingOperationStatus() != WritingOperationStatus.Failed)
{
//Spin through the collection and update the affected rows.
foreach (var rowIndex in writerResult.GetRowCollection())
{
//Only reset the IsDirty value to false since the updates when through.
projectionsDataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.IsDirty].Value = false;
projectionsDataGridView.Rows[rowIndex.Key - 1].HeaderCell.Style.BackColor = ApplicationColors.EditingSaved;
}
}
}
if (displayInformation) informationLabel.Text += @"Successfully saved projections." + Environment.NewLine;
}
else if (projectionsTrimmingStatus == TrimmingOperationResult.NoChangesRequired)
{
if (displayInformation) informationLabel.Text += @"No changes to projections detected." + Environment.NewLine;
}
else
{
errorLabel.Text += @"Failed to process projections." + Environment.NewLine;
}
return success;
}
@@ -3087,29 +3173,528 @@ namespace AdvertsingProfitControl
var dbT = new DatabaseTracker();
var dbW = new DatabaseWriter(dbT.DatabaseConnectionString);
var dbR = new DatabaseReader();
var dateId = dbR.RetrieveDateIdByDateString(weekEndingCalendar.SelectionStart.ToString("d"), dbT.DatabaseConnectionString);
var dateId = dbR.RetrieveDateIdByDateString(date, dbT.DatabaseConnectionString);
//If the ID is zero (0) that means the date isn't in the database so simply insert it.
if (dateId == 0)
if (dateId != 0) return dateId;
//Try inserting the date string.
if (dbW.InsertIntoWeekEnding(date))
{
//Try inserting the date string.
if (dbW.InsertIntoWeekEnding(weekEndingCalendar.SelectionStart.ToString("d")))
dateId = dbR.RetrieveDateIdByDateString(date, dbT.DatabaseConnectionString);
}
else
{
//The above method reports that it failed to insert the date into the database.
errorLabel.Text = @"Failed to insert the date '" + date + @"' into the database.";
}
return dateId;
}
#endregion
#region Table Trimming Operations
private TrimmingOperationResult ConstructCleanedSalesTable(string tableName, int dateId, out DataTable newInsertTable, out DataTable updateTable)
{
//Create two DataTables one for the new items to be added to the database
//and one for items that have to be updated.
//New Sales Table Layout (Based on the Database's Physical Layout)
//0:Sold 1:SalePrice 2:TotalSales 3:cost 4:ProfitReturn 5:TotalProfitReturn
//6:FK_AdItemID 7:RowAttribute 8:FK_AdSpecialGroupName (ID) 9:RowPosition (not index based)
//10:FK_DateID
newInsertTable = new DataTable(tableName);
//Update Sales Table Layout (Based on the Database's Physical Layout)
//0:ID 1:Sold 2:SalePrice 3:TotalSales 4:cost 5:ProfitReturn 6:TotalProfitReturn
//7:FK_AdItemID 8:RowAttribute 9:FK_AdSpecialGroupName (ID) 10:RowPosition (not index based)
//11:FK_DateID
updateTable = new DataTable(tableName);
//Create a reference to the DataGridView that will be used (either Projections or Actual Sales).
var dataGridView = tableName == "Projections" ? projectionsDataGridView : actualSalesDataGridView;
//Construct a list of column names for the projections/actual sales DataGridViews and the inventory DataGirdView.
string[] saleColumnNames =
{
"ID", "Sold", "SalePrice", "TotalSales", "Cost", "ProfitReturn",
"TotalProfitReturn", "AdItemID", "RowAttribute", "AdSpecialID", "RowPosition", "DateID"
};
foreach (var columnName in saleColumnNames)
{
if (columnName == "ID") continue;
var column = new DataColumn(columnName);
newInsertTable.Columns.Add(column);
}
foreach (var columnName in saleColumnNames)
{
var column = new DataColumn(columnName);
updateTable.Columns.Add(column);
}
//Create the database interaction objects.
var databaseTracker = new DatabaseTracker();
var databaseReader = new DatabaseReader();
var databaseWriter = new DatabaseWriter(databaseTracker.DatabaseConnectionString);
var adSpecialId = 0; //Entries in the database are not allowed to be zero (unique ID wise that is).
//Grab the ad special ID, assuming there is one.
if (_adSpecialIndex != -1)
{
//An ad special does exist so grab its ID from the database.
int.TryParse(databaseReader.RetrieveGroupIdByString(
dataGridView.Rows[_adSpecialIndex].Cells[(int)SalesTableColumns.AdItem]
.EditedFormattedValue.ToString(), databaseTracker.DatabaseConnectionString), out adSpecialId);
}
//Begin spinning through all the rows in the projections table.
foreach (DataGridViewRow row in dataGridView.Rows)
{
//Always check for new row.
if (row.IsNewRow) break;
//Check to see if the current row is the ad special row.
if (row.Index == _adSpecialIndex)
{
dateId = dbR.RetrieveDateIdByDateString(weekEndingCalendar.SelectionStart.ToString("d"), dbT.DatabaseConnectionString);
//Now if its still zero (0) then that means something went really wrong and failed to insert.
if (dateId == 0)
continue;
}
//Check to see if the row is dirty.
if (!(bool)row.Cells[(int)SalesTableColumns.IsDirty].Value)
{
//If it is not then continue on to the next row.
continue;
}
//Try grabbing the row's ID number (the number that it is in the database).
var rowIdNumber = row.Cells[(int)SalesTableColumns.Id].EditedFormattedValue.ToString() == "" ? 0 : int.Parse(row.Cells[(int)SalesTableColumns.Id].EditedFormattedValue.ToString());
//Grab the ad item ID.
var adItemId = int.Parse(databaseReader.RetrieveAdItemId(row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue.ToString(), databaseTracker.DatabaseConnectionString));
//If the return value is zero (0) then the ad item is not in the database so try to add it.
if (adItemId == 0)
{
//Add the item to the database.
adItemId = databaseWriter.InsertNewAdItem(row.Cells[(int)SalesTableColumns.AdItem].EditedFormattedValue.ToString());
if (adItemId == 0)
{
MessageBox.Show(
@"Failed to retrieve date ID after supposedly inserting the date into the database.",
@"Failed To Get Date ID", MessageBoxButtons.OK, MessageBoxIcon.Error);
//TODO: Throw an exception, this can not be allowed.
//AdItemInsertionFailedException
newInsertTable.Rows.Clear(); //Work around for now
updateTable.Rows.Clear();
errorLabel.Text = @"Failed to ad item to database.";
return TrimmingOperationResult.FailedToTrim;
}
}
//Determine the row's attribute.
var rowAttribute = 0; //Zero (0) means no grouping, its not a header nor a member.
if ((bool)row.Cells[(int)SalesTableColumns.IsHeaderRow].Value && !(bool)row.Cells[(int)SalesTableColumns.IsMemberRow].Value)
{
rowAttribute = 1;
}
else if ((bool)row.Cells[(int)SalesTableColumns.IsMemberRow].Value && !(bool)row.Cells[(int)SalesTableColumns.IsHeaderRow].Value)
{
rowAttribute = 2;
}
//Add the values to their respective data table.
if (rowIdNumber == 0)
{
//This table is full of data not in the database so the ID column isn't needed.
var newRow = new object[11];
newRow[0] = row.Cells[(int)SalesTableColumns.Sold].EditedFormattedValue.ToString();//Sold, is string
newRow[1] = row.Cells[(int)SalesTableColumns.SalePrice].EditedFormattedValue.ToString();//SalePrice, is string
newRow[2] = row.Cells[(int)SalesTableColumns.TotalSales].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(row.Cells[(int)SalesTableColumns.TotalSales].EditedFormattedValue.ToString());//TotalSales, must be a number
newRow[3] = row.Cells[(int)SalesTableColumns.Cost].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(row.Cells[(int)SalesTableColumns.Cost].EditedFormattedValue.ToString());//Cost, must be a number
newRow[4] = row.Cells[(int)SalesTableColumns.ProfitReturn].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(row.Cells[(int)SalesTableColumns.ProfitReturn].EditedFormattedValue.ToString());//ProfitReturn, must be a number
newRow[5] = row.Cells[(int)SalesTableColumns.TotalProfitReturn].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(row.Cells[(int)SalesTableColumns.TotalProfitReturn].EditedFormattedValue.ToString());//TotalProfitReturn, must be a number
newRow[6] = adItemId;
newRow[7] = rowAttribute;
if (_adSpecialIndex != -1 && row.Index > _adSpecialIndex)
{
newRow[8] = adSpecialId;
newRow[9] = row.Index; //"Subtract" one since we don't need to acknowledge the Ad Special Row's existence.
}
else
{
newRow[8] = 0;
newRow[9] = row.Index + 1;
}
newRow[10] = dateId;
newInsertTable.Rows.Add(newRow);
}
else
{
//The above method reports that it failed to insert the date into the database.
MessageBox.Show(@"Failed to insert the date '" + weekEndingCalendar.SelectionStart.ToString("d") + @"' into the database.", @"Failed To Get Date ID", MessageBoxButtons.OK, MessageBoxIcon.Error);
//This table is full of data that is already in the database.
var newRow = new object[12];
newRow[0] = rowIdNumber;
newRow[1] = row.Cells[(int)SalesTableColumns.Sold].EditedFormattedValue.ToString();//Sold, is string
newRow[2] = row.Cells[(int)SalesTableColumns.SalePrice].EditedFormattedValue.ToString();//SalePrice, is string
newRow[3] = row.Cells[(int)SalesTableColumns.TotalSales].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(row.Cells[(int)SalesTableColumns.TotalSales].EditedFormattedValue.ToString());//TotalSales, must be a number
newRow[4] = row.Cells[(int)SalesTableColumns.Cost].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(row.Cells[(int)SalesTableColumns.Cost].EditedFormattedValue.ToString());//Cost, must be a number
newRow[5] = row.Cells[(int)SalesTableColumns.ProfitReturn].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(row.Cells[(int)SalesTableColumns.ProfitReturn].EditedFormattedValue.ToString());//ProfitReturn, must be a number
newRow[6] = row.Cells[(int)SalesTableColumns.TotalProfitReturn].EditedFormattedValue.ToString() == "" ? 0 : double.Parse(row.Cells[(int)SalesTableColumns.TotalProfitReturn].EditedFormattedValue.ToString());//TotalProfitReturn, must be a number
newRow[7] = adItemId;
newRow[8] = rowAttribute;
if (_adSpecialIndex != -1 && row.Index > _adSpecialIndex)
{
newRow[9] = adSpecialId;
newRow[10] = row.Index; //"Subtract" one since we don't need to acknowledge the Ad Special Row's existence.
}
else
{
newRow[9] = 0;
newRow[10] = row.Index + 1;
}
newRow[11] = dateId;
updateTable.Rows.Add(newRow);
}
}
return dateId;
if (newInsertTable.Rows.Count == 0 && updateTable.Rows.Count == 0)
{
return TrimmingOperationResult.NoChangesRequired;
}
if (newInsertTable.Rows.Count > 0 && updateTable.Rows.Count == 0)
{
return TrimmingOperationResult.CreatedNewInsertionTable;
}
if (newInsertTable.Rows.Count == 0 && updateTable.Rows.Count > 0)
{
return TrimmingOperationResult.CreatedUpdateTable;
}
return TrimmingOperationResult.CreatedNewInsertionAndUpdateTables;
}
private TrimmingOperationResult ConstructCleanedInventoryTable(int dateId, out DataTable newInsertTable, out DataTable updateTable)
{
//Create two DataTables one for the new items to be added to the database
//and one for items that have to be updated.
//New inventory Table Layout (Based on the Database's Physical Layout)
//0:Beginning Inventory 1:Received 2:Total Inventory 3:Ending Inventory
//4:AdItemID 5:RowAttribute 6:GroupID 7:RowPosition 8:DateID
newInsertTable = new DataTable("Inventory");
//Update Sales Table Layout (Based on the Database's Physical Layout)
//0:ID 1:BeginningInventory 2:Received 3:TotalInventory 4:EndingInventory
//5:AdItemID 6:RowAttribute 7:GroupID 8:RowPosition 9:DateID
updateTable = new DataTable("Inventory");
//Construct a list of column names for the projections/actual sales DataGridViews and the inventory DataGirdView.
string[] saleColumnNames =
{
"ID", "Sold", "SalePrice", "TotalSales", "Cost", "ProfitReturn",
"TotalProfitReturn", "AdItemID", "RowAttribute", "AdSpecialID", "RowPosition", "DateID"
};
foreach (var columnName in saleColumnNames)
{
if (columnName == "ID") continue;
var column = new DataColumn(columnName);
newInsertTable.Columns.Add(column);
}
foreach (var columnName in saleColumnNames)
{
var column = new DataColumn(columnName);
updateTable.Columns.Add(column);
}
//Create the database interaction objects.
var databaseTracker = new DatabaseTracker();
var databaseReader = new DatabaseReader();
var databaseWriter = new DatabaseWriter(databaseTracker.DatabaseConnectionString);
var adSpecialId = 0; //Entries in the database are not allowed to be zero (unique ID wise that is).
//Grab the ad special ID, assuming there is one.
if (_adSpecialIndex != -1)
{
//An ad special does exist so grab its ID from the database.
int.TryParse(databaseReader.RetrieveGroupIdByString(
inventoryDataGridView.Rows[_adSpecialIndex].Cells[(int)InventoryTableColumns.AdItem]
.EditedFormattedValue.ToString(), databaseTracker.DatabaseConnectionString), out adSpecialId);
}
//Begin spinning through all the rows in the projections table.
foreach (DataGridViewRow row in inventoryDataGridView.Rows)
{
//Always check for new row.
if (row.IsNewRow) break;
//Check to see if the current row is the ad special row.
if (row.Index == _adSpecialIndex)
{
//LogConsole.WriteToLog(FrmLogConsole.Level.Verbose, "Ad Special Row found at index " + row.Index + ".");
continue;
}
//Check to see if the row is dirty.
if (!(bool)row.Cells[(int)InventoryTableColumns.IsDirty].Value)
{
//If it is not then continue on to the next row.
continue;
}
//Try grabbing the row's ID number (the number that it is in the database).
var rowIdNumber = row.Cells[(int)InventoryTableColumns.Id].EditedFormattedValue.ToString() == "" ? 0 : int.Parse(row.Cells[(int)InventoryTableColumns.Id].EditedFormattedValue.ToString());
//Grab the ad item ID.
var adItemId = int.Parse(databaseReader.RetrieveAdItemId(row.Cells[(int)InventoryTableColumns.AdItem].EditedFormattedValue.ToString(), databaseTracker.DatabaseConnectionString));
//If the return value is zero (0) then the ad item is not in the database so try to add it.
if (adItemId == 0)
{
//Add the item to the database.
adItemId = databaseWriter.InsertNewAdItem(row.Cells[(int)InventoryTableColumns.AdItem].EditedFormattedValue.ToString());
if (adItemId == 0)
{
//TODO: Throw an exception, this can not be allowed.
//AdItemInsertionFailedException
newInsertTable.Rows.Clear(); //Work around for now
updateTable.Rows.Clear();
return TrimmingOperationResult.FailedToTrim;
}
}
//Determine the row's attribute.
var rowAttribute = 0; //Zero (0) means no grouping, its not a header nor a member.
if ((bool)row.Cells[(int)InventoryTableColumns.IsHeaderRow].Value && !(bool)row.Cells[(int)InventoryTableColumns.IsMemberRow].Value)
{
rowAttribute = 1;
}
else if ((bool)row.Cells[(int)InventoryTableColumns.IsMemberRow].Value && !(bool)row.Cells[(int)InventoryTableColumns.IsHeaderRow].Value)
{
rowAttribute = 2;
}
//Add the values to their respective data table.
if (rowIdNumber == 0)
{
//This table is full of data not in the database so the ID column isn't needed.
var newRow = new object[9];
newRow[0] = row.Cells[(int)InventoryTableColumns.BeginningInventory].EditedFormattedValue.ToString();//Beginning inventory, is string
newRow[1] = row.Cells[(int)InventoryTableColumns.Recieved].EditedFormattedValue.ToString();//Received, is string
newRow[2] = row.Cells[(int)InventoryTableColumns.Total].EditedFormattedValue.ToString();//Total inventory. is string
newRow[3] = row.Cells[(int)InventoryTableColumns.EndingInventory].EditedFormattedValue.ToString(); //Ending inventory, is string
newRow[4] = adItemId;
newRow[5] = rowAttribute;
if (_adSpecialIndex != -1 && row.Index > _adSpecialIndex)
{
newRow[6] = adSpecialId;
newRow[7] = row.Index; //"Subtract" one since we don't need to acknowledge the Ad Special Row's existence.
}
else
{
newRow[6] = 0;
newRow[7] = row.Index + 1;
}
newRow[8] = dateId;
newInsertTable.Rows.Add(newRow);
}
else
{
//This table is full of data that is already in the database.
var newRow = new object[10];
newRow[0] = rowIdNumber;
newRow[1] = row.Cells[(int)InventoryTableColumns.BeginningInventory].EditedFormattedValue.ToString();//Beginning inventory, is string
newRow[2] = row.Cells[(int)InventoryTableColumns.Recieved].EditedFormattedValue.ToString();//Received, is string
newRow[3] = row.Cells[(int)InventoryTableColumns.Total].EditedFormattedValue.ToString();//Total inventory. is string
newRow[4] = row.Cells[(int)InventoryTableColumns.EndingInventory].EditedFormattedValue.ToString(); //Ending inventory, is string
newRow[5] = adItemId;
newRow[6] = rowAttribute;
if (_adSpecialIndex != -1 && row.Index > _adSpecialIndex)
{
newRow[7] = adSpecialId;
newRow[8] = row.Index; //"Subtract" one since we don't need to acknowledge the Ad Special Row's existence.
}
else
{
newRow[7] = 0;
newRow[8] = row.Index + 1;
}
newRow[9] = dateId;
updateTable.Rows.Add(newRow);
}
}
if (newInsertTable.Rows.Count == 0 && updateTable.Rows.Count == 0)
{
return TrimmingOperationResult.NoChangesRequired;
}
if (newInsertTable.Rows.Count > 0 && updateTable.Rows.Count == 0)
{
return TrimmingOperationResult.CreatedNewInsertionTable;
}
if (newInsertTable.Rows.Count == 0 && updateTable.Rows.Count > 0)
{
return TrimmingOperationResult.CreatedUpdateTable;
}
return TrimmingOperationResult.CreatedNewInsertionAndUpdateTables;
}
private bool ProcessTrimmingStatusResult(DataGridView dataGridView, string tableName, TrimmingOperationResult operationStatus, DataTable trimmedTable, DataTable updateTable)
{
//Create the database interaction objects.
var dbT = new DatabaseTracker();
var dbW = new DatabaseWriter(dbT.DatabaseConnectionString);
DbTableWriterStatus dbWriterStatus;
var successful = false;
switch (operationStatus)
{
case TrimmingOperationResult.NoChangesRequired:
informationLabel.Text += @"No changes to the " + TextFormat.AddSpacesToSentence(tableName, false) + @" table detected." + Environment.NewLine;
successful = true;
break;
case TrimmingOperationResult.CreatedNewInsertionTable:
dbWriterStatus = dbW.InsertIntoSalesTable(trimmedTable, dbT.DatabaseConnectionString);
if (dbWriterStatus.GetErrorMessage() == string.Empty)
{
//Spin through the collection and update the affected rows.
foreach (var rowIndex in dbWriterStatus.GetRowCollection())
{
dataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.Id].Value = rowIndex.Value;
dataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.IsDirty].Value = false;
dataGridView.Rows[rowIndex.Key - 1].HeaderCell.Style.BackColor = ApplicationColors.EditingSaved;
}
informationLabel.Text += TextFormat.AddSpacesToSentence(tableName, false) + @" table successfully added to the database." + Environment.NewLine;
successful = true;
}
else
{
errorLabel.Text = @"Failed to insert " + trimmedTable + @" into the database." + Environment.NewLine;
}
break;
case TrimmingOperationResult.CreatedUpdateTable:
dbWriterStatus = dbW.UpdateSalesTable(updateTable, dbT.DatabaseConnectionString);
if (dbWriterStatus.GetErrorMessage() == string.Empty)
{
//Spin through the collection and update the affected rows.
foreach (var rowIndex in dbWriterStatus.GetRowCollection())
{
//Only reset the IsDirty value to false since the updates when through.
dataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.IsDirty].Value = false;
dataGridView.Rows[rowIndex.Key - 1].HeaderCell.Style.BackColor = ApplicationColors.EditingSaved;
}
informationLabel.Text += TextFormat.AddSpacesToSentence(tableName, false) + @" table successfully updated." + Environment.NewLine;
successful = true;
}
else
{
errorLabel.Text = @"Failed to update the " + trimmedTable + @" table." + Environment.NewLine;
}
break;
case TrimmingOperationResult.CreatedNewInsertionAndUpdateTables:
//Insert the new values...
dbWriterStatus = dbW.InsertIntoSalesTable(trimmedTable, dbT.DatabaseConnectionString);
if (dbWriterStatus.GetErrorMessage() == string.Empty)
{
//Spin through the collection and update the affected rows.
foreach (var rowIndex in dbWriterStatus.GetRowCollection())
{
//Add the ID numbers to the first column since these are new additions to the database.
dataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.Id].Value = rowIndex.Value;
dataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.IsDirty].Value = false;
dataGridView.Rows[rowIndex.Key - 1].HeaderCell.Style.BackColor = ApplicationColors.EditingSaved;
}
informationLabel.Text += TextFormat.AddSpacesToSentence(tableName, false) + @" table successfully added to the database." + Environment.NewLine;
}
else
{
errorLabel.Text = @"Failed to insert " + trimmedTable + @" into the database." + Environment.NewLine;
}
//... and update the existing values.
dbWriterStatus = dbW.UpdateSalesTable(updateTable, dbT.DatabaseConnectionString);
if (dbWriterStatus.GetErrorMessage() == string.Empty)
{
//Spin through the collection and update the affected rows.
foreach (var rowIndex in dbWriterStatus.GetRowCollection())
{
//Only reset the IsDirty value to false since the updates when through.
dataGridView.Rows[rowIndex.Key - 1].Cells[(int)SalesTableColumns.IsDirty].Value = false;
dataGridView.Rows[rowIndex.Key - 1].HeaderCell.Style.BackColor = ApplicationColors.EditingSaved;
}
informationLabel.Text += TextFormat.AddSpacesToSentence(tableName, false) + @" table successfully updated." + Environment.NewLine;
successful = true;
}
else
{
errorLabel.Text = @"Failed to update the " + trimmedTable + @" table." + Environment.NewLine;
}
break;
}
return successful;
}
private bool ProcessTrimmingStatusForInventory(TrimmingOperationResult operationStatus, DataTable trimmedTable, DataTable updateTable)
{
//Create the database interaction objects.
var dbT = new DatabaseTracker();
var dbW = new DatabaseWriter(dbT.DatabaseConnectionString);
DbTableWriterStatus dbWriterStatus;
var successful = false;
switch (operationStatus)
{
case TrimmingOperationResult.NoChangesRequired:
informationLabel.Text += @"No changes to the Inventory table detected." + Environment.NewLine;
successful = true;
break;
case TrimmingOperationResult.CreatedNewInsertionTable:
dbWriterStatus = dbW.InsertIntoInventoryTable(trimmedTable, dbT.DatabaseConnectionString);
if (dbWriterStatus.GetErrorMessage() == string.Empty)
{
//Spin through the collection and update the affected rows.
foreach (var rowIndex in dbWriterStatus.GetRowCollection())
{
inventoryDataGridView.Rows[rowIndex.Key - 1].Cells[(int)InventoryTableColumns.Id].Value = rowIndex.Value;
inventoryDataGridView.Rows[rowIndex.Key - 1].Cells[(int)InventoryTableColumns.IsDirty].Value = false;
inventoryDataGridView.Rows[rowIndex.Key - 1].HeaderCell.Style.BackColor = ApplicationColors.EditingSaved;
}
informationLabel.Text += @"Inventory table successfully added to the database." + Environment.NewLine;
successful = true;
}
else
{
errorLabel.Text = @"Failed to insert Inventory table into the database." + Environment.NewLine;
}
break;
case TrimmingOperationResult.CreatedUpdateTable:
dbWriterStatus = dbW.UpdateInventoryTable(updateTable, dbT.DatabaseConnectionString);
if (dbWriterStatus.GetErrorMessage() == string.Empty)
{
//Spin through the collection and update the affected rows.
foreach (var rowIndex in dbWriterStatus.GetRowCollection())
{
//Only reset the IsDirty value to false since the updates when through.
inventoryDataGridView.Rows[rowIndex.Key - 1].Cells[(int)InventoryTableColumns.IsDirty].Value = false;
inventoryDataGridView.Rows[rowIndex.Key - 1].HeaderCell.Style.BackColor = ApplicationColors.EditingSaved;
}
informationLabel.Text += @"Inventory table successfully updated." + Environment.NewLine;
successful = true;
}
else
{
errorLabel.Text = @"Failed to update the changes in the Inventory table." + Environment.NewLine;
}
break;
case TrimmingOperationResult.CreatedNewInsertionAndUpdateTables:
//Insert the new values...
dbWriterStatus = dbW.InsertIntoInventoryTable(trimmedTable, dbT.DatabaseConnectionString);
if (dbWriterStatus.GetErrorMessage() == string.Empty)
{
//Spin through the collection and update the affected rows.
foreach (var rowIndex in dbWriterStatus.GetRowCollection())
{
//Add the ID numbers to the first column since these are new additions to the database.
inventoryDataGridView.Rows[rowIndex.Key - 1].Cells[(int)InventoryTableColumns.Id].Value = rowIndex.Value;
inventoryDataGridView.Rows[rowIndex.Key - 1].Cells[(int)InventoryTableColumns.IsDirty].Value = false;
inventoryDataGridView.Rows[rowIndex.Key - 1].HeaderCell.Style.BackColor = ApplicationColors.EditingSaved;
}
informationLabel.Text += @"Inventory table successfully added to the database." + Environment.NewLine;
successful = true;
}
else
{
errorLabel.Text = @"Failed to insert Inventory table into the database." + Environment.NewLine;
}
dbWriterStatus = dbW.UpdateInventoryTable(updateTable, dbT.DatabaseConnectionString);
if (dbWriterStatus.GetErrorMessage() == string.Empty)
{
//Spin through the collection and update the affected rows.
foreach (var rowIndex in dbWriterStatus.GetRowCollection())
{
//Only reset the IsDirty value to false since the updates when through.
inventoryDataGridView.Rows[rowIndex.Key - 1].Cells[(int)InventoryTableColumns.IsDirty].Value = false;
inventoryDataGridView.Rows[rowIndex.Key - 1].HeaderCell.Style.BackColor = ApplicationColors.EditingSaved;
}
informationLabel.Text += @"Inventory table successfully updated." + Environment.NewLine;
successful = true;
}
else
{
errorLabel.Text = @"Failed to update the changes in the Inventory table." + Environment.NewLine;
}
break;
}
inventoryDataGridView.RefreshEdit();
return successful;
}
private void addRecordButton_Click(object sender, EventArgs e)
{
SaveRecords();
}
#endregion