/* * Modifys a record from the database and its corresponding ListViewItem */ private void btnEditRecord_Click(object sender, EventArgs e) { if (lstDatabase.SelectedItems.Count > 0) { MwlItemDialog itemDlg = new MwlItemDialog(false); if (itemDlg.ShowDialog(this) == DialogResult.OK) { try { // Update the item in the database SQLiteConnection SqlConn = new SQLiteConnection(); SqlConn.ConnectionString = String.Format("Data Source={0};New=False;Version=3", m_strDBFileName); SqlConn.Open(); SQLiteCommand cmd = SqlConn.CreateCommand(); cmd.CommandText = itemDlg.m_strSqlQuery; cmd.ExecuteNonQuery(); SqlConn.Close(); // Update the ListViewItem lstDatabase.Items[lstDatabase.SelectedIndices[0]] = new ListViewItem(itemDlg.m_strItemValues); } catch (Exception ex) { MessageBox.Show("Error modifying record in database:\r\n\r\n" + ex.ToString()); } } } else { MessageBox.Show("Please select an item to edit"); } }
/* * Adds a record to the database and its corresponding ListViewItem */ private void btnAddRecord_Click(object sender, EventArgs e) { MwlItemDialog itemDlg = new MwlItemDialog(true); if (itemDlg.ShowDialog(this) == DialogResult.OK) { try { // Add the new item into the database SQLiteConnection SqlConn = new SQLiteConnection(); SqlConn.ConnectionString = String.Format("Data Source={0};New=False;Version=3", m_strDBFileName); SqlConn.Open(); SQLiteCommand cmd = SqlConn.CreateCommand(); cmd.CommandText = itemDlg.m_strSqlQuery; cmd.ExecuteNonQuery(); // Get the Item_ID of the record we just added and update the string array for creating the ListViewItem cmd = SqlConn.CreateCommand(); cmd.CommandText = "SELECT last_insert_rowid()"; itemDlg.m_strItemValues[23] = Convert.ToString(cmd.ExecuteScalar()); SqlConn.Close(); // Add the new ListViewItem lstDatabase.Items.Add(new ListViewItem(itemDlg.m_strItemValues)); } catch (Exception ex) { MessageBox.Show("Error adding record to database:\r\n\r\n" + ex.ToString()); } } }