Ejemplo n.º 1
0
        private void toolStripImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                object missing = Type.Missing;
                Excel.Range range;
                Excel.Worksheet sheet = null;
                Excel.Workbook wb = null;

                Excel.Application excelApp = new Excel.Application();
                wb = excelApp.Workbooks.Open(dlg.FileName,
                        missing, missing, missing,
                        missing, missing, missing, missing,
                        missing, missing, missing, missing,
                        missing, missing, missing);
                sheet = (Excel.Worksheet)wb.Worksheets[1];
                range = sheet.UsedRange;

                if (range != null)
                {
                    using (var db = new SMEntities())
                    {
                        // Bind DataSource
                        int nRows = range.Rows.Count;
                        int num;
                        decimal d;
                        bool parsed;
                        for (int iRow = 2; iRow <= nRows; iRow++)
                        {
                            Product p = new Product();
                            // Get ID
                            range = (Excel.Range)sheet.Cells[iRow, 2];
                            p.prod_id = range.Text;
                            // Get Name
                            range = (Excel.Range)sheet.Cells[iRow, 3];
                            p.prod_name = range.Text;

                            // Get Quality_control
                            range = (Excel.Range)sheet.Cells[iRow, 4];
                            parsed = int.TryParse(range.Text, out num);
                            if (parsed)
                            {
                                p.quantity_control = num;
                            }
                            else p.quantity_control = 1;

                            // Get Sale_price_by_stock
                            range = (Excel.Range)sheet.Cells[iRow, 5];
                            parsed = decimal.TryParse(range.Text, out d);
                            if (parsed)
                            {
                                p.sale_price_by_stock = d;
                            }
                            else p.sale_price_by_stock = 0;

                            // Set sale_price_by_unit
                            range = (Excel.Range)sheet.Cells[iRow, 6];
                            parsed = decimal.TryParse(range.Text, out d);
                            if (parsed)
                            {
                                p.sale_price_by_unit = d;
                            }
                            else p.sale_price_by_unit = 0;

                            // Check as if product has been existed
                            var prod = db.Products.Where(t => t.prod_id.Equals(p.prod_id));
                            if (!prod.Any())//Not exist
                            {
                                db.Products.Add(p);
                                db.SaveChanges();
                            }
                        }
                    }
                }

                wb.Close();
                excelApp.Quit();

                Marshal.ReleaseComObject(wb);
                Marshal.ReleaseComObject(sheet);
            }
        }
Ejemplo n.º 2
0
        private void btnCreateNew_Click(object sender, EventArgs e)
        {
            using(var db = new SMEntities()){
                DialogResult result = MessageBox.Show("Bạn muốn tạo mới khách hàng với thông tin như trên?",
                    "Confirmation",
                    MessageBoxButtons.YesNoCancel);
                if(result == DialogResult.Yes)
                {
                    if (txtCustName.Text == String.Empty || txtAddress.Text == String.Empty)
                    {
                        MessageBox.Show("Nhập thiếu thông tin khách hàng");
                        return;
                    }
                    Customer c = db.Customers.Create();
                    c.cust_name = txtCustName.Text;
                    c.cust_phone = txtPhone.Text;
                    c.cust_address = txtAddress.Text;

                    db.Customers.Add(c);
                    db.SaveChanges();
                }
            }
        }
Ejemplo n.º 3
0
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if(txtId.Text == string.Empty || txtName.Text == string.Empty ||
                txtQC.Text == string.Empty || txtSalePriceStock.Text == string.Empty)
            {
                MessageBox.Show("Chưa nhập đủ dữ liệu sản phẩm");
                return;
            }

            using (var db = new SMEntities())
            {
                // Bind DataSource
                Product p = new Product();
                p.prod_id = txtId.Text.ToString();
                p.prod_name = txtName.Text.ToString();
                p.quantity_control = int.Parse(txtQC.Text.ToString());

                p.sale_price_by_stock = decimal.Parse(txtSalePriceStock.Text.ToString());
                p.sale_price_by_unit = decimal.Parse(txtSalePriceUnit.Text.ToString());

                // Get default values
                p.quantity_by_stock = 0;
                p.quantity_by_unit = 0;
                p.date_added = DateTime.Now;
                p.date_modified = p.date_added;

                // Check as if products has been existed
                var query = db.Products.Where(t => t.prod_id.Equals(p.prod_id));
                if (!query.Any())// Not exist
                {
                    db.Products.Add(p);
                    db.SaveChanges();
                    clearEntries();
                }
                else
                {
                    // Accept change Name and sale price
                    var result = MessageBox.Show(this, "Sản phẩm đã có trong dữ liệu.\n Bạn có muốn lưu tên và giá bán hiện tại?",
                         "Lưu sản phẩm hàng",
                         MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        Product updatePro = query.First();
                        updatePro.prod_name = p.prod_name;
                        updatePro.sale_price_by_stock = p.sale_price_by_stock;
                        updatePro.sale_price_by_unit = p.sale_price_by_unit;
                        db.SaveChanges();
                        clearEntries();
                    }
                }
            }
        }