Finished validation code for the cost analysis, weekly sales and taxable. Fixed a bug with the update flag not being set if an ID number was set. Wrote comment and weekly sales database writing functions; untested.

This commit is contained in:
2016-12-02 23:12:05 -06:00
parent 8dcc290aef
commit 6813bbb341
7 changed files with 315 additions and 25 deletions
+130
View File
@@ -524,6 +524,136 @@ namespace AdvertsingProfitControl
return status;
}
public DbWriterStatus ProcessComments(string comments, int dateId, string connectionString, int id = 0)
{
var status = new DbWriterStatus();
var oleDbConnection = new OleDbConnection(connectionString);
var oleDbCommand = new OleDbCommand
{
Connection = oleDbConnection
};
OleDbTransaction oleDbTransaction = null;
try
{
oleDbConnection.Open();
oleDbTransaction = oleDbConnection.BeginTransaction();
oleDbCommand.Transaction = oleDbTransaction;
if (id == 0)
{
oleDbCommand.CommandText = "INSERT INTO Comment (Comment, FK_DateID) VALUES (?, ?)";
oleDbCommand.Parameters.AddWithValue("Comment", comments);
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
oleDbCommand.ExecuteNonQuery();
//If the execute non query didn't throw an exception then we're good to go.
oleDbTransaction.Commit();
//Now grab the ID number of the comment that was just added.
oleDbCommand.CommandText = "SELECT ID FROM Comment WHERE FK_DateID = ?";
oleDbCommand.Parameters.Clear();
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
//Now read the ID number and add it to the collection with a default key.
var reader = oleDbCommand.ExecuteReader();
while (reader != null && reader.Read())
{
status.AddRowId(0, int.Parse(reader[0].ToString()));
}
}
else
{
oleDbCommand.CommandText = "UPDATE Comment SET Comment = ? WHERE ID = ?";
oleDbCommand.Parameters.AddWithValue("Comment", comments);
oleDbCommand.Parameters.AddWithValue("ID", id);
oleDbCommand.ExecuteNonQuery();
//If the execute non query didn't throw an exception then we're good to go.
oleDbTransaction.Commit();
}
}
catch (OleDbException e)
{
status.SetStatus(WritingOperationStatus.Failed);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to process the comment(s): " + e.Message);
status.SetErrorMessage("Failed to update or insert comments into the database.");
oleDbTransaction?.Rollback();
}
finally
{
oleDbConnection.Close();
}
return status;
}
public DbWriterStatus ProcessWeeklySales(double[] weeklySales, int dateId, string connectionString, int id = 0)
{
var status = new DbWriterStatus();
var oleDbConnection = new OleDbConnection(connectionString);
var oleDbCommand = new OleDbCommand
{
Connection = oleDbConnection
};
OleDbTransaction oleDbTransaction = null;
try
{
oleDbConnection.Open();
oleDbTransaction = oleDbConnection.BeginTransaction();
oleDbCommand.Transaction = oleDbTransaction;
if (id == 0)
{
oleDbCommand.CommandText = "INSERT INTO WeeklySales (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, TotalSales, FK_DateID) VALUES (?,?,?,?,?,?,?,?,?)";
oleDbCommand.Parameters.AddWithValue("Sunday", weeklySales[0]);
oleDbCommand.Parameters.AddWithValue("Monday", weeklySales[1]);
oleDbCommand.Parameters.AddWithValue("Tuesday", weeklySales[2]);
oleDbCommand.Parameters.AddWithValue("Wednesday", weeklySales[3]);
oleDbCommand.Parameters.AddWithValue("Thursday", weeklySales[4]);
oleDbCommand.Parameters.AddWithValue("Friday", weeklySales[5]);
oleDbCommand.Parameters.AddWithValue("Saturday", weeklySales[6]);
oleDbCommand.Parameters.AddWithValue("TotalSales", weeklySales[7]);
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
oleDbCommand.ExecuteNonQuery();
//If the execute non query didn't throw an exception then we're good to go.
oleDbTransaction.Commit();
//Now grab the ID number of the comment that was just added.
oleDbCommand.CommandText = "SELECT ID FROM WeeklySales WHERE FK_DateID = ?";
oleDbCommand.Parameters.Clear();
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
//Now read the ID number and add it to the collection with a default key.
var reader = oleDbCommand.ExecuteReader();
while (reader != null && reader.Read())
{
status.AddRowId(0, int.Parse(reader[0].ToString()));
}
}
else
{
oleDbCommand.CommandText = "UPDATE WeeklySales SET Sunday = ?, Monday = ?, Tuesday = ?, Wednesday = ?, Thursday = ?, Friday = ?, Saturday = ?, TotalSales = ? WHERE ID = ?";
oleDbCommand.Parameters.AddWithValue("Sunday", weeklySales[0]);
oleDbCommand.Parameters.AddWithValue("Monday", weeklySales[1]);
oleDbCommand.Parameters.AddWithValue("Tuesday", weeklySales[2]);
oleDbCommand.Parameters.AddWithValue("Wednesday", weeklySales[3]);
oleDbCommand.Parameters.AddWithValue("Thursday", weeklySales[4]);
oleDbCommand.Parameters.AddWithValue("Friday", weeklySales[5]);
oleDbCommand.Parameters.AddWithValue("Saturday", weeklySales[6]);
oleDbCommand.Parameters.AddWithValue("TotalSales", weeklySales[7]);
oleDbCommand.Parameters.AddWithValue("ID", id);
oleDbCommand.ExecuteNonQuery();
//If the execute non query didn't throw an exception then we're good to go.
oleDbTransaction.Commit();
}
}
catch (OleDbException e)
{
status.SetStatus(WritingOperationStatus.Failed);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to process the weekly sales: " + e.Message);
status.SetErrorMessage("Failed to update or insert weekly sales into the database.");
oleDbTransaction?.Rollback();
}
finally
{
oleDbConnection.Close();
}
return status;
}
/// <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.