Esempio n. 1
0
        //-------------------------------------------------------------------
        private void frmAddProduct_Load(object sender, EventArgs e)
        {
            BOM = new BindingList <ProductBuildSheet>();
            //add Parts to the combobox
            ClassLibrary.Part part = new ClassLibrary.Part();

            dtParts                = part.GetAll().Tables[0];
            cmbParts.DataSource    = dtParts;
            cmbParts.DisplayMember = "PART_name";

            //ADDING MODE
            if (drCustomer == null)
            {
            }
            //EDITING MODE
            else
            {
                try
                {
                    //System.Drawing.Image _image = System.Drawing.Image.FromStream
                    //    (new System.IO.MemoryStream((byte[])drCustomer[6]));

                    //byte[] blob = (byte[])drCustomer[6];
                    //picProductImage.Image = _image;
                    //picProductImage.SizeMode = PictureBoxSizeMode.StretchImage;
                    //picProductImage.Refresh();

                    picProductImage.Image    = Image.FromFile(picturesPath + drCustomer[6].ToString());
                    picProductImage.SizeMode = PictureBoxSizeMode.StretchImage;
                    picProductImage.Refresh();
                }
                catch (Exception ex) {}


                //display product information
                txtName.Text        = drCustomer.ItemArray[1].ToString();
                txtCost.Text        = String.Format("{0:c}", drCustomer.ItemArray[2]);
                txtPrice.Text       = String.Format("{0:c}", drCustomer.ItemArray[3]);
                txtColor.Text       = drCustomer.ItemArray[4].ToString();
                txtDescription.Text = drCustomer.ItemArray[5].ToString();

                //display buildsheet
                ClassLibrary.BuildOfMaterial BuildOfMaterial = new ClassLibrary.BuildOfMaterial();
                BuildOfMaterial.ProductId = Convert.ToInt32(drCustomer.ItemArray[0]);
                DataTable thisTable = BuildOfMaterial.GetAllPartsByProductId().Tables[0];


                foreach (DataRow row in thisTable.Rows)
                {
                    //add to generic list
                    BOM.Add(new ProductBuildSheet
                    {
                        PartID   = Convert.ToInt32(row.ItemArray[2]),
                        PartName = row.ItemArray[0].ToString(),
                        Quantity = Convert.ToInt32(row.ItemArray[1]),
                    });
                }

                dataGridView1.DataSource = BOM;
            }
        }
Esempio n. 2
0
        //-------------------------------------------------------------------
        private void btnSaveToDatabase_Click(object sender, EventArgs e)
        {
            decimal result;

            if (
                (txtName.Text == "") ||
                (txtPrice.Text == "") ||
                (txtCost.Text == "") ||
                (txtDescription.Text == "")
                )
            {
                MessageBox.Show("please fill all the empty fields");
            }
            else if (!decimal.TryParse(txtPrice.Text.Replace("$", ""), out result))
            {
                MessageBox.Show("please enter a valid number for 'price'");
            }
            else if (!decimal.TryParse(txtCost.Text.Replace("$", ""), out result))
            {
                MessageBox.Show("please enter a valid number for 'Cost'");
            }
            else
            {
                try
                {
                    ClassLibrary.Product product = new ClassLibrary.Product();
                    product.Color       = txtColor.Text;
                    product.Cost        = Convert.ToDecimal(txtCost.Text.Replace("$", ""));
                    product.Description = txtDescription.Text;
                    product.Name        = txtName.Text;
                    product.Price       = Convert.ToDecimal(txtPrice.Text.Replace("$", ""));



                    //ADDING MODE
                    if (drCustomer == null)
                    {
                        if (imageName != null)
                        {
                            product.Pic = imageName;
                            //copy image to the root directory of the application
                            System.IO.File.Copy(imagePath, picturesPath + imageName, true);
                        }
                        else
                        {
                            product.Pic = "";
                        }
                        //add product
                        int LastAddedId = product.Add();

                        // product buildsheet
                        ClassLibrary.BuildOfMaterial BuildOfMaterial = new ClassLibrary.BuildOfMaterial();
                        BuildOfMaterial.ProductId = LastAddedId;

                        foreach (ProductBuildSheet bom in BOM)
                        {
                            BuildOfMaterial.PartID       = bom.PartID;
                            BuildOfMaterial.PartQuantity = bom.Quantity;
                            BuildOfMaterial.Add();
                        }
                    }
                    //EDITING MODE
                    else
                    {
                        //add image
                        if (imageName != null)
                        {
                            product.Pic = imageName;
                            //copy image to the root directory of the application
                            System.IO.File.Copy(imagePath, picturesPath + imageName, true);
                        }
                        else
                        {
                            //use the same image name you got when form was loaded
                            product.Pic = drCustomer.ItemArray[6].ToString();
                        }

                        //update product information
                        product.Id = Convert.ToInt32(drCustomer.ItemArray[0]);
                        product.Update();

                        //update product buildsheet
                        ClassLibrary.BuildOfMaterial BuildOfMaterial = new ClassLibrary.BuildOfMaterial();
                        BuildOfMaterial.ProductId = product.Id;
                        BuildOfMaterial.Delete();

                        foreach (ProductBuildSheet bom in BOM)
                        {
                            BuildOfMaterial.PartID       = bom.PartID;
                            BuildOfMaterial.PartQuantity = bom.Quantity;
                            BuildOfMaterial.Add();
                        }
                    }

                    frmDisProducts.DisplayAllProducts();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                Close();
            }
        }