private void Import_Click(object sender, EventArgs e) { // 2. import both CC activity files (file select dialog) if (openFileDialog1.ShowDialog() == DialogResult.OK) { StreamReader openFile = new StreamReader(openFileDialog1.OpenFile()); string check = "", cat = ""; string[] line; item x = new item(); try { // check to see if it is understandable check = openFile.ReadLine(); if (check != "Type,Trans Date,Post Date,Description,Amount") { MessageBox.Show("Not a Chase activity file!"); return; } } catch (IOException f) { MessageBox.Show("File cannot be read!"); return; } catch (OutOfMemoryException f) { MessageBox.Show("File too large to read!"); return; } // obtain max GUID to add on to for new items x.id = Form1_Helper.getGUID(); while (!openFile.EndOfStream) { check = openFile.ReadLine(); line = check.Split(','); if (line[0] == "Sale") // deal with payments later maybe { // pick one of line[1] & line[2] to serve as date x.dt = DateTime.Parse(line[1]); x.desc = line[3]; x.cost = double.Parse(line[4]); // add a duplicate checker (date/desc/amt) ignore if match if (!Form1_Helper.IsDuplicate(x)) { // increment the ID from obtained maximum, or increment from previous Form1_Helper.IncrementID(ref x.id); // before prompting for categorization check if prior // categorization relationship already established cat = Form1_Helper.IsCategorized(x.desc); if (cat == "poop") { // 3. prompt for categorization of each line item if (Form1_Helper.CatBox(x.desc, x.cost, x.dt, ref x.cat) == DialogResult.OK) { // 4. assemble into internal database if (!Form1_Helper.writeItem(x)) { MessageBox.Show("Writing to database failed!"); return; } // should probably make this cleaner } else { MessageBox.Show("Skipped adding this item to database."); } } else { x.cat = cat; cat = ""; if (!Form1_Helper.writeItem(x)) { MessageBox.Show("Writing to database failed!"); return; } } } } } } }
public bool writeItem(item x) { try { SqlCeConnection cs = new SqlCeConnection(@"Data Source = Expenses.sdf"); cs.Open(); SqlCeCommand cmd = new SqlCeCommand("INSERT INTO LineItems VALUES (@Date, @Description, @Amount, @Category, @ID)", cs); cmd.Parameters.AddWithValue("@ID", x.id); cmd.Parameters.AddWithValue("@Date", x.dt); cmd.Parameters.AddWithValue("@Description", x.desc); cmd.Parameters.AddWithValue("@Amount", x.cost); cmd.Parameters.AddWithValue("@Category", x.cat); cmd.ExecuteNonQuery(); cs.Close(); } catch { return false; } return true; }
private void Manual_Click(object sender, EventArgs e) { // 1. dialog box with all fields: date, description, amount, category AddExpense form = new AddExpense(); item x = new item(); if (form.ShowDialog() == DialogResult.OK) { x.id = Form1_Helper.getGUID(); Form1_Helper.IncrementID(ref x.id); x.dt = form.timestamp; x.cost = -form.amount; x.desc = form.description; x.cat = form.category; } else { MessageBox.Show("Manual add cancelled!"); return; } // 2. assemble into internal database Form1_Helper.writeItem(x); }
public bool IsDuplicate(item x) { bool success; // need to write a dupe checker try { SqlCeConnection cs = new SqlCeConnection(@"Data Source = Expenses.sdf"); cs.Open(); SqlCeCommand cmd = new SqlCeCommand("SELECT ID FROM LineItems WHERE (Date = @Date AND Description = @Description AND Amount = @Amount)", cs); cmd.Parameters.AddWithValue("@Date", x.dt); cmd.Parameters.AddWithValue("@Description", x.desc); cmd.Parameters.AddWithValue("@Amount", x.cost); SqlCeDataReader r = cmd.ExecuteReader(); if (r.Read()) success = true; else success = false; cs.Close(); } catch { MessageBox.Show("Database access error!"); return true; } return success; }