Fixed a bug in the Inventory and Actual Sales tables where the user could leave a row without an ad item. Fixed data type mismatch with the database insertion methods.

This commit is contained in:
2016-11-11 17:09:43 -06:00
parent 35e90a6f86
commit ad3fa573b8
11 changed files with 131 additions and 83 deletions
+97 -49
View File
@@ -743,6 +743,7 @@ namespace AdvertsingProfitControl
if (Regex.Replace(projectionsDataGridView.Rows[e.RowIndex].Cells[adItemIndex].EditedFormattedValue.ToString(), @"\s+", "") == "")
{
MessageBox.Show(@"An ad item is required.", @"No Ad Item Specified");
projectionsDataGridView.Rows[e.RowIndex].Cells[adItemIndex].Selected = true;
e.Cancel = true;
}
//Check to see if the user left a row that already exists and doesn't require being copied over.
@@ -809,16 +810,11 @@ namespace AdvertsingProfitControl
rowContents[i] = projectionsDataGridView.Rows[e.RowIndex].Cells[i].EditedFormattedValue.ToString();
}
break;
case (int)SalesTableColumns.IsHeaderRow:
rowContents[i] = projectionsDataGridView.Rows[e.RowIndex].Cells[(int)SalesTableColumns.IsHeaderRow].Value;
break;
case (int)SalesTableColumns.IsMemberRow:
rowContents[i] = projectionsDataGridView.Rows[e.RowIndex].Cells[(int)SalesTableColumns.IsMemberRow].Value;
break;
case (int)SalesTableColumns.IsDirty:
rowContents[i] = true;
break;
default:
if(i >= (int)SalesTableColumns.IsHeaderRow) continue;
rowContents[i] = "";
break;
}
@@ -839,12 +835,6 @@ namespace AdvertsingProfitControl
inventoryNewRow[(int)InventoryTableColumns.AdItem] =
projectionsDataGridView.Rows[e.RowIndex].Cells[i].EditedFormattedValue.ToString();
break;
case (int)SalesTableColumns.IsHeaderRow:
rowContents[(int)InventoryTableColumns.IsHeaderRow] = projectionsDataGridView.Rows[e.RowIndex].Cells[i].Value;
break;
case (int)SalesTableColumns.IsMemberRow:
rowContents[(int)InventoryTableColumns.IsMemberRow] = projectionsDataGridView.Rows[e.RowIndex].Cells[i].Value;
break;
case (int)SalesTableColumns.IsDirty:
inventoryNewRow[(int)InventoryTableColumns.IsDirty] = true;
break;
@@ -1072,23 +1062,24 @@ namespace AdvertsingProfitControl
private void ValidateInventoryRow(object sender, DataGridViewCellCancelEventArgs e)
{
//Grab the index of the ad item, assuming the Sales tables and the Inventory table stay in the same position.
const int adItemIndex = (int)SalesTableColumns.AdItem;
const int adItemIndex = (int)InventoryTableColumns.AdItem;
//Do not even attempt anything since this is a new row and nothing to worry about.
if (inventoryDataGridView.Rows[e.RowIndex].IsNewRow)
{
return;
}
//Clear all whitespace and check for a null value in the ad item column.
if (Regex.Replace(inventoryDataGridView.Rows[e.RowIndex].Cells[adItemIndex].EditedFormattedValue.ToString(), @"\s+", "") == "")
{
MessageBox.Show(@"An ad item is required.", @"No Ad Item Specified");
inventoryDataGridView.Rows[e.RowIndex].Cells[adItemIndex].Selected = true;
e.Cancel = true;
}
//Check to see if the user left a row that already exists and doesn't require being copied over.
if (inventoryDataGridView.Rows[e.RowIndex].Cells[adItemIndex].EditedFormattedValue.ToString() == actualSalesDataGridView.Rows[e.RowIndex].Cells[adItemIndex].EditedFormattedValue.ToString())
{
return;
}
//Clear all whitespace and check for a null value in the ad item column.
if (Regex.Replace(projectionsDataGridView.Rows[e.RowIndex].Cells[adItemIndex].EditedFormattedValue.ToString(), @"\s+", "") == "")
{
MessageBox.Show(@"An ad item is required.", @"No Ad Item Specified");
e.Cancel = true;
}
//Now check to make sure there is an ad item present, otherwise throw an error and block the user from leaving the current row.
if (inventoryDataGridView.Rows[e.RowIndex].Cells[adItemIndex].EditedFormattedValue.ToString() != "")
{
@@ -1124,16 +1115,14 @@ namespace AdvertsingProfitControl
case (int)SalesTableColumns.AdItem:
rowContents[i] = inventoryDataGridView.Rows[e.RowIndex].Cells[(int)InventoryTableColumns.AdItem].EditedFormattedValue.ToString();
break;
case (int)SalesTableColumns.IsHeaderRow:
rowContents[i] = inventoryDataGridView.Rows[e.RowIndex].Cells[(int)InventoryTableColumns.IsHeaderRow].Value;
break;
case (int)SalesTableColumns.IsMemberRow:
rowContents[i] = projectionsDataGridView.Rows[e.RowIndex].Cells[(int)InventoryTableColumns.IsMemberRow].Value;
break;
case (int)SalesTableColumns.IsDirty:
rowContents[i] = true;
break;
default:
if (i >= (int) InventoryTableColumns.IsHeaderRow)
{
continue;
}
rowContents[i] = "";
break;
}
@@ -1144,7 +1133,7 @@ namespace AdvertsingProfitControl
else
{
MessageBox.Show(@"An Ad Item is required.", @"Invalid Ad Item");
projectionsDataGridView.Rows[e.RowIndex].Cells[adItemIndex].Selected = true;
inventoryDataGridView.Rows[e.RowIndex].Cells[adItemIndex].Selected = true;
e.Cancel = true;
}
}
@@ -1231,17 +1220,18 @@ namespace AdvertsingProfitControl
{
return;
}
//Check to see if the user left a row that already exists and doesn't require being copied over.
if (actualSalesDataGridView.Rows[e.RowIndex].Cells[adItemIndex].EditedFormattedValue.ToString() == projectionsDataGridView.Rows[e.RowIndex].Cells[adItemIndex].EditedFormattedValue.ToString())
{
return;
}
//Clear all whitespace and check for a null value in the ad item column.
if (Regex.Replace(actualSalesDataGridView.Rows[e.RowIndex].Cells[adItemIndex].EditedFormattedValue.ToString(), @"\s+", "") == "")
{
MessageBox.Show(@"An ad item is required.", @"No Ad Item Specified");
actualSalesDataGridView.Rows[e.RowIndex].Cells[adItemIndex].Selected = true;
e.Cancel = true;
}
//Check to see if the user left a row that already exists and doesn't require being copied over.
if (actualSalesDataGridView.Rows[e.RowIndex].Cells[adItemIndex].EditedFormattedValue.ToString() == projectionsDataGridView.Rows[e.RowIndex].Cells[adItemIndex].EditedFormattedValue.ToString())
{
return;
}
//Now check to make sure there is an ad item present, otherwise throw an error and block the user from leaving the current row.
if (actualSalesDataGridView.Rows[e.RowIndex].Cells[adItemIndex].EditedFormattedValue.ToString() != "")
{
@@ -1283,16 +1273,11 @@ namespace AdvertsingProfitControl
case (int)SalesTableColumns.Cost:
rowContents[i] = actualSalesDataGridView.Rows[e.RowIndex].Cells[i].EditedFormattedValue.ToString();
break;
case (int)SalesTableColumns.IsHeaderRow:
rowContents[i] = actualSalesDataGridView.Rows[e.RowIndex].Cells[(int) SalesTableColumns.IsHeaderRow].Value;
break;
case (int)SalesTableColumns.IsMemberRow:
rowContents[i] = actualSalesDataGridView.Rows[e.RowIndex].Cells[(int)SalesTableColumns.IsMemberRow].Value;
break;
case (int)SalesTableColumns.IsDirty:
rowContents[i] = true;
break;
default:
if (i >= (int)SalesTableColumns.IsHeaderRow) continue;
rowContents[i] = "";
break;
}
@@ -1310,12 +1295,6 @@ namespace AdvertsingProfitControl
inventoryNewRow[(int)InventoryTableColumns.AdItem] =
actualSalesDataGridView.Rows[e.RowIndex].Cells[i].EditedFormattedValue.ToString();
break;
case (int)SalesTableColumns.IsHeaderRow:
rowContents[(int)InventoryTableColumns.IsHeaderRow] = actualSalesDataGridView.Rows[e.RowIndex].Cells[(int)SalesTableColumns.IsHeaderRow].Value;
break;
case (int)SalesTableColumns.IsMemberRow:
rowContents[(int)InventoryTableColumns.IsMemberRow] = actualSalesDataGridView.Rows[e.RowIndex].Cells[(int)SalesTableColumns.IsMemberRow].Value;
break;
case (int)SalesTableColumns.IsDirty:
inventoryNewRow[(int)InventoryTableColumns.IsDirty] = true;
break;
@@ -1332,7 +1311,7 @@ namespace AdvertsingProfitControl
else
{
MessageBox.Show(@"An Ad Item is required.", @"Invalid Ad Item");
projectionsDataGridView.Rows[e.RowIndex].Cells[adItemIndex].Selected = true;
actualSalesDataGridView.Rows[e.RowIndex].Cells[adItemIndex].Selected = true;
e.Cancel = true;
}
}
@@ -1715,7 +1694,21 @@ namespace AdvertsingProfitControl
DataTable updateTable;
informationLabel.Text = @"Compressing data tables...";
var operationStatus = ConstructCleanedSalesTable("Projections", dateId, out trimmedTable, out updateTable);
ProcessTrimmingStatusResult(projectionsDataGridView, "Projections", operationStatus, trimmedTable, updateTable);
if (operationStatus == TrimmingOperationResult.FailedToTrim)
{
informationLabel.Text = @"Failed to trim the Projections table.";
return;
}
ProcessTrimmingStatusResult(projectionsDataGridView, "Projections", operationStatus, trimmedTable, updateTable);
trimmedTable.Rows.Clear();
updateTable.Rows.Clear();
operationStatus = ConstructCleanedInventoryTable(dateId, out trimmedTable, out updateTable);
if (operationStatus == TrimmingOperationResult.FailedToTrim)
{
informationLabel.Text = @"Failed to trim the Inventory table.";
return;
}
ProcessTrimmingStatusForInventory(operationStatus, trimmedTable, updateTable);
//Create the transaction scope.
//By default the TransactionScopeOption is "Required", so if an ambient transaction does not
//exist then the new transaction that is made (in the first method) becomes the root transaction.
@@ -2237,11 +2230,66 @@ namespace AdvertsingProfitControl
}
informationLabel.Text += Environment.NewLine + @"All operations completed successfully.";
break;
case TrimmingOperationResult.FailedToTrim:
errorLabel.Text = @"Failed to trim " + tableName + @".";
informationLabel.Text = "";
return;
}
dataGridView.RefreshEdit();
}
private void ProcessTrimmingStatusForInventory(TrimmingOperationResult operationStatus, DataTable trimmedTable, DataTable updateTable)
{
//Create the database interaction objects.
var dbT = new DatabaseTracker();
var dbW = new DatabaseWriter(dbT.DatabaseConnectionString);
DbWriterStatus dbWriterStatus;
switch (operationStatus)
{
case TrimmingOperationResult.NoChangesRequired:
informationLabel.Text += Environment.NewLine + @"No changes for the Inventory table detected.";
break;
case TrimmingOperationResult.CreatedNewInsertionTable:
informationLabel.Text += Environment.NewLine + @"Inserting new changes to the Inventory table.";
dbWriterStatus = dbW.InsertIntoInventoryTable(trimmedTable, dbT.DatabaseConnectionString);
//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;
}
informationLabel.Text += Environment.NewLine + @"Inventory table successfully added to the database.";
break;
case TrimmingOperationResult.CreatedUpdateTable:
informationLabel.Text += Environment.NewLine + @"Updating changes made to the Inventory table.";
dbWriterStatus = dbW.UpdateInventoryTable(updateTable, dbT.DatabaseConnectionString);
//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;
}
informationLabel.Text += Environment.NewLine + @"Inventory table successfully updated.";
break;
case TrimmingOperationResult.CreatedNewInsertionAndUpdateTables:
//Insert the new values...
informationLabel.Text += Environment.NewLine + @"Inserting new changes to the Inventory table.";
dbWriterStatus = dbW.InsertIntoInventoryTable(trimmedTable, dbT.DatabaseConnectionString);
//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;
}
//... and update the existing values.
informationLabel.Text += Environment.NewLine + @"Updating changes made to the Inventory table.";
dbWriterStatus = dbW.UpdateInventoryTable(updateTable, dbT.DatabaseConnectionString);
//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;
}
informationLabel.Text += Environment.NewLine + @"All operations completed successfully.";
break;
}
inventoryDataGridView.RefreshEdit();
}
#endregion
}