public void UpdateGrid() { int brojac = -1; //Counter used to count the id of the datagridview field LoginForm fasdorm1 = new LoginForm(); masterusername = fasdorm1.Getuser(); masterpassword = fasdorm1.Getpassword(); //Get the username and password of the user dataGridView1.Rows.Clear(); //Clear all the rows SQLiteConnection dbConnection; dbConnection = new SQLiteConnection("Data Source=" + Fajl() + ";Version=3;"); try { //Connect to the file dbConnection.Open(); int idbroj = 1; //promenliva za skladiranje na id brojot od posledniot rekord string maxid = "SELECT MAX(ID) FROM passwords;"; //komanda za selektiranje na posledniot rekord string sql = "SELECT * FROM passwords ORDER BY id "; SQLiteCommand maxidkomanda = new SQLiteCommand(maxid, dbConnection); SQLiteDataReader reader1 = maxidkomanda.ExecuteReader(); while (reader1.Read()) { idbroj = reader1.GetInt32(0); break; //Get the highest ID number from the records, as in the last record entered } reader1.Close(); SQLiteCommand command = new SQLiteCommand(sql, dbConnection); SQLiteDataReader reader = command.ExecuteReader(); while (reader.Read()) { // if (int.Parse(reader["id"].ToString()) <= idbroj ) // { if (reader["id"].ToString() != "1") //If the record's ID isn't 1, because the first record is used to store the encrypted password { if (reader["visible"].ToString() == "1") //if the record's visible field is set to 1, because the "deleted" record's IDs are set to 0 { brojac++; //Increment the counter (move to the next row of dataGridView) dbid = reader["id"].ToString(); dburl = reader["URL"].ToString(); dbname = reader["name"].ToString(); dbusername = reader["username"].ToString(); dbpassword = reader["password"].ToString(); dbnotes = reader["notes"].ToString(); //Get the record's data and turn it into strings dataGridView1.Rows.Add(); dataGridView1.Rows[brojac].Cells[0].Value = (int.Parse(dbid)).ToString(); dataGridView1.Rows[brojac].Cells[1].Value = Cryptography.Decrypt(dburl, masterpassword); dataGridView1.Rows[brojac].Cells[2].Value = Cryptography.Decrypt(dbname, masterpassword); dataGridView1.Rows[brojac].Cells[3].Value = Cryptography.Decrypt(dbusername, masterpassword); dataGridView1.Rows[brojac].Cells[4].Value = Cryptography.Decrypt(dbpassword, masterpassword); dataGridView1.Rows[brojac].Cells[5].Value = Cryptography.Decrypt(dbnotes, masterpassword); //Input the record's data into the dataGridView table after it gets decrypted with the password gotten from the login form } } } dbConnection.Dispose(); dbConnection.Close(); reader.Close(); //Close the connection } catch (Exception ex) { // MessageBox.Show(ex.Message); } }
private void Button3_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count > 0) { if (dataGridView1.SelectedRows.Count < 2) { //if only one row has been selected from the dataGridView table int selectedid = int.Parse(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["id"].Value.ToString()); //Get the selected row's ID from the hidden ID column SQLiteConnection dbConnection; dbConnection = new SQLiteConnection("Data Source=" + Fajl() + ";Version=3;"); try { //Connect to the file dbConnection.Open(); string sql = "SELECT * FROM passwords WHERE ID=" + selectedid + ";"; //Select the record with the ID gotten from before SQLiteCommand command = new SQLiteCommand(sql, dbConnection); SQLiteDataReader reader = command.ExecuteReader(); while (reader.Read()) { textBox1.Text = Cryptography.Decrypt(reader["URL"].ToString(), masterpassword); textBox2.Text = Cryptography.Decrypt(reader["name"].ToString(), masterpassword); textBox3.Text = Cryptography.Decrypt(reader["username"].ToString(), masterpassword); textBox4.Text = Cryptography.Decrypt(reader["password"].ToString(), masterpassword); textBox5.Text = Cryptography.Decrypt(reader["notes"].ToString(), masterpassword); } //Fill the textboxes with the decrypted values of the record } catch (Exception ex) { MessageBox.Show(ex.Message); } //Make the textfields visible #region visible label1.Visible = true; label2.Visible = true; label3.Visible = true; label4.Visible = true; label5.Visible = true; textBox1.Visible = true; textBox2.Visible = true; textBox3.Visible = true; textBox4.Visible = true; textBox5.Visible = true; button4.Visible = true; button5.Visible = true; #endregion } } else { //Make the text fields invisible #region invisible label1.Visible = false; label2.Visible = false; label3.Visible = false; label4.Visible = false; label5.Visible = false; textBox1.Visible = false; textBox2.Visible = false; textBox3.Visible = false; textBox4.Visible = false; textBox5.Visible = false; button4.Visible = false; button5.Visible = false; #endregion } }