Reputation: 693
Ok, My counting does work somewhat, meaning that it display correct number when data is added and deleted.
The problem is that if I add only 1 entry, I am displayed that I have 1 entry, but if I delete the only entry there is, my counter is still showing 1. Why not 0?
This is how I do it (the fields where I am updating my panel titles are on the bottom):
public void DisplayFileContent(string filePath)
{
lvPC.Items.Clear();
lvWeb.Items.Clear();
lvSerialCode.Items.Clear();
string hashShortPass = hash.ShortHash(storedAuth.Password);
// Counting all entries.
int countEntries = 0;
int countPC = 0;
int countWeb = 0;
int countSerial = 0;
string connectionString =
@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0};
Persist Security Info=False; Jet OLEDB:Database Password={1};";
// Encrypting/Decrypting data.
Security security = new Security();
using (OleDbConnection connection = new OleDbConnection())
{
connection.ConnectionString = string.Format(connectionString, filePath, hashShortPass);
using (OleDbCommand command = new OleDbCommand
("Select * FROM PersonalData", connection))
{
try
{
// Open database connection.
connection.Open();
using (OleDbDataReader read = command.ExecuteReader())
{
// Checking if there is any data in the file.
if (read.HasRows)
{
// Reading information from the file.
while (read.Read())
{
// Count all entries read from the reader.
countEntries++;
if (security.DecryptAES(read.GetString(1),
storedAuth.Password, storedAuth.UserName)
== "PC Password")
{
// Count PC passwords.
countPC++;
PC.Tag = read.GetInt32(0);
lvPC.Items.Add(PC);
}
else if (security.DecryptAES(read.GetString(1),
storedAuth.Password, storedAuth.UserName)
== "Web Site Password")
{
countWeb++;
Web.Tag = read.GetInt32(0);
lvWeb.Items.Add(Web);
}
else if (security.DecryptAES(read.GetString(1),
storedAuth.Password, storedAuth.UserName)
== "Serial Code")
{
countSerial++;
Serial.Tag = read.GetInt32(0);
}
tabPage1.Text = "PC Passwords ( " + countPC + " )";
tabPage2.Text = "Web Site Passwords ( " + countWeb + " )";
tabPage3.Text = "Serial Codes ( " + countSerial + " )";
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
}
Upvotes: 0
Views: 110
Reputation: 44971
The problem is that if you delete an entry, the code that updates the labels is not getting executed.
You need to move the following block:
tabPage1.Text = "PC Passwords ( " + countPC + " )";
tabPage2.Text = "Web Site Passwords ( " + countWeb + " )";
tabPage3.Text = "Serial Codes ( " + countSerial + " )";
after the }
associated with this statement:
if (read.HasRows)
Also, since the HasRows is not needed, you can rewrite it something like:
using (OleDbDataReader read = command.ExecuteReader())
{
// Reading information from the file.
while (read.Read())
{
// Count all entries read from the reader.
countEntries++;
if (security.DecryptAES(read.GetString(1),
storedAuth.Password, storedAuth.UserName)
== "PC Password")
{
// Count PC passwords.
countPC++;
PC.Tag = read.GetInt32(0);
lvPC.Items.Add(PC);
}
else if (security.DecryptAES(read.GetString(1),
storedAuth.Password, storedAuth.UserName)
== "Web Site Password")
{
countWeb++;
Web.Tag = read.GetInt32(0);
lvWeb.Items.Add(Web);
}
else if (security.DecryptAES(read.GetString(1),
storedAuth.Password, storedAuth.UserName)
== "Serial Code")
{
countSerial++;
Serial.Tag = read.GetInt32(0);
}
}
}
tabPage1.Text = "PC Passwords ( " + countPC + " )";
tabPage2.Text = "Web Site Passwords ( " + countWeb + " )";
tabPage3.Text = "Serial Codes ( " + countSerial + " )";
Upvotes: 2