Marked as 'AdvertsingProfitControl No Print' by its folder name. Updated the 'Mordify Record' form. I believe the 'No Print' was related to creating a PDF version of the Advertising Profit Control sheet. The next version after this was most likely to have the code for that.

This commit is contained in:
2021-01-28 19:58:37 -06:00
parent 665f4784e4
commit 75061c9d51
19 changed files with 904 additions and 2171 deletions
+62 -468
View File
@@ -1,321 +1,18 @@
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Transactions;
using System.Windows.Forms;
namespace AdvertsingProfitControl
{
internal class DatabaseWriter
class DatabaseWriter
{
private readonly OleDbConnection _oleDbConnection = new OleDbConnection();
private readonly OleDbConnection _connectionobject = new OleDbConnection();
private readonly FrmLogConsole _logConsole = FrmLogConsole.GetStaticInstance;
public DatabaseWriter(string connectionString)
{
_oleDbConnection.ConnectionString = connectionString;
_connectionobject.ConnectionString = connectionString;
}
#region New Code
/// <summary>
/// Inserts new records into the specified sales table.
/// Supports rolling back the database to prevent corruption.
/// </summary>
/// <param name="salesTable">The data table that contains the data to be inserted.</param>
/// <param name="connectionString"></param>
/// <returns></returns>
public Dictionary<int, int> InsertIntoSalesTable(DataTable salesTable, string connectionString)
{
var rowIndex = 0;
var rows = new Dictionary<int, int>();
var oleDbConnection = new OleDbConnection(connectionString);
//IDs from the database can not be zero (0), so this will be the default value (no group).
var oleDbCommand = new OleDbCommand
{
Connection = oleDbConnection
};
OleDbTransaction oleDbTransaction = null;
try
{
oleDbConnection.Open();
oleDbTransaction = oleDbConnection.BeginTransaction();
oleDbCommand.Transaction = oleDbTransaction;
for (var i = 0; i < salesTable.Rows.Count; i++)
{
oleDbCommand.CommandText =
"INSERT INTO " + salesTable.TableName + " (Sold, SalePrice, TotalSales, Cost, ProfitReturn, TotalProfitReturn, FK_AdItemID, RowAttribute, FK_AdSpecialGroupName, RowPosition, FK_DateID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
//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
oleDbCommand.Parameters.AddWithValue("Sold", salesTable.Rows[i][0]);
oleDbCommand.Parameters.AddWithValue("SalesPrice", salesTable.Rows[i][1]);
oleDbCommand.Parameters.AddWithValue("TotalSales", salesTable.Rows[i][2]);
oleDbCommand.Parameters.AddWithValue("Cost", salesTable.Rows[i][3]);
oleDbCommand.Parameters.AddWithValue("ProfitReturn", salesTable.Rows[i][4]);
oleDbCommand.Parameters.AddWithValue("TotalProfitReturn", salesTable.Rows[i][5]);
oleDbCommand.Parameters.AddWithValue("adItemID", int.Parse(salesTable.Rows[i][6].ToString()));
oleDbCommand.Parameters.AddWithValue("RowAttribute", int.Parse(salesTable.Rows[i][7].ToString()));
oleDbCommand.Parameters.AddWithValue("adSpecialID", int.Parse(salesTable.Rows[i][8].ToString()));
oleDbCommand.Parameters.AddWithValue("RowPosition", int.Parse(salesTable.Rows[i][9].ToString()));
oleDbCommand.Parameters.AddWithValue("dateID", salesTable.Rows[i][10]);
oleDbCommand.ExecuteNonQuery();
oleDbCommand.Parameters.Clear();
rowIndex++;
}
oleDbTransaction.Commit();
foreach (DataRow row in salesTable.Rows)
{
var rowId = RetrieveRowId(salesTable.TableName, int.Parse(row[6].ToString()), int.Parse(row[10].ToString()));
if (int.Parse(row[8].ToString()) != 0)
{
//Account for the ad special row.
rows.Add(int.Parse(row[9].ToString()) + 1, rowId);
}
else
{
rows.Add(int.Parse(row[9].ToString()), rowId);
}
}
}
catch (OleDbException ex)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to write to database: " + ex.Message);
if (rowIndex <= 0)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed to begin parsing data rows, var dump of erroneous row unavailable.");
}
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 + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Ad Item ID: \"" +
salesTable.Rows[rowIndex][6] + "\" Sold: \"" + salesTable.Rows[rowIndex][0] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Sale Price: \"" + salesTable.Rows[rowIndex][1] + "\" Total Sales: \"" + salesTable.Rows[rowIndex][2] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Cost: \"" + salesTable.Rows[rowIndex][3] + "\" Profit Return: \"" + salesTable.Rows[rowIndex][4] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Total Profit Return: \"" +
salesTable.Rows[rowIndex][5] + "\" Ad Special Group: \"" + salesTable.Rows[rowIndex][8] + "\"");
}
oleDbTransaction?.Rollback();
_logConsole.WriteToLog(FrmLogConsole.Level.Critical, "Rollback completed successfully, " + salesTable.TableName+ " table failed on insertion.");
}
finally
{
_oleDbConnection.Close();
}
return rows;
}
public bool UpdateSalesTable(DataTable salesTable, string connectionString)
{
var wasSuccessful = false;
var rowIndex = 0;
var oleDbConnection = new OleDbConnection(connectionString);
//IDs from the database can not be zero (0), so this will be the default value (no group).
var oleDbCommand = new OleDbCommand
{
Connection = oleDbConnection
};
OleDbTransaction oleDbTransaction = null;
try
{
oleDbTransaction = oleDbConnection.BeginTransaction();
_oleDbConnection.Open();
oleDbCommand.Transaction = oleDbTransaction;
for (var i = 0; i < salesTable.Rows.Count; i++)
{
oleDbCommand.CommandText =
"UPDATE " + salesTable.TableName + " SET Sold = ?, SalePrice = ?, TotalSales = ?, Cost = ?, ProfitReturn = ?, TotalProfitReturn = ?, RowAttribute = ?, FK_AdSpecialGroupName = ?, RowPosition = ? WHERE ID = ?";
//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
oleDbCommand.Parameters.AddWithValue("Sold", salesTable.Rows[i][1]);
oleDbCommand.Parameters.AddWithValue("SalesPrice", salesTable.Rows[i][2]);
oleDbCommand.Parameters.AddWithValue("TotalSales", salesTable.Rows[i][3]);
oleDbCommand.Parameters.AddWithValue("Cost", salesTable.Rows[i][4]);
oleDbCommand.Parameters.AddWithValue("ProfitReturn", salesTable.Rows[i][5]);
oleDbCommand.Parameters.AddWithValue("TotalProfitReturn", salesTable.Rows[i][6]);
oleDbCommand.Parameters.AddWithValue("RowAttribute", salesTable.Rows[i][8]);
oleDbCommand.Parameters.AddWithValue("adSpecialID", salesTable.Rows[i][9]);
oleDbCommand.Parameters.AddWithValue("RowPosition", salesTable.Rows[i][10]);
oleDbCommand.Parameters.AddWithValue("ID", salesTable.Rows[i][0]);
oleDbCommand.ExecuteNonQuery();
oleDbCommand.Parameters.Clear();
rowIndex++;
}
oleDbTransaction.Commit();
wasSuccessful = true;
}
catch (OleDbException ex)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to update the database for " + salesTable.TableName + ": " + ex.Message);
if (rowIndex <= 0)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed to begin parsing data rows, var dump of erroneous row unavailable.");
}
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 + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Ad Item ID: \"" +
salesTable.Rows[rowIndex][7] + "\" Sold: \"" + salesTable.Rows[rowIndex][1] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Sale Price: \"" + salesTable.Rows[rowIndex][2] + "\" Total Sales: \"" + salesTable.Rows[rowIndex][3] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Cost: \"" + salesTable.Rows[rowIndex][4] + "\" Profit Return: \"" + salesTable.Rows[rowIndex][5] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Total Profit Return: \"" +
salesTable.Rows[rowIndex][6] + "\" Ad Special Group: \"" + salesTable.Rows[rowIndex][9] + "\"");
}
oleDbTransaction?.Rollback();
_logConsole.WriteToLog(FrmLogConsole.Level.Critical, "Rollback completed successfully, " + salesTable.TableName + " table failed on update.");
}
finally
{
_oleDbConnection.Close();
}
return wasSuccessful;
}
/// <summary>
/// Inserts a new ad item into the database and returns the new item's ID number.
/// Supports rolling back as to not corrupt the database.
/// </summary>
/// <param name="adItemName">The ad item's name that is to be added.</param>
/// <param name="oleDbCommand">A reference to a command that already has a transaction active.</param>
/// <returns>The ID number of the ad item that was just added.</returns>
public int InsertNewAdItem(string adItemName, OleDbCommand oleDbCommand = null)
{
var adItemId = 0;
var needsTransaction = false;
if (oleDbCommand == null)
{
oleDbCommand = new OleDbCommand
{
Connection = _oleDbConnection
};
needsTransaction = true;
}
oleDbCommand.CommandText = "INSERT INTO AdItem (AdItem) VALUES (?)";
oleDbCommand.Parameters.Clear();
oleDbCommand.Parameters.AddWithValue("AdItem", adItemName);
OleDbTransaction oleDbTransaction = null;
try
{
if (needsTransaction)
{
//If the OleDbCommand object is not supplied, then we'll need to create and assign the transaction object.
oleDbCommand.Connection = _oleDbConnection;
_oleDbConnection.Open();
oleDbTransaction = _oleDbConnection.BeginTransaction();
oleDbCommand.Transaction = oleDbTransaction;
}
if (oleDbCommand.ExecuteNonQuery() == 1)
{
oleDbCommand.CommandText = "SELECT ID FROM AdItem WHERE AdItem.AdItem = ?";
oleDbCommand.Parameters.Clear();
oleDbCommand.Parameters.AddWithValue("AdItem", adItemName);
var reader = oleDbCommand.ExecuteReader();
//Get the ID of the new ad item.
while (reader != null && reader.Read())
{
int.TryParse(reader[0].ToString(), out adItemId);
}
reader?.Close();
if (adItemId != 0)
{
//All was successful.
oleDbTransaction?.Commit();
}
else
{
//
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Ad item \"" + adItemName + "\" was not found in the database; rolling back changes.");
oleDbTransaction?.Rollback();
}
}
else
{
//Something failed but wasn't caught.
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed to add the ad item \"" + adItemName + "\" into the database.");
oleDbTransaction?.Rollback();
}
}
catch (OleDbException e)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, e.Message);
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed to insert ad item " + adItemName + " into the database.");
oleDbTransaction?.Rollback();
}
finally
{
if (needsTransaction)
{
_oleDbConnection.Close();
}
}
return adItemId;
}
/// <summary>
/// Infrastructure for the DatabaseWriter class, not meant to be used with external code.
/// Grabs the ID of the specified row.
/// </summary>
/// <param name="tableName">The name of the table to check for the row in.</param>
/// <param name="adItemId">The ID of the ad item in the row.</param>
/// <param name="dateId">The ID of the date in the row.</param>
/// <returns>The ID of the row, zero(0) if no rows are found.</returns>
private int RetrieveRowId(string tableName, int adItemId, int dateId)
{
var id = 0;
var rowsEffected = 0;
var oleDbCommand = new OleDbCommand
{
CommandText = "SELECT ID FROM " + tableName + " WHERE FK_AdItemID = ? AND FK_DateID = ?",
Connection = _oleDbConnection
};
oleDbCommand.Parameters.AddWithValue("AdItemID", adItemId);
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
try
{
_oleDbConnection.Open();
var reader = oleDbCommand.ExecuteReader();
while (reader != null && reader.Read())
{
rowsEffected++;
int.TryParse(reader[0].ToString(), out id);
}
reader?.Close();
if (rowsEffected > 1)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Redundancy found row with the ad item ID " + adItemId + " with the date ID " + dateId + ".");
}
}
catch (OleDbException e)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to look up the existence of a row with the ad item ID " + adItemId + " with the date ID " + dateId + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, e.Message);
}
_oleDbConnection.Close();
return id;
}
#endregion
//10 to 1 ratio of tries to code
//http://codebetter.com/karlseguin/2006/04/05/understanding-and-using-exceptions/nfrastructure
@@ -330,7 +27,7 @@ namespace AdvertsingProfitControl
CommandText = "INSERT INTO WeekEnding(EndOfWeekDate) VALUES ( dateString )"
};
oleDbCommand.Parameters.AddWithValue("dateString", dateString);
oleDbCommand.Connection = _oleDbConnection;
oleDbCommand.Connection = _connectionobject;
//Try to execute the query
try
{
@@ -338,8 +35,8 @@ namespace AdvertsingProfitControl
//Check for the existence of the date string that was provided.
oleDbCommand.CommandText = "SELECT ID FROM WeekEnding WHERE EndOfWeekDate = @dateString";
oleDbCommand.Parameters.AddWithValue("@dateString", dateString);
oleDbCommand.Connection = _oleDbConnection;
_oleDbConnection.Open();
oleDbCommand.Connection = _connectionobject;
_connectionobject.Open();
//Execute the SELECT statement and retrieve the row(s) effected.
var reader = oleDbCommand.ExecuteReader();
var recordIndex = 0;
@@ -351,7 +48,7 @@ namespace AdvertsingProfitControl
}
recordIndex++;
}
reader?.Close();
if (reader != null) reader.Close();
//IF one row was affected, then return with "True", since the value is already there.
if (numberOfRowsEffected == 1)
{
@@ -406,7 +103,7 @@ namespace AdvertsingProfitControl
finally
{
//Make certain that the connection is closed before returning.
_oleDbConnection.Close();
_connectionobject.Close();
}
return insertWasSuccessful;
@@ -418,7 +115,7 @@ namespace AdvertsingProfitControl
var insertWasSuccessful = false;
var storedComment = "";
//Set up the command object, SQL command string and parameters.
var oleDbCommand = new OleDbCommand {Connection = _oleDbConnection};
var oleDbCommand = new OleDbCommand {Connection = _connectionobject};
//Try to execute the query
try
{
@@ -430,7 +127,7 @@ namespace AdvertsingProfitControl
oleDbCommand.CommandText = "SELECT Comment FROM Comment WHERE FK_DateID = dateID";
oleDbCommand.Parameters.Clear();
oleDbCommand.Parameters.AddWithValue("dateID", dateIdString);
_oleDbConnection.Open();
_connectionobject.Open();
//OpenConnection();
var reader = oleDbCommand.ExecuteReader();
var recordIndex = 0;
@@ -449,7 +146,7 @@ namespace AdvertsingProfitControl
recordIndex++;
}
}
reader?.Close();
if (reader != null) reader.Close();
//IF there is a comment that has the same date then check to see if it is the same as the one entered.
if (recordIndex == 1)
{
@@ -538,7 +235,7 @@ namespace AdvertsingProfitControl
finally
{
//Make certain that the connection is closed before returning.
_oleDbConnection.Close();
_connectionobject.Close();
}
return insertWasSuccessful;
@@ -560,7 +257,7 @@ namespace AdvertsingProfitControl
var noRowsAdded = new List<int> {0};
return noRowsAdded;
}
_oleDbConnection.Open();
_connectionobject.Open();
var rowsAdded = new List<int>();
var rowNumber = 0;
//DataTable's structure will reflect the APC database table structure.
@@ -612,7 +309,7 @@ namespace AdvertsingProfitControl
oleDbCommand.Parameters.AddWithValue("AdItemID", adItemId);
oleDbCommand.Parameters.AddWithValue("GroupID", row[19]);
oleDbCommand.Parameters.AddWithValue("dateID", row[20]);
oleDbCommand.Connection = _oleDbConnection;
oleDbCommand.Connection = _connectionobject;
try
{
@@ -631,7 +328,7 @@ namespace AdvertsingProfitControl
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "An error occurred trying to write a record to the APC table.");
_logConsole.WriteToLog(FrmLogConsole.Level.Debug, e.Message);
_oleDbConnection.Close();
_connectionobject.Close();
//Add an error code at the end of array of rows that were added to show an error occurred.
rowsAdded.Add(-1);
return rowsAdded;
@@ -646,7 +343,7 @@ namespace AdvertsingProfitControl
rowsAdded.Add(0);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "An error occurred trying to update a record APC table.");
_logConsole.WriteToLog(FrmLogConsole.Level.Debug, "An error occurred trying to update ad item ID " + adItemId + " and dateID " + dateId + ".");
_oleDbConnection.Close();
_connectionobject.Close();
return rowsAdded;
}
}
@@ -654,7 +351,7 @@ namespace AdvertsingProfitControl
}
//Close the database connection before returning.
_oleDbConnection.Close();
_connectionobject.Close();
return rowsAdded;
}
@@ -673,16 +370,15 @@ namespace AdvertsingProfitControl
oleDbCommand.CommandText = "SELECT ID FROM APC WHERE FK_AdItemID = adItemID AND FK_DateID = dateID";
oleDbCommand.Parameters.AddWithValue("adItemID", adItemId);
oleDbCommand.Parameters.AddWithValue("dateID", dateId);
oleDbCommand.Connection = _oleDbConnection;
oleDbCommand.Connection = _connectionobject;
try
{
oleDbCommand.Transaction = _oleDbConnection.BeginTransaction();
var reader = oleDbCommand.ExecuteReader();
while (reader != null && reader.Read())
{
rowsEffected++;
}
reader?.Close();
if (reader != null) reader.Close();
}
catch (OleDbException ex)
{
@@ -728,12 +424,10 @@ namespace AdvertsingProfitControl
}
//Record the number of rows affected when the statement is executed, again assume nothing was affected.
var adItemId = "0";
var oleDbCommand = new OleDbCommand
{
CommandText = "INSERT INTO AdItem (AdItem) VALUES (?)",
Connection = _oleDbConnection
};
var oleDbCommand = new OleDbCommand();
oleDbCommand.CommandText = "INSERT INTO AdItem (AdItem) VALUES (?)";
oleDbCommand.Parameters.AddWithValue("adItem", adItemName);
oleDbCommand.Connection = _connectionobject;
//Attempt to execute the INSERT SQL statement.
try
{
@@ -749,7 +443,7 @@ namespace AdvertsingProfitControl
{
adItemId = reader[0].ToString();
}
reader?.Close();
if (reader != null) reader.Close();
}
//ELSE IF more then one (1) row was affected...
else if (rowsEffected > 1)
@@ -793,7 +487,7 @@ namespace AdvertsingProfitControl
}
var oleDbCommand = new OleDbCommand {CommandText = "SELECT ID FROM AdItem WHERE UCASE(AdItem.AdItem) = UCASE(adItemName)"};
oleDbCommand.Parameters.AddWithValue("adItemName", adItemName);
oleDbCommand.Connection = _oleDbConnection;
oleDbCommand.Connection = _connectionobject;
try
{
@@ -863,7 +557,7 @@ namespace AdvertsingProfitControl
oleDbCommand.Parameters.AddWithValue("groupID", parameters[19]);
oleDbCommand.Parameters.AddWithValue("dateID", parameters[20]);
oleDbCommand.Parameters.AddWithValue("adItemID", adItemId);
oleDbCommand.Connection = _oleDbConnection;
oleDbCommand.Connection = _connectionobject;
try
{
@@ -903,22 +597,22 @@ namespace AdvertsingProfitControl
var supplierQueryCommand = new OleDbCommand
{
CommandText = "SELECT ID FROM Supplier WHERE SupplierName = ?",
Connection = _oleDbConnection
Connection = _connectionobject
};
//Grabbing the ID of the invoice by the invoice number and the supplier's ID.
var invoiceCheckCommand = new OleDbCommand
{
CommandText = "SELECT ID FROM Invoice WHERE InvoiceNumber = ? AND FK_Supplier = ?",
Connection = _oleDbConnection
Connection = _connectionobject
};
//Inserting a new record into the Invoice table.
var insertNewInvoice = new OleDbCommand
{
CommandText =
"INSERT INTO Invoice (InvoiceDate, InvoiceNumber, InvoiceNetAmountAtCost, InvoiceNetAmount, InvoiceNote, FK_Supplier, FK_DateID) VALUES (?, ?, ?, ?, ?, ?, ?)",
Connection = _oleDbConnection
Connection = _connectionobject
};
_oleDbConnection.Open();
_connectionobject.Open();
foreach (DataRow row in parameters.Rows)
{
var itemCount = 0;
@@ -1042,12 +736,12 @@ namespace AdvertsingProfitControl
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to write supplier " + row[1] + " into the invoice table.");
_logConsole.WriteToLog(FrmLogConsole.Level.Debug, e.Message);
_oleDbConnection.Close();
_connectionobject.Close();
break;
}
}
_oleDbConnection.Close();
_connectionobject.Close();
return rowsAdded;
}
/// <summary>
@@ -1073,7 +767,7 @@ namespace AdvertsingProfitControl
var supplierNameId = "0";
var oleDbCommand = new OleDbCommand {CommandText = "INSERT INTO Supplier (SupplierName) VALUES (?)"};
oleDbCommand.Parameters.AddWithValue("SupplierName", supplierName);
oleDbCommand.Connection = _oleDbConnection;
oleDbCommand.Connection = _connectionobject;
try
{
@@ -1131,7 +825,7 @@ namespace AdvertsingProfitControl
oleDbCommand.Parameters.AddWithValue("InvoiceNumber", invoiceField[2]);
oleDbCommand.Parameters.AddWithValue("SupplierID", supplierId);
oleDbCommand.Parameters.AddWithValue("DateID", invoiceField[6]);
oleDbCommand.Connection = _oleDbConnection;
oleDbCommand.Connection = _connectionobject;
try
{
@@ -1169,7 +863,7 @@ namespace AdvertsingProfitControl
CommandText = "SELECT ID FROM WeeklySales WHERE FK_DateID = ?"
};
weekEndingDateCheck.Parameters.AddWithValue("DateID", parameters[8]);
weekEndingDateCheck.Connection = _oleDbConnection;
weekEndingDateCheck.Connection = _connectionobject;
//Object to update a field with the date supplied, assuming an entry was found with the previous object.
var updateWeeklySales = new OleDbCommand
{
@@ -1185,7 +879,7 @@ namespace AdvertsingProfitControl
updateWeeklySales.Parameters.AddWithValue("Saturday", parameters[6]);
updateWeeklySales.Parameters.AddWithValue("TotalSales", parameters[7]);
updateWeeklySales.Parameters.AddWithValue("DateID", parameters[8]);
updateWeeklySales.Connection = _oleDbConnection;
updateWeeklySales.Connection = _connectionobject;
//Object to insert a new field into the database if no entry is present.
var insertWeeklySales = new OleDbCommand
{
@@ -1201,10 +895,10 @@ namespace AdvertsingProfitControl
insertWeeklySales.Parameters.AddWithValue("Saturday", parameters[6]);
insertWeeklySales.Parameters.AddWithValue("TotalSales", parameters[7]);
insertWeeklySales.Parameters.AddWithValue("DateID", parameters[8]);
insertWeeklySales.Connection = _oleDbConnection;
insertWeeklySales.Connection = _connectionobject;
try
{
_oleDbConnection.Open();
_connectionobject.Open();
//Execute the select statement to check for the presence of an entry with the supplied date (parameters[8]).
var reader = weekEndingDateCheck.ExecuteReader();
@@ -1237,7 +931,7 @@ namespace AdvertsingProfitControl
}
finally
{
_oleDbConnection.Close();
_connectionobject.Close();
}
return rowsEffected;
@@ -1253,18 +947,18 @@ namespace AdvertsingProfitControl
CommandText = "SELECT ID FROM GroupCategory WHERE GroupDescription = ?"
};
categoryDescCheckQueryCommand.Parameters.AddWithValue("GroupName", groupName);
categoryDescCheckQueryCommand.Connection = _oleDbConnection;
categoryDescCheckQueryCommand.Connection = _connectionobject;
//Setting up an object to insert a new ad special keyword.
var insertNewCategoryDesc = new OleDbCommand
{
CommandText = "INSERT INTO GroupCategory (GroupDescription) VALUES (?)"
};
insertNewCategoryDesc.Parameters.AddWithValue("GroupName", groupName);
insertNewCategoryDesc.Connection = _oleDbConnection;
insertNewCategoryDesc.Connection = _connectionobject;
try
{
_oleDbConnection.Open();
_connectionobject.Open();
var reader = categoryDescCheckQueryCommand.ExecuteReader();
while (reader != null && reader.Read())
@@ -1291,7 +985,7 @@ namespace AdvertsingProfitControl
}
finally
{
_oleDbConnection.Close();
_connectionobject.Close();
}
return rowsEffected;
@@ -1305,11 +999,11 @@ namespace AdvertsingProfitControl
CommandText = "DELETE FROM GroupCategory WHERE GroupDescription = ?"
};
oleDbCommand.Parameters.AddWithValue("AdSpecialName", adSpecialName);
oleDbCommand.Connection = _oleDbConnection;
oleDbCommand.Connection = _connectionobject;
try
{
_oleDbConnection.Open();
_connectionobject.Open();
rowsEffected = oleDbCommand.ExecuteNonQuery();
}
catch (OleDbException e)
@@ -1320,7 +1014,7 @@ namespace AdvertsingProfitControl
}
finally
{
_oleDbConnection.Close();
_connectionobject.Close();
}
return rowsEffected;
@@ -1335,11 +1029,11 @@ namespace AdvertsingProfitControl
};
deleteCommand.Parameters.AddWithValue("AdItemID", adItemId);
deleteCommand.Parameters.AddWithValue("DateID", dateId);
deleteCommand.Connection = _oleDbConnection;
deleteCommand.Connection = _connectionobject;
try
{
_oleDbConnection.Open();
_connectionobject.Open();
recordsDeleted = deleteCommand.ExecuteNonQuery();
}
catch (OleDbException e)
@@ -1349,7 +1043,7 @@ namespace AdvertsingProfitControl
}
finally
{
_oleDbConnection.Close();
_connectionobject.Close();
}
return recordsDeleted;
@@ -1364,11 +1058,11 @@ namespace AdvertsingProfitControl
};
oleDbCommand.Parameters.AddWithValue("InvoiceNumber", invoiceNumber);
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
oleDbCommand.Connection = _oleDbConnection;
oleDbCommand.Connection = _connectionobject;
try
{
_oleDbConnection.Open();
_connectionobject.Open();
recordsAffected = oleDbCommand.ExecuteNonQuery();
}
catch (OleDbException e)
@@ -1378,7 +1072,7 @@ namespace AdvertsingProfitControl
}
finally
{
_oleDbConnection.Close();
_connectionobject.Close();
}
return recordsAffected;
@@ -1399,39 +1093,39 @@ namespace AdvertsingProfitControl
CommandText = "DELETE FROM APC WHERE FK_DateID = ?"
};
clearApCommand.Parameters.AddWithValue("DateID", dateId);
clearApCommand.Connection = _oleDbConnection;
clearApCommand.Connection = _connectionobject;
//Object to clear comments associated with the date ID.
var clearCommentsCommand = new OleDbCommand()
{
CommandText = "DELETE FROM Comment WHERE FK_DateID = ?"
};
clearCommentsCommand.Parameters.AddWithValue("DateID", dateId);
clearCommentsCommand.Connection = _oleDbConnection;
clearCommentsCommand.Connection = _connectionobject;
//Object to clear the invoices associated with the date ID.
var clearInvoicesCommand = new OleDbCommand()
{
CommandText = "DELETE FROM Invoice WHERE FK_DateID = ?"
};
clearInvoicesCommand.Parameters.AddWithValue("DateID", dateId);
clearInvoicesCommand.Connection = _oleDbConnection;
clearInvoicesCommand.Connection = _connectionobject;
//Object to clear the weekly sales associated with the date ID.
var clearWeeklySalesCommand = new OleDbCommand()
{
CommandText = "DELETE FROM WeeklySales WHERE FK_DateID = ?"
};
clearWeeklySalesCommand.Parameters.AddWithValue("DateID", dateId);
clearWeeklySalesCommand.Connection = _oleDbConnection;
clearWeeklySalesCommand.Connection = _connectionobject;
//Object to clear the date associated with the ID passed in.
var clearEndOfWeekDateCommand = new OleDbCommand()
{
CommandText = "DELETE FROM WeekEnding WHERE ID = ?"
};
clearEndOfWeekDateCommand.Parameters.AddWithValue("DateID", dateId);
clearEndOfWeekDateCommand.Connection = _oleDbConnection;
clearEndOfWeekDateCommand.Connection = _connectionobject;
try
{
_oleDbConnection.Open();
_connectionobject.Open();
recordsRemoved += clearApCommand.ExecuteNonQuery();
recordsRemoved += clearCommentsCommand.ExecuteNonQuery();
recordsRemoved += clearInvoicesCommand.ExecuteNonQuery();
@@ -1445,7 +1139,7 @@ namespace AdvertsingProfitControl
}
finally
{
_oleDbConnection.Close();
_connectionobject.Close();
}
return recordsRemoved;
}
@@ -1467,12 +1161,12 @@ namespace AdvertsingProfitControl
};
oleDbCommand.Parameters.AddWithValue("Comment", comment);
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
oleDbCommand.Connection = _oleDbConnection;
oleDbCommand.Connection = _connectionobject;
try
{
//IF the delete command deletes more then one row it returns 0.
_oleDbConnection.Open();
_connectionobject.Open();
var recordsEffected = oleDbCommand.ExecuteNonQuery();
if (recordsEffected == 1)
{
@@ -1486,110 +1180,10 @@ namespace AdvertsingProfitControl
}
finally
{
_oleDbConnection.Close();
_connectionobject.Close();
}
return wasSuccessful;
}
public bool RemoveAdItem(string adItemName)
{
var success = false;
var oleDbCommand = new OleDbCommand
{
CommandText = "DELETE FROM AdItem WHERE AdItem = ?"
};
oleDbCommand.Parameters.AddWithValue("AdItemName", adItemName);
oleDbCommand.Connection = _oleDbConnection;
try
{
_oleDbConnection.Open();
var rowsEffected = oleDbCommand.ExecuteNonQuery();
if(rowsEffected == 1)
{
success = true;
}
else if(rowsEffected > 1)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Info, "Removed redundant entry " + adItemName + " from the database successfully.");
success = true;
}
else
{
}
}
catch (OleDbException e)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to delete the ad item " + adItemName + " from the database.");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, e.Message);
}
finally
{
_oleDbConnection.Close();
}
return success;
}
public bool AddNewItem(string adItemName)
{
var success = false;
var oleDbCommand = new OleDbCommand
{
CommandText = "INSERT INTO AdItem (AdItem) VALUES (?)"
};
oleDbCommand.Parameters.AddWithValue("AdItemName", adItemName);
oleDbCommand.Connection = _oleDbConnection;
var redundantancyCheck = new OleDbCommand
{
CommandText = "SELECT AdItem FROM AdItem WHERE AdItem = ?"
};
redundantancyCheck.Parameters.AddWithValue("AdItemName", adItemName);
redundantancyCheck.Connection = _oleDbConnection;
try
{
_oleDbConnection.Open();
using(var reader = redundantancyCheck.ExecuteReader())
{
var rows = 0;
while(reader != null && reader.Read())
{
rows++;
}
if(rows == 0)
{
var rowsEffected = oleDbCommand.ExecuteNonQuery();
if (rowsEffected == 1)
{
success = true;
}
else
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to insert new ad item " + adItemName + ".");
}
}
else
{
success = true;
_logConsole.WriteToLog(FrmLogConsole.Level.Debug, "Ad Item " + adItemName + " found with " + rows + " row(s).");
}
}
}
catch (OleDbException e)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to add the new ad item " + adItemName + " to the database.");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, e.Message);
}
finally
{
_oleDbConnection.Close();
}
return success;
}
}
}