Exemple #1
0
        private void btnDelete_Click(object sender, EventArgs e)
        {
            // get the key of the current product in the data grid view
            int    rowNum   = productDataGridView.CurrentCell.RowIndex;                                   // index of the current row
            string prodCode = productDataGridView["dataGridViewTextBoxColumn1", rowNum].Value.ToString(); // Column for ProductCode

            DialogResult answer = MessageBox.Show("Are you sure you want to delete " + prodCode + "?", "Confirm", MessageBoxButtons.OKCancel);

            if (answer == DialogResult.OK)
            {
                using (ProductsDataContext dbContext = new ProductsDataContext())
                {
                    try
                    {
                        Product currentProduct = (from p in dbContext.Products
                                                  where p.ProductCode == prodCode
                                                  select p).Single();
                        //MessageBox.Show("Testing concurrency: update or delete current record from SSMS and click OK");

                        dbContext.Products.DeleteOnSubmit(currentProduct);
                        dbContext.SubmitChanges();
                        RefreshGridView();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, ex.GetType().ToString());
                    }
                }
            }
        }
Exemple #2
0
        private void btnModify_Click(object sender, EventArgs e)
        {
            // get the key of the current product in the data grid view
            int    rowNum   = productDataGridView.CurrentCell.RowIndex;                                   // index of the current row
            string prodCode = productDataGridView["dataGridViewTextBoxColumn1", rowNum].Value.ToString(); // Column for ProductCode

            Product currentProduct;

            using (ProductsDataContext dbContext = new ProductsDataContext())
            {
                currentProduct = (from p in dbContext.Products
                                  where p.ProductCode == prodCode
                                  select p).Single();
            }

            frmAddModifyProduct secondForm = new frmAddModifyProduct();

            secondForm.isAdd          = false;                             // it Modify
            secondForm.currentProduct = currentProduct;
            DialogResult result = secondForm.ShowDialog();                 // display second form modal

            if (result == DialogResult.OK || result == DialogResult.Retry) // successful update or concurrency exception
            {
                RefreshGridView();
            }
        }
Exemple #3
0
 private void Form1_Load(object sender, EventArgs e)
 {
     using (ProductsDataContext dbContext = new ProductsDataContext())
     {
         productDataGridView.DataSource = dbContext.Products; // entire table - no need for LINQ query
         //productDataGridView.DataSource = from prod in dbContext.Products
         //                                 orderby prod.Description
         //                                 select prod;
     }
 }
Exemple #4
0
        private bool IsUniqueCode(TextBox productCodeTextBox)
        {
            Product prod = null;

            using (ProductsDataContext dbContext = new ProductsDataContext())
            {
                prod = (from p in dbContext.Products
                        where p.ProductCode == productCodeTextBox.Text
                        select p).SingleOrDefault();

                //dbContext.Products.Single(p => p.ProductCode == productCodeTextBox.Text);
                if (prod != null) // there is another product with same code
                {
                    MessageBox.Show("Product code must be unique", "Entry Error");
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
        }
Exemple #5
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (isAdd)
            {
                if (Validator.IsPresent(productCodeTextBox) &&
                    Validator.IsCorrectLength(productCodeTextBox, 10) &&
                    IsUniqueCode(productCodeTextBox) && // coded below in this form
                    Validator.IsPresent(descriptionTextBox) &&
                    Validator.IsCorrectLength(descriptionTextBox, 50) &&
                    Validator.IsPresent(unitPriceTextBox) &&
                    Validator.IsDecimal(unitPriceTextBox) &&
                    Validator.IsNonNegativeDecimal(unitPriceTextBox) &&
                    Validator.IsPresent(onHandQuantityTextBox) &&
                    Validator.IsInt32(onHandQuantityTextBox) &&
                    Validator.IsNonNegativeInt(onHandQuantityTextBox)
                    )
                // replace with data validation: all fields provided, code unique,
                // price and quantity appropriate numeric type and non-negative
                {
                    Product newProduct = new Product // create product using provided data
                    {
                        ProductCode    = productCodeTextBox.Text,
                        Description    = descriptionTextBox.Text,
                        UnitPrice      = Convert.ToDecimal(unitPriceTextBox.Text),
                        OnHandQuantity = Convert.ToInt32(onHandQuantityTextBox.Text)
                    };
                    using (ProductsDataContext dbContext = new ProductsDataContext())
                    {
                        // insert through data context object from the main form
                        dbContext.Products.InsertOnSubmit(newProduct);
                        dbContext.SubmitChanges(); // submit to the database
                    }
                    DialogResult = DialogResult.OK;
                }
                else // validation  failed
                {
                    DialogResult = DialogResult.Cancel;
                }
            }
            else // it is Modify
            {
                if (Validator.IsPresent(descriptionTextBox) &&
                    Validator.IsCorrectLength(descriptionTextBox, 50) &&
                    Validator.IsPresent(unitPriceTextBox) &&
                    Validator.IsDecimal(unitPriceTextBox) &&
                    Validator.IsNonNegativeDecimal(unitPriceTextBox) &&
                    Validator.IsPresent(onHandQuantityTextBox) &&
                    Validator.IsInt32(onHandQuantityTextBox) &&
                    Validator.IsNonNegativeInt(onHandQuantityTextBox)
                    )
                {
                    try
                    {
                        using (ProductsDataContext dbContext = new ProductsDataContext())
                        {
                            // get the product with Code from the current text box
                            Product prod = dbContext.Products.Single(p => p.ProductCode == productCodeTextBox.Text);

                            //MessageBox.Show("Testing concurrency: update or delete current record from SSMS and click OK");

                            if (prod != null)
                            {
                                // make changes by copying values from text boxes
                                prod.Description    = descriptionTextBox.Text;
                                prod.UnitPrice      = Convert.ToDecimal(unitPriceTextBox.Text);
                                prod.OnHandQuantity = Convert.ToInt32(onHandQuantityTextBox.Text);
                                // submit changes
                                dbContext.SubmitChanges();
                                DialogResult = DialogResult.OK;
                            }
                        }
                    }
                    catch (ChangeConflictException)
                    {
                        MessageBox.Show("Another user changed or deleted the current record", "Concurrency Exception");
                        DialogResult = DialogResult.Retry;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, ex.GetType().ToString());
                    }
                }
                else // validation failed
                {
                    DialogResult = DialogResult.Cancel;
                }
            }
        }
Exemple #6
0
        private void RefreshGridView()
        {
            ProductsDataContext dbContext = new ProductsDataContext();

            productDataGridView.DataSource = dbContext.Products;
        }