コード例 #1
0
        // method to insert and update records to the database
        private void btnSave_Click(object sender, EventArgs e)
        {
            int num;

            if (btnSave.Text == "Add")
            {
                try
                {
                    if (txtProductId.Text == "")
                    {
                        MessageBox.Show("Please Enter Product Id", "Product Id");
                        txtProductId.Focus();
                    }
                    else if (!Int32.TryParse(txtProductId.Text, out num) && num <= 0)
                    {
                        MessageBox.Show("Please Enter a valid Number", "Entry_Error");
                        txtProductId.Focus();
                    }
                    else if (txtProductName.Text == "")
                    {
                        MessageBox.Show("Please Enter Product Name", "Product Name");
                        txtProductName.Focus();
                    }
                    else
                    {
                        prod           = new Product();
                        prod.ProductId = Convert.ToInt32(txtProductId.Text);
                        prod.Name      = txtProductName.Text;
                        ProductDB.AddEntity(prod);
                        LoadComboxProducts();
                        LoadListView();
                        pnlProduct.Visible       = false;
                        lsvProduct.Visible       = true;
                        lblSelectProd.Visible    = true;
                        cboProducts.Visible      = true;
                        btnDeleteProduct.Enabled = true;
                        btnProduct.Enabled       = true;
                        txtProductId.Enabled     = true;
                        MessageBox.Show("New Product Added Successfully");
                    }
                }
                catch (SqlException ex)
                {
                    string errorMsg = "A record exist with the same Product Id";
                    MessageBox.Show(errorMsg, ex.Message);
                }
            }
            // update record
            else if (btnSave.Text == "Update")
            {
                Product newProduct = new Product();
                newProduct.ProductId = prod.ProductId;
                acceptRecord(newProduct);
                try
                {
                    if (!ProductDB.UpdateProduct(prod, newProduct))
                    {
                        MessageBox.Show("Another Product Exist with similar records", "Database Error");
                        this.DialogResult = DialogResult.Retry;
                    }
                    else
                    {
                        prod = newProduct;
                        LoadComboxProducts();
                        LoadListView();
                        pnlProduct.Visible       = false;
                        lsvProduct.Visible       = true;
                        lblSelectProd.Visible    = true;
                        cboProducts.Visible      = true;
                        btnDeleteProduct.Enabled = true;
                        btnProduct.Enabled       = true;
                        txtProductId.Enabled     = true;
                        MessageBox.Show("Product record Updated successfully", "Update Record");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, ex.GetType().ToString());
                }
            }
        }