예제 #1
0
 public bool AddProduct(string ProductName, string ProductDesc,
     string ProductPrice, string ProductCategory, string ProductImagePath)
 {
     var myProduct = new Product();
     myProduct.ProductName = ProductName;
     myProduct.Description = ProductDesc;
     myProduct.UnitPrice = Convert.ToDouble(ProductPrice);
     myProduct.ImagePath = ProductImagePath;
     myProduct.CategoryID = Convert.ToInt32(ProductCategory);
     using (ProductContext _db = new ProductContext())
     {
         _db.Products.Add(myProduct);
         _db.SaveChanges();
     }
     return true;
 }
예제 #2
0
        protected void updateProductName_ServerValidate(object sender, ServerValidateEventArgs e)
        {
            //String connectionString = @"Data Source=DESKTOP-S3P3TBM\SQLEXPRESS;Initial Catalog=wingtiptoys.mdf;Integrated Security=True";
            string connectionString = "Data Source=MYLAPTOP\\SQLEXPRESS;Database=wingtiptoys.mdf;Integrated Security=True";
            string queryString      = "SELECT * FROM dbo.Products WHERE ProductName = '" + DropDownUpdateProductName.SelectedItem.Text + "'";

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(queryString, connection);
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                try
                {
                    if (reader.Read())
                    {
                        //update product record in database
                        e.IsValid = true;
                        System.Diagnostics.Debug.WriteLine(e.Value.ToString() + " Exists");
                        //update product in database
                        using (var _db = new WingtipToys.Models.ProductContext())
                        {
                            var myItem = (from c in _db.Products where c.ProductName == DropDownUpdateProductName.SelectedItem.Text select c).FirstOrDefault();
                            if (myItem != null)
                            {
                                System.Diagnostics.Debug.WriteLine(myItem.Description);
                                //remove product from database
                                _db.Products.Remove(myItem);
                                _db.SaveChanges();

                                // Add product data to DB.
                                AddProducts products = new AddProducts();
                                var         product  = new WingtipToys.Models.Product();
                                //update name, description, and price, but keep same category id and image file path for now
                                bool addSuccess = products.AddProduct(DropDownUpdateProductName.SelectedItem.Text, UpdateProductDescription.Text,
                                                                      UpdateProductPrice.Text, DropDownUpdateCategory.SelectedValue, myItem.ImagePath);
                                if (addSuccess)
                                {
                                    // Reload the page.
                                    string pageUrl = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.Count() - Request.Url.Query.Count());
                                    Response.Redirect(pageUrl + "?ProductAction=add");
                                }
                                else
                                {
                                    System.Diagnostics.Debug.WriteLine("Unable to add new product to database.");
                                }
                            }
                            else
                            {
                                System.Diagnostics.Debug.WriteLine("Unable to locate product.");
                            }
                        }
                    }
                    else
                    {
                        //Throw validation error because product does not exist and cannot be updated
                        e.IsValid = false;
                        System.Diagnostics.Debug.WriteLine(e.Value.ToString() + " Does not exist");
                    }
                }
                finally
                {
                    reader.Close();
                }
            }
        }