Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
Data Redundancy Reduction and Data Integrity Enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
VoxDroid committed Dec 21, 2023
1 parent f50f028 commit 6fa1867
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 38 deletions.
29 changes: 29 additions & 0 deletions SubPages/ModBudMan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ private void modifydata_Click(object sender, EventArgs e)
decimal budget = string.IsNullOrWhiteSpace(budgetText) ? originalBudget : Convert.ToDecimal(budgetText);
decimal remaining = string.IsNullOrWhiteSpace(remainingText) ? originalRemaining : Convert.ToDecimal(remainingText);

if (IsRecordExists(name, category, budget))
{
budgetstatuslabel.ForeColor = Color.Maroon;
budgetstatuslabel.Enabled = true;
budgetstatuslabel.Visible = true;
budgetstatuslabel.Text = "Record with the same name, category, and allocation already exists.";
return;
}

StringBuilder confirmationMessage = new StringBuilder("Are you sure you want to update this record with the following changes?\n\n");

if (!string.IsNullOrWhiteSpace(name) && name != originalName)
Expand Down Expand Up @@ -223,6 +232,26 @@ private void modifydata_Click(object sender, EventArgs e)
}
}

private bool IsRecordExists(string name, string category, decimal allocation)
{
string sqlCheck = "SELECT COUNT(*) FROM budman WHERE name = @name AND category = @category AND allocation = @allocation";

using (MySqlConnection connection = new MySqlConnection(connet))
{
connection.Open();

using (MySqlCommand command = new MySqlCommand(sqlCheck, connection))
{
command.Parameters.AddWithValue("@name", name);
command.Parameters.AddWithValue("@category", category);
command.Parameters.AddWithValue("@allocation", allocation);

int count = Convert.ToInt32(command.ExecuteScalar());

return count > 0;
}
}
}

private void NumbersOnly_KeyPress(object sender, KeyPressEventArgs e)
{
Expand Down
30 changes: 30 additions & 0 deletions SubPages/ModTranLo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ private void modifydata_Click(object sender, EventArgs e)

decimal remaining = string.IsNullOrWhiteSpace(remainingText) ? originalRemaining : Convert.ToDecimal(remainingText);

if (IsRecordExists(name, category, remaining))
{
budgetstatuslabel.ForeColor = Color.Maroon;
budgetstatuslabel.Enabled = true;
budgetstatuslabel.Visible = true;
budgetstatuslabel.Text = "Record with the same name, category, and amount already exists.";
return;
}

StringBuilder confirmationMessage = new StringBuilder("Are you sure you want to update this record with the following changes?\n\n");

if (!string.IsNullOrWhiteSpace(name) && name != originalName)
Expand Down Expand Up @@ -213,6 +222,27 @@ private void modifydata_Click(object sender, EventArgs e)
}
}

private bool IsRecordExists(string name, string category, decimal amount)
{
string sqlCheck = "SELECT COUNT(*) FROM tranlo WHERE name = @name AND category = @category AND amount = @amount";

using (MySqlConnection connection = new MySqlConnection(connet))
{
connection.Open();

using (MySqlCommand command = new MySqlCommand(sqlCheck, connection))
{
command.Parameters.AddWithValue("@name", name);
command.Parameters.AddWithValue("@category", category);
command.Parameters.AddWithValue("@amount", amount);

int count = Convert.ToInt32(command.ExecuteScalar());

return count > 0;
}
}
}


private void NumbersOnly_KeyPress(object sender, KeyPressEventArgs e)
{
Expand Down
99 changes: 64 additions & 35 deletions SubPages/SubBudMan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private void modify_Click(object sender, EventArgs e)
private void createbudget_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(nametb.Text) || string.IsNullOrWhiteSpace(categorytb.Text) ||
string.IsNullOrWhiteSpace(alloctb.Text) || string.IsNullOrWhiteSpace(remtb.Text))
string.IsNullOrWhiteSpace(alloctb.Text) || string.IsNullOrWhiteSpace(remtb.Text))
{
budgetstatuslabel.ForeColor = Color.Maroon;
budgetstatuslabel.Enabled = true;
Expand All @@ -79,50 +79,79 @@ private void createbudget_Click(object sender, EventArgs e)
double budget = Convert.ToDouble(alloctb.Text);
double remaining = Convert.ToDouble(remtb.Text);

string sqlInsert = "INSERT INTO budman (name, category, allocation, remaining) VALUES (@name, @category, @allocation, @remaining)";

try
if (IsRecordExists(name, category, budget))
{
using (MySqlConnection connection = new MySqlConnection(connet))
{
connection.Open();
budgetstatuslabel.ForeColor = Color.Maroon;
budgetstatuslabel.Enabled = true;
budgetstatuslabel.Visible = true;
budgetstatuslabel.Text = "Record with the same name, category, and allocation already exists.";
}
else
{
string sqlInsert = "INSERT INTO budman (name, category, allocation, remaining) VALUES (@name, @category, @allocation, @remaining)";

using (MySqlCommand command = new MySqlCommand(sqlInsert, connection))
try
{
using (MySqlConnection connection = new MySqlConnection(connet))
{
command.Parameters.AddWithValue("@name", name);
command.Parameters.AddWithValue("@category", category);
command.Parameters.AddWithValue("@allocation", budget);
command.Parameters.AddWithValue("@remaining", remaining);

int rowsAffected = command.ExecuteNonQuery();
connection.Open();

if (rowsAffected > 0)
using (MySqlCommand command = new MySqlCommand(sqlInsert, connection))
{
budgetstatuslabel.Enabled = true;
budgetstatuslabel.Visible = true;
budgetstatuslabel.ForeColor = Color.DarkGreen;
budgetstatuslabel.Text = "Record inserted successfully.";
nametb.Clear();
categorytb.Clear();
alloctb.Clear();
remtb.Clear();

LoadRecentCategories();
command.Parameters.AddWithValue("@name", name);
command.Parameters.AddWithValue("@category", category);
command.Parameters.AddWithValue("@allocation", budget);
command.Parameters.AddWithValue("@remaining", remaining);

int rowsAffected = command.ExecuteNonQuery();

if (rowsAffected > 0)
{
budgetstatuslabel.Enabled = true;
budgetstatuslabel.Visible = true;
budgetstatuslabel.ForeColor = Color.DarkGreen;
budgetstatuslabel.Text = "Record inserted successfully.";
nametb.Clear();
categorytb.Clear();
alloctb.Clear();
remtb.Clear();

LoadRecentCategories();
}
else
{
MessageBox.Show("Failed to insert record.");

LoadRecentCategories();
}
}
else
{
MessageBox.Show("Failed to insert record.");

LoadRecentCategories();
}


}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
catch (Exception ex)
}
}

private bool IsRecordExists(string name, string category, double allocation)
{
string sqlCheck = "SELECT COUNT(*) FROM budman WHERE name = @name AND category = @category AND allocation = @allocation";

using (MySqlConnection connection = new MySqlConnection(connet))
{
connection.Open();

using (MySqlCommand command = new MySqlCommand(sqlCheck, connection))
{
MessageBox.Show("Error: " + ex.Message);
command.Parameters.AddWithValue("@name", name);
command.Parameters.AddWithValue("@category", category);
command.Parameters.AddWithValue("@allocation", allocation);

int count = Convert.ToInt32(command.ExecuteScalar());

return count > 0;
}
}
}
Expand Down
36 changes: 33 additions & 3 deletions SubPages/SubTranLo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,20 @@ private void createbudget_Click(object sender, EventArgs e)
}
else
{
string name = nametb.Text;
string description = categorytb.Text;
string category = alloctb.Text;
string name = nametb.Text.Trim();
string description = categorytb.Text.Trim();
string category = alloctb.Text.Trim();
double amount = Convert.ToDouble(remtb.Text);

if (IsRecordExists(name, category, amount))
{
budgetstatuslabel.ForeColor = Color.Maroon;
budgetstatuslabel.Enabled = true;
budgetstatuslabel.Visible = true;
budgetstatuslabel.Text = "Record with the same name, category, and amount already exists.";
return;
}

DateTime currentDate = DateTime.Now;

string sqlInsert = "INSERT INTO tranlo (name, category, description, amount, date) " +
Expand Down Expand Up @@ -126,6 +135,27 @@ private void createbudget_Click(object sender, EventArgs e)
}
}

private bool IsRecordExists(string name, string category, double amount)
{
string sqlCheck = "SELECT COUNT(*) FROM tranlo WHERE name = @name AND category = @category AND amount = @amount";

using (MySqlConnection connection = new MySqlConnection(connet))
{
connection.Open();

using (MySqlCommand command = new MySqlCommand(sqlCheck, connection))
{
command.Parameters.AddWithValue("@name", name);
command.Parameters.AddWithValue("@category", category);
command.Parameters.AddWithValue("@amount", amount);

int count = Convert.ToInt32(command.ExecuteScalar());

return count > 0;
}
}
}

private void NumbersOnly_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
Expand Down

0 comments on commit 6fa1867

Please sign in to comment.