static void Main(string[] args)
        {
            northwindEntities db1 = new northwindEntities();
            northwindEntities db2 = new northwindEntities();

            //Categories can be added simultaneously
            for (int i = 0; i < 10; i++)
            {
                db1.Categories.Add(new Category()
                {
                    CategoryName = "NewCategory" + i
                });
                db2.Categories.Add(new Category()
                {
                    CategoryName = "NewCategory" + i
                });
                db1.SaveChanges();
                db2.SaveChanges();
            }

            //Editin same entry
            var firstEntry = db1.Categories.Where((x) => x.CategoryName == "NewCategory0").First();

            firstEntry.CategoryName = "Lol";

            var sameEntry = db2.Categories.Where(x => x.CategoryName == "NewCategory0").First();

            db2.Categories.Remove(sameEntry);

            //Removing an entry before editing it throws an exception
            //db2.SaveChanges();
            db1.SaveChanges();
        }
Exemplo n.º 2
0
    public void InsertNewItem(OrderedDictionary newValues)
    {
        Category category = new Category();

        LoadNewValues(category, newValues);
        dataBase.Categories.Add(category);
        dataBase.SaveChanges();
    }
Exemplo n.º 3
0
        public ActionResult Create([Bind(Include = "ContactID,ContactType,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Extension,Fax,HomePage,PhotoPath,Photo")] Contacts contacts)
        {
            if (ModelState.IsValid)
            {
                db.Contacts.Add(contacts);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(contacts));
        }
Exemplo n.º 4
0
        public ActionResult Create([Bind(Include = "CategoryID,CategoryName,Description,Picture")] Categories categories)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Add(categories);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(categories));
        }
Exemplo n.º 5
0
        public ActionResult Create([Bind(Include = "CustomerID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax")] Customers customers)
        {
            if (ModelState.IsValid)
            {
                db.Customers.Add(customers);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(customers));
        }
Exemplo n.º 6
0
        public ActionResult Create([Bind(Include = "EmployeeID,LastName,FirstName,Title,TitleOfCourtesy,BirthDate,HireDate,Address,City,Region,PostalCode,Country,HomePhone,Extension,Photo,Notes,ReportsTo,PhotoPath")] emp_apu emp_apu)
        {
            if (ModelState.IsValid)
            {
                db.emp_apu.Add(emp_apu);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(emp_apu));
        }
 public ActionResult Edit([Bind(Include = "ShipperID, CompanyName, Phone, RegionID")] Shippers shipper) //haetaan ShipperID jne kentät...? tämä jäi vähän hämäräksi
 {
     if (ModelState.IsValid)
     {
         db.Entry(shipper).State = EntityState.Modified;
         db.SaveChanges();
         ViewBag.RegionID = new SelectList(db.Region, "RegionID", "RegionDescription", shipper.RegionID);
         return(RedirectToAction("Index"));
     }
     return(View(shipper));
 }
 [ValidateAntiForgeryToken] //Katso https://go.microsoft.com/fwlink/?LinkId=317598
 public ActionResult Edit([Bind(Include = "ShipperID,CompanyName,Phone,RegionID")] Shippers shipper)
 {
     if (ModelState.IsValid)
     {
         entities.Entry(shipper).State = EntityState.Modified;
         entities.SaveChanges();
         ViewBag.RegionID = new SelectList(entities.Region, "RegionID", "RegionDescription", shipper.RegionID);
         return(RedirectToAction("Index"));
     }
     return(View(shipper));
 }
Exemplo n.º 9
0
        public ActionResult Create(string CustomerID, string CompanyName, string ContactName, string ContactTitle, string Country, string City, string Region, string Address, string PostalCode, string Phone, string Fax)
        {
            bool isError = false;

            if (string.IsNullOrWhiteSpace(CustomerID))
            {
                ModelState.AddModelError("CustomerID", "客戶編號為必填");
                isError = true;
            }
            else
            {
                ModelState.AddModelError("CustomerID", "");
            }

            if (string.IsNullOrWhiteSpace(CompanyName))
            {
                ModelState.AddModelError("CompanyName", "公司名稱為必填");
                isError = true;
            }
            else
            {
                ModelState.AddModelError("CompanyName", "");
            }

            if (isError)
            {
                return(View());
            }
            Customers cust = new Customers();

            cust.CustomerID   = CustomerID;
            cust.CompanyName  = CompanyName;
            cust.ContactName  = ContactName;
            cust.ContactTitle = ContactTitle;
            cust.Country      = Country;
            cust.City         = City;
            cust.Region       = Region;
            cust.Address      = Address;
            cust.PostalCode   = PostalCode;
            cust.Phone        = Phone;
            cust.Fax          = Fax;
            db.Customers.Add(cust);
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                ViewBag.Message = "新增資料發生錯誤:" + ex.Message;
                return(View());
            }
            return(RedirectToAction("Index"));
        }
        public ActionResult Create([Bind(Include = "EmployeeID,LastName,FirstName,Title,TitleOfCourtesy,BirthDate,HireDate,Address,City,Region,PostalCode,Country,HomePhone,Extension,ReportsTo")] Employees employees)
        {
            if (ModelState.IsValid)
            {
                db.Employees.Add(employees);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ReportsTo = new SelectList(db.Employees, "EmployeeID", "LastName", employees.ReportsTo);
            return(View(employees));
        }
Exemplo n.º 11
0
        public ActionResult Create([Bind(Include = "ShipperID,RegionID,CompanyName,Phone")] Shippers shippers)
        {
            if (ModelState.IsValid)
            {
                db.Shippers.Add(shippers);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.RegionID = new SelectList(db.Region, "RegionID", "RegionDescription", shippers.RegionID);
            return(View(shippers));
        }
        public ActionResult Create([Bind(Include = "OrderID,ProductID,UnitPrice,Quantity,Discount")] Order_Details order_Details)
        {
            if (ModelState.IsValid)
            {
                db.Order_Details.Add(order_Details);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.OrderID   = new SelectList(db.Orders, "OrderID", "CustomerID", order_Details.OrderID);
            ViewBag.ProductID = new SelectList(db.Products, "ProductID", "ProductName", order_Details.ProductID);
            return(View(order_Details));
        }
Exemplo n.º 13
0
        public ActionResult Create([Bind(Include = "ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued")] Products.Model.Products products)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(products);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", products.CategoryID);
            ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "CompanyName", products.SupplierID);
            return(View(products));
        }
Exemplo n.º 14
0
        public ActionResult Create([Bind(Include = "OrderID,CustomerID,EmployeeID,OrderDate,RequiredDate,ShippedDate,ShipVia,Freight,ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry")] Orders orders)
        {
            if (ModelState.IsValid)
            {
                db.Orders.Add(orders);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "CompanyName", orders.CustomerID);
            ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "LastName", orders.EmployeeID);
            ViewBag.ShipVia    = new SelectList(db.Shippers, "ShipperID", "CompanyName", orders.ShipVia);
            return(View(orders));
        }
Exemplo n.º 15
0
        static void InsertOrder(string customerID, DateTime orderDate, string address, params Tuple <int, int>[] products)
        {
            Order newOrder = new Order()
            {
                CustomerID  = customerID,
                OrderDate   = orderDate,
                ShipAddress = address
            };

            var insertedOrder = db.Orders.Add(newOrder);

            foreach (var prod in products)
            {
                Order_Detail newDetail = new Order_Detail()
                {
                    OrderID   = insertedOrder.OrderID,
                    ProductID = prod.Item1,
                    Quantity  = (short)prod.Item2
                };

                db.Order_Details.Add(newDetail);
            }

            db.SaveChanges();
        }
Exemplo n.º 16
0
        public ActionResult Duzenle(Products model)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "Hatalı bir işlemi başarısız");
                return(View(model));
            }

            var db   = new northwindEntities();
            var urun = db.Products.Find(model.ProductID);

            if (urun == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            try
            {
                urun.ProductName  = model.ProductName;
                urun.UnitPrice    = model.UnitPrice;
                urun.UnitsInStock = model.UnitsInStock;
                urun.Discontinued = model.Discontinued;
                urun.CategoryID   = model.CategoryID;
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Güncelle işlemi başarısız");
                return(View(model));
            }
            //return View(model);
            return(RedirectToAction("Duzenle", new { id = urun.ProductID }));
        }
Exemplo n.º 17
0
 public ActionResult Edit([Bind(Include = "ShipperID, CompanyName, Phone, RegionID")] Shippers shipperi)
 {
     if (Session["UserName"] == null)
     {
         return(RedirectToAction("login", "home"));
     }
     else
     {
         if (ModelState.IsValid)
         {
             db.Entry(shipperi).State = EntityState.Modified;
             db.SaveChanges();
             ViewBag.RegionID = new SelectList(db.Region, "RegionID", "RegionDescription", shipperi.RegionID);
             return(RedirectToAction("Index"));
         }
         return(View(shipperi));
     }
 }
Exemplo n.º 18
0
        public static bool InsertCustomer(int customerId, string companyName, string contactName, string country, string phone)
        {
            Customer newCustomer = new Customer()
            {
                CustomerID  = Convert.ToString(customerId),
                CompanyName = companyName,
                ContactName = contactName,
                Country     = country,
                Phone       = phone
            };

            db.Customers.Add(newCustomer);
            if (db.SaveChanges() == 1)
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 19
0
        // POST api/values
        public void Post(Book value)
        {
            Book book = value;

            northwindEntities context = new northwindEntities();
            Product           product = new Product();

            product.ProductName = book.title;

            context.Products.Add(product);
            context.SaveChanges();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Product p = new Product()
            {
                ProductName  = TextBox1.Text,
                UnitPrice    = decimal.Parse(TextBox2.Text),
                UnitsInStock = short.Parse(TextBox3.Text),
                CategoryID   = Convert.ToInt32(DropDownList1.SelectedValue)
            };

            db.Products.Add(p);
            db.SaveChanges();
            Response.Redirect("SelectData.aspx");
        }
Exemplo n.º 21
0
        public ActionResult Yeni(Products model)
        {
            var urun = new Products()
            {
                ProductName  = model.ProductName,
                UnitPrice    = model.UnitPrice,
                Discontinued = model.Discontinued,
                CategoryID   = model.CategoryID,
                UnitsInStock = model.UnitsInStock
            };

            try
            {
                var db = new northwindEntities();
                db.Products.Add(urun);
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw;
            }
            return(RedirectToAction("Duzenle", new { id = urun.ProductID }));
        }