示例#1
0
        public static bool ReSeedData(out string error)
        {
            // LINQ-to-SQL data context. Automatically created with .dbml
            TelevisionDbDataContext context = new TelevisionDbDataContext();

            try
            {
                // delete all existing rows (DELETE FROM Defects).
                context.Products.DeleteAllOnSubmit(context.Products);
                // insert each Defect from the sample data
                foreach (Product d in data)
                {
                    context.Products.InsertOnSubmit(d);
                }
                // commit
                context.SubmitChanges();
                error = "";
                return(true);
            }
            catch (Exception e)
            {
                // return message from thrown exception
                error = e.Message;
                return(false);
            }
        }
示例#2
0
        protected void btn_purchase_Click(object sender, EventArgs e)
        {
            TelevisionDbDataContext context = new TelevisionDbDataContext();
            Dictionary<string, int> televisions = (Dictionary<string, int>)Session["cart"];

            
            using (TransactionScope cartTrans = new TransactionScope())
            {
                try
                {
                    //Updating the Database
                    foreach (var key in televisions.Keys)
                    {
                        int value = televisions[key];

                        //Checking to See if Quantity in cart is greater than 0
                        if (value > 0)
                        {

                            var query = from p in context.Products
                                        where p.Name == key
                                        select p;
                            theProduct = query.Single();

                            theProduct.Quantity = theProduct.Quantity - televisions[key];
                            context.SubmitChanges();

                        }
                    }
                    var nameList = (from p in context.Products
                                    select p.Name).ToList();

                    List<string> names = new List<string>();
                    names = nameList;

                    televisions = new Dictionary<string, int>();

                    for (int i = 0; i < names.Count; i++)
                    {
                        televisions.Add(names[i].Trim(), 0);
                    }

                    Session["cart"] = televisions;

                    Response.Redirect("~/ThankYou.aspx",false);

                    cartTrans.Complete();
                }
                catch (Exception E)
                {
                    cartTrans.Dispose();
                    Response.Redirect("~/ErrorPage.aspx?err=" + E.Message);
                }
            }
        }