Fixed a bug where the row row would get reported as the last row processed in all the database insert and update methods. Fixed a bug where the NewRow would get set as dirty if an ad special row was renamed with no members in it. Sales table compression and database methods tested and working.

This commit is contained in:
2016-11-11 12:43:47 -06:00
parent d0c6ce5e42
commit 9105429fcb
13 changed files with 321 additions and 584 deletions
+80 -51
View File
@@ -15,13 +15,6 @@ namespace AdvertsingProfitControl
#region New Code
public OleDbCommand GetOleDbCommand(string connectionString)
{
var oleDbCommand = new OleDbCommand();
return oleDbCommand;
}
/// <summary>
/// Inserts new records into the specified sales table.
/// Supports rolling back the database to prevent corruption.
@@ -31,7 +24,7 @@ namespace AdvertsingProfitControl
/// <returns>A DbWriterStatus object that contains a status, rows added and an error message if necessary.</returns>
public DbWriterStatus InsertIntoSalesTable(DataTable salesTable, string connectionString)
{
var rowIndex = 0;
var lastRowProcessed = 0;
var writerStatus = new DbWriterStatus();
var oleDbConnection = new OleDbConnection(connectionString);
var oleDbCommand = new OleDbCommand
@@ -65,7 +58,8 @@ namespace AdvertsingProfitControl
oleDbCommand.Parameters.AddWithValue("dateID", salesTable.Rows[i][10]);
oleDbCommand.ExecuteNonQuery();
oleDbCommand.Parameters.Clear();
rowIndex++;
//Row index is only used to keep track of what row failed to update.
lastRowProcessed = int.Parse(salesTable.Rows[i][9].ToString()) - 1;
}
oleDbTransaction.Commit();
foreach (DataRow row in salesTable.Rows)
@@ -92,7 +86,7 @@ namespace AdvertsingProfitControl
{
writerStatus.SetStatus(WritingOperationStatus.Failed);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to write to database: " + ex.Message);
if (rowIndex <= 0)
if (lastRowProcessed <= 0)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed to begin parsing data rows, var dump of erroneous row unavailable.");
@@ -101,25 +95,25 @@ namespace AdvertsingProfitControl
else
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed on row " + (rowIndex + 1) + " due to the above error. Dumping contents of row " + (rowIndex + 1) + " from " + salesTable.TableName + ".");
"Failed on row " + (lastRowProcessed + 1) + " due to the above error. Dumping contents of row " + (lastRowProcessed + 1) + " from " + salesTable.TableName + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Ad Item ID: \"" +
salesTable.Rows[rowIndex][6] + "\" Sold: \"" + salesTable.Rows[rowIndex][0] + "\"");
salesTable.Rows[lastRowProcessed][6] + "\" Sold: \"" + salesTable.Rows[lastRowProcessed][0] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Sale Price: \"" + salesTable.Rows[rowIndex][1] + "\" Total Sales: \"" + salesTable.Rows[rowIndex][2] + "\"");
"Sale Price: \"" + salesTable.Rows[lastRowProcessed][1] + "\" Total Sales: \"" + salesTable.Rows[lastRowProcessed][2] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Cost: \"" + salesTable.Rows[rowIndex][3] + "\" Profit Return: \"" + salesTable.Rows[rowIndex][4] + "\"");
"Cost: \"" + salesTable.Rows[lastRowProcessed][3] + "\" Profit Return: \"" + salesTable.Rows[lastRowProcessed][4] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Total Profit Return: \"" +
salesTable.Rows[rowIndex][5] + "\" Ad Special Group: \"" + salesTable.Rows[rowIndex][8] + "\"");
writerStatus.SetErrorMessage("Failed to write to the database on row " + (rowIndex + 1) + ".");
salesTable.Rows[lastRowProcessed][5] + "\" Ad Special Group: \"" + salesTable.Rows[lastRowProcessed][8] + "\"");
writerStatus.SetErrorMessage("Failed to write to the database on row " + (lastRowProcessed + 1) + ".");
}
oleDbTransaction?.Rollback();
_logConsole.WriteToLog(FrmLogConsole.Level.Critical, "Rollback completed successfully, " + salesTable.TableName+ " table failed on insertion.");
}
finally
{
_oleDbConnection.Close();
oleDbConnection.Close();
}
return writerStatus;
}
@@ -134,7 +128,7 @@ namespace AdvertsingProfitControl
public DbWriterStatus UpdateSalesTable(DataTable salesTable, string connectionString)
{
var updateStatus = new DbWriterStatus();
var rowIndex = 0;
var lastRowProccessed = 0;
var oleDbConnection = new OleDbConnection(connectionString);
var oleDbCommand = new OleDbCommand
{
@@ -142,9 +136,9 @@ namespace AdvertsingProfitControl
};
OleDbTransaction oleDbTransaction = null;
try
{
{
oleDbConnection.Open();
oleDbTransaction = oleDbConnection.BeginTransaction();
_oleDbConnection.Open();
oleDbCommand.Transaction = oleDbTransaction;
for (var i = 0; i < salesTable.Rows.Count; i++)
{
@@ -168,16 +162,33 @@ namespace AdvertsingProfitControl
oleDbCommand.ExecuteNonQuery();
oleDbCommand.Parameters.Clear();
//Row index is only used to keep track of what row failed to update.
rowIndex++;
lastRowProccessed = int.Parse(salesTable.Rows[i][10].ToString()) - 1;
}
oleDbTransaction.Commit();
foreach (DataRow row in salesTable.Rows)
{
if (int.Parse(row[9].ToString()) != 0)
{
//Account for the ad special row.
//Index 10 of the row indicates that this row is an ad special member row
//so we must add one to the row position to offset the fact that a row
//is "missing" from this DataTable.
//Row[10] is its row position, which is not zero index based.
updateStatus.AddRowId(int.Parse(row[10].ToString()) + 1, int.Parse(row[0].ToString()));
}
else
{
//The ad special row doesn't exist so no need to offset it.
updateStatus.AddRowId(int.Parse(row[10].ToString()), int.Parse(row[0].ToString()));
}
}
updateStatus.SetStatus(WritingOperationStatus.UpdateSuccessful);
}
catch (OleDbException ex)
{
updateStatus.SetStatus(WritingOperationStatus.Failed);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to update the database for " + salesTable.TableName + ": " + ex.Message);
if (rowIndex <= 0)
if (lastRowProccessed <= 0)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed to begin parsing data rows, var dump of erroneous row unavailable.");
@@ -186,25 +197,25 @@ namespace AdvertsingProfitControl
else
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed on row " + (rowIndex + 1) + " due to the above error. Dumping contents of row " + (rowIndex + 1) + " from " + salesTable.TableName + ".");
"Failed on row " + (lastRowProccessed + 1) + " due to the above error. Dumping contents of row " + (lastRowProccessed + 1) + " from " + salesTable.TableName + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Ad Item ID: \"" +
salesTable.Rows[rowIndex][7] + "\" Sold: \"" + salesTable.Rows[rowIndex][1] + "\"");
salesTable.Rows[lastRowProccessed][7] + "\" Sold: \"" + salesTable.Rows[lastRowProccessed][1] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Sale Price: \"" + salesTable.Rows[rowIndex][2] + "\" Total Sales: \"" + salesTable.Rows[rowIndex][3] + "\"");
"Sale Price: \"" + salesTable.Rows[lastRowProccessed][2] + "\" Total Sales: \"" + salesTable.Rows[lastRowProccessed][3] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Cost: \"" + salesTable.Rows[rowIndex][4] + "\" Profit Return: \"" + salesTable.Rows[rowIndex][5] + "\"");
"Cost: \"" + salesTable.Rows[lastRowProccessed][4] + "\" Profit Return: \"" + salesTable.Rows[lastRowProccessed][5] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Total Profit Return: \"" +
salesTable.Rows[rowIndex][6] + "\" Ad Special Group: \"" + salesTable.Rows[rowIndex][9] + "\"");
updateStatus.SetErrorMessage("Failed to write to the database on row " + (rowIndex + 1) + ".");
salesTable.Rows[lastRowProccessed][6] + "\" Ad Special Group: \"" + salesTable.Rows[lastRowProccessed][9] + "\"");
updateStatus.SetErrorMessage("Failed to write to the database on row " + (lastRowProccessed + 1) + ".");
}
oleDbTransaction?.Rollback();
_logConsole.WriteToLog(FrmLogConsole.Level.Critical, "Rollback completed successfully, " + salesTable.TableName + " table failed on update.");
}
finally
{
_oleDbConnection.Close();
oleDbConnection.Close();
}
return updateStatus;
@@ -219,7 +230,7 @@ namespace AdvertsingProfitControl
public DbWriterStatus InsertIntoInventoryTable(DataTable table, string connectionString)
{
var status = new DbWriterStatus();
var rowIndex = 0;
var lastRowProcessed = 0;
var oleDbConnection = new OleDbConnection(connectionString);
var oleDbCommand = new OleDbCommand
{
@@ -228,8 +239,8 @@ namespace AdvertsingProfitControl
OleDbTransaction oleDbTransaction = null;
try
{
oleDbConnection.Open();
oleDbTransaction = oleDbConnection.BeginTransaction();
_oleDbConnection.Open();
oleDbCommand.Transaction = oleDbTransaction;
for (var i = 0; i < table.Rows.Count; i++)
{
@@ -250,7 +261,7 @@ namespace AdvertsingProfitControl
oleDbCommand.ExecuteNonQuery();
oleDbCommand.Parameters.Clear();
//Row index is only used to keep track of what row failed to update.
rowIndex++;
lastRowProcessed = int.Parse(table.Rows[i][7].ToString()) - 1;
}
oleDbTransaction.Commit();
foreach (DataRow row in table.Rows)
@@ -277,7 +288,7 @@ namespace AdvertsingProfitControl
{
status.SetStatus(WritingOperationStatus.Failed);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to update the database for " + table.TableName + ": " + e.Message);
if (rowIndex <= 0)
if (lastRowProcessed <= 0)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed to begin parsing data rows, var dump of erroneous row unavailable.");
@@ -285,19 +296,19 @@ namespace AdvertsingProfitControl
}
else
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed on row " + (rowIndex + 1) + " due to the above error. Dumping contents of row " + (rowIndex + 1) + " from " + table.TableName + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Ad Item ID: \"" + table.Rows[rowIndex][4] + "\" Beginning Inventory: \"" + table.Rows[rowIndex][0]);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Received: \"" + table.Rows[rowIndex][1] + "\" Total Inventory: \"" + table.Rows[rowIndex][2] + "\" Ending Inventory: \"" + table.Rows[rowIndex][3] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Row Attribute: \"" + table.Rows[rowIndex][5] + "\" Ad Special ID: \"" + table.Rows[rowIndex][7] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Date ID: \"" + table.Rows[rowIndex][8] + "\"");
status.SetErrorMessage("Failed to write to the database on row " + (rowIndex + 1) + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed on row " + (lastRowProcessed + 1) + " due to the above error. Dumping contents of row " + (lastRowProcessed + 1) + " from " + table.TableName + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Ad Item ID: \"" + table.Rows[lastRowProcessed][4] + "\" Beginning Inventory: \"" + table.Rows[lastRowProcessed][0]);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Received: \"" + table.Rows[lastRowProcessed][1] + "\" Total Inventory: \"" + table.Rows[lastRowProcessed][2] + "\" Ending Inventory: \"" + table.Rows[lastRowProcessed][3] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Row Attribute: \"" + table.Rows[lastRowProcessed][5] + "\" Ad Special ID: \"" + table.Rows[lastRowProcessed][7] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Date ID: \"" + table.Rows[lastRowProcessed][8] + "\"");
status.SetErrorMessage("Failed to write to the database on row " + (lastRowProcessed + 1) + ".");
}
oleDbTransaction?.Rollback();
_logConsole.WriteToLog(FrmLogConsole.Level.Critical, "Rollback completed successfully, " + table.TableName + " table failed on update.");
}
finally
{
_oleDbConnection.Close();
oleDbConnection.Close();
}
return status;
@@ -312,7 +323,7 @@ namespace AdvertsingProfitControl
public DbWriterStatus UpdateInventoryTable(DataTable table, string connectionString)
{
var status = new DbWriterStatus();
var rowIndex = 0;
var lastRowProcessed = 0;
var oleDbConnection = new OleDbConnection(connectionString);
var oleDbCommand = new OleDbCommand
{
@@ -321,8 +332,8 @@ namespace AdvertsingProfitControl
OleDbTransaction oleDbTransaction = null;
try
{
oleDbConnection.Open();
oleDbTransaction = oleDbConnection.BeginTransaction();
_oleDbConnection.Open();
oleDbCommand.Transaction = oleDbTransaction;
for (var i = 0; i < table.Rows.Count; i++)
{
@@ -342,16 +353,34 @@ namespace AdvertsingProfitControl
oleDbCommand.ExecuteNonQuery();
oleDbCommand.Parameters.Clear();
//Row index is only used to keep track of what row failed to update.
rowIndex++;
lastRowProcessed = int.Parse(table.Rows[i][8].ToString()) - 1;
}
oleDbTransaction.Commit();
foreach (DataRow row in table.Rows)
{
var rowId = RetrieveRowId(table.TableName, int.Parse(row[4].ToString()), int.Parse(row[8].ToString()));
if (int.Parse(row[7].ToString()) != 0)
{
//Account for the ad special row.
//Index 8 of the row indicates that this row is an ad special member row
//so we must add one to the row position to offset the fact that a row
//is "missing" from this DataTable.
//Row[8] is its row position, which is not zero index based.
status.AddRowId(int.Parse(row[8].ToString()) + 1, rowId);
}
else
{
//The ad special row doesn't exist so no need to offset it.
status.AddRowId(int.Parse(row[8].ToString()), rowId);
}
}
status.SetStatus(WritingOperationStatus.InsertionSuccessful);
}
catch (OleDbException e)
{
status.SetStatus(WritingOperationStatus.Failed);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to update the database for " + table.TableName + ": " + e.Message);
if (rowIndex <= 0)
if (lastRowProcessed <= 0)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed to begin parsing data rows, var dump of erroneous row unavailable.");
@@ -359,19 +388,19 @@ namespace AdvertsingProfitControl
}
else
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed on row " + (rowIndex + 1) + " due to the above error. Dumping contents of row " + (rowIndex + 1) + " from " + table.TableName + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Ad Item ID: \"" + table.Rows[rowIndex][4] + "\" Beginning Inventory: \"" + table.Rows[rowIndex][0]);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Received: \"" + table.Rows[rowIndex][1] + "\" Total Inventory: \"" + table.Rows[rowIndex][2] + "\" Ending Inventory: \"" + table.Rows[rowIndex][3] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Row Attribute: \"" + table.Rows[rowIndex][5] + "\" Ad Special ID: \"" + table.Rows[rowIndex][7] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Date ID: \"" + table.Rows[rowIndex][8] + "\"");
status.SetErrorMessage("Failed to write to the database on row " + (rowIndex + 1) + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed on row " + (lastRowProcessed + 1) + " due to the above error. Dumping contents of row " + (lastRowProcessed + 1) + " from " + table.TableName + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Ad Item ID: \"" + table.Rows[lastRowProcessed][4] + "\" Beginning Inventory: \"" + table.Rows[lastRowProcessed][0]);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Received: \"" + table.Rows[lastRowProcessed][1] + "\" Total Inventory: \"" + table.Rows[lastRowProcessed][2] + "\" Ending Inventory: \"" + table.Rows[lastRowProcessed][3] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Row Attribute: \"" + table.Rows[lastRowProcessed][5] + "\" Ad Special ID: \"" + table.Rows[lastRowProcessed][7] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Date ID: \"" + table.Rows[lastRowProcessed][8] + "\"");
status.SetErrorMessage("Failed to write to the database on row " + (lastRowProcessed + 1) + ".");
}
oleDbTransaction?.Rollback();
_logConsole.WriteToLog(FrmLogConsole.Level.Critical, "Rollback completed successfully, " + table.TableName + " table failed on update.");
}
finally
{
_oleDbConnection.Close();
oleDbConnection.Close();
}
return status;