private void detach_Purchase_Items(Purchase_Item entity)
 {
     this.SendPropertyChanging();
     entity.Purchase = null;
 }
 private void attach_Purchase_Items(Purchase_Item entity)
 {
     this.SendPropertyChanging();
     entity.Purchase = this;
 }
 partial void DeletePurchase_Item(Purchase_Item instance);
 partial void UpdatePurchase_Item(Purchase_Item instance);
 partial void InsertPurchase_Item(Purchase_Item instance);
Ejemplo n.º 6
0
        private void btnPurchase_Click(object sender, EventArgs e)
        {
            // using (Global.Context)
            using (DataClasses1DataContext c = new DataClasses1DataContext())
            {
                //Global.CurrUser = c.UserAccounts.Where(u => u.username == "rb").FirstOrDefault();

                try
                {
                    if (Global.CurrUser.balance > Global.MAXBALANCE)
                    {
                        MessageBox.Show(String.Format("Your current balance is {0:C}."
                                                      + "\nThe Maximum Balance allowed to make a purchase is {1:C}." +
                                                      "Please make a payment in order to purchase further.", Global.CurrUser.balance, Global.MAXBALANCE));
                        this.Hide();
                    }
                    else
                    {
                        int     colVal;
                        int     itemID;
                        decimal total    = 0m;
                        int     numItems = 0;
                        decimal price;
                        string  itemDesc;
                        List <Purchase_Item> purItems = new List <Purchase_Item>();


                        foreach (DataGridViewRow aRow in dataGridView1.Rows)
                        {
                            DataGridViewCell col = aRow.Cells["Quantity"];//dk if will work
                            if (col.Value != null)
                            {
                                if (Int32.TryParse(col.Value.ToString(), out colVal))
                                {
                                    Purchase_Item pi = new Purchase_Item();
                                    itemID = (int)(aRow.Cells["itemID"].Value);

                                    pi.itemID = itemID;
                                    pi.amount = colVal;
                                    purItems.Add(pi);

                                    total    += colVal * (Decimal)(aRow.Cells["price"].Value);
                                    numItems += colVal;
                                }
                                else
                                {
                                    MessageBox.Show("Quantity column is innacurate for " + aRow.Cells["itemDesc"].Value.ToString() + ".\nExcluded from order.");
                                }
                            }
                        }
                        Purchase p = new Purchase();

                        p.username   = Global.CurrUser.username;
                        p.numItems   = numItems;
                        p.totalPrice = (decimal)(total);
                        //p.totalPrice = 7.97m;
                        p.purchaseDate = DateTime.Now;
                        //Global.Context.Purchases.InsertOnSubmit(p);

                        //test -this works
                        // int i = Global.Context.ExecuteCommand("insert into Purchase values('rb', 2, 5.00, getDate())");
                        //ChangeSet x2 = Global.Context.GetChangeSet();
                        c.Purchases.InsertOnSubmit(p);
                        c.SubmitChanges();
                        //Global.Context.SubmitChanges();
                        //doesn't submit?
                        //bool x = Global.Context.Purchases.Contains(p);
                        //MessageBox.Show(x.ToString());

                        int pID = p.PurchaseID;

                        //does this update the table in the datacontext?
                        Global.CurrUser.balance = Global.CurrUser.balance + total;

                        foreach (Purchase_Item pi in purItems)
                        {
                            pi.purchaseID = pID;
                            //Global.Context.Purchase_Items.InsertOnSubmit(pi);
                            c.Purchase_Items.InsertOnSubmit(pi);
                            //Global.Context.SubmitChanges();
                            c.SubmitChanges();
                        }
                        MessageBox.Show("Order submitted. Thank you for shopping with us");
                        this.Hide();
                    }
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Error, could not process order" + (ex.StackTrace));
                }
                //catch(NotSupportedException ex1)
                //{
                //    MessageBox.Show("Error, could not process order");
                //}
            }
        }