Esempio n. 1
0
        }//end remove item

        /// <summary>
        /// if given invoice id Updates the date and total charge. delete everything in the LineItem table, and write the data again with the data in the datatable
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddUpdate_Click(object sender, RoutedEventArgs e)
        {
            try {
                ///if statement to check and compare dates
                if (invoiceDatePicker.SelectedDate == null)
                {
                    invoiceDatePicker.SelectedDate = DateTime.Now.Date;
                }              // if no date is picked, default to today
                int count = 0; //keeps track of records inserted
                ///if invoice id is null
                if (invoiceId != "")
                {
                    //updates the date, even if there were no changes.
                    String sSQL = mydb.updateInvoiceDate(invoiceDatePicker.SelectedDate.Value.ToShortDateString(), invoiceId);
                    db.ExecuteNonQuery(sSQL);
                    System.Console.WriteLine(sSQL);

                    //updates the cost of the associated invoiceId
                    sSQL = mydb.updateTotalCharge(calculateTotal() + "", invoiceId);
                    db.ExecuteNonQuery(sSQL);

                    System.Console.WriteLine(sSQL);

                    //removing all LineItems associated with that invoice number, and adding them again with the added/removed items
                    sSQL = mydb.DeleteLineItems(invoiceId);
                    db.ExecuteNonQuery(sSQL);


                    //grabs the data from the DataTable, runs a sql statement adding each line individually.
                    for (int i = 0; i < dtInvoice.Rows.Count; i++)
                    {
                        sSQL = mydb.addLineItem(invoiceId, i + 1 + "", inventoryDictionary[dtInvoice.Rows[i][0] + ""]);
                        System.Console.WriteLine(sSQL);
                        db.ExecuteNonQuery(sSQL);
                        count++;
                    }

                    MessageBox.Show("Invoice: " + invoiceId + " added successfully!\n" + count + " items added");
                }
                else
                {
                    String sSQL = mydb.addInvoice(invoiceDatePicker.SelectedDate.Value.ToShortDateString(), calculateTotal() + "");
                    db.ExecuteNonQuery(sSQL);
                    sSQL      = mydb.latestInvoice();
                    invoiceId = db.ExecuteScalarSQL(sSQL);

                    for (int i = 0; i < dtInvoice.Rows.Count; i++)
                    {
                        sSQL = mydb.addLineItem(invoiceId, i + 1 + "", inventoryDictionary[dtInvoice.Rows[i][0] + ""]);
                        System.Console.WriteLine(sSQL);
                        db.ExecuteNonQuery(sSQL);
                        count++;//keeps track of items added.
                    }

                    MessageBox.Show("Invoice: " + invoiceId + " added successfully!\n" + count + " items added");
                }//end else
            } catch (Exception) {
                MessageBox.Show(MethodInfo.GetCurrentMethod().DeclaringType.Name);
            }
        }//end add/update click