private void SelectData() { string commandVendors = "SELECT * FROM VENDORS"; string commandMeasures = "SELECT * FROM MEASURES"; string commandCategories = "SELECT * FROM Categories"; DataTable dtV = new DataTable(); DataTable dtM = new DataTable(); DataTable dtC = new DataTable(); try { OracleDBAction oracleAction = new OracleDBAction(); dtV = oracleAction.SelectProduct(commandVendors); dtM = oracleAction.SelectProduct(commandMeasures); dtC = oracleAction.SelectProduct(commandCategories); if (dtV.Rows.Count > 0 && dtM.Rows.Count > 0) { Vendor[] tempVendors = new Vendor[dtV.Rows.Count]; Measure[] tempMeasures = new Measure[dtM.Rows.Count]; Category[] tempCategories = new Category[dtC.Rows.Count]; int counter = 0; foreach (DataRow currentRow in dtV.Rows) { int vendorId; string vendorName; if (!int.TryParse(currentRow[0].ToString(), out vendorId)) { throw new ArgumentException("Id for a vendor is not integer."); } vendorName = currentRow[1].ToString(); if (vendorName == string.Empty) { throw new ArgumentNullException("Vendor name is empty."); } tempVendors[counter] = new Vendor { Id= vendorId, Name = vendorName}; counter += 1; } vendors.Clear(); vendors.AddRange(tempVendors); counter = 0; foreach (DataRow currentRow in dtM.Rows) { int measureId; string measureName; if (!int.TryParse(currentRow[0].ToString(), out measureId)) { throw new ArgumentNullException("Id for a measure is not integer."); } measureName = currentRow[1].ToString(); if (measureName == string.Empty) { throw new ArgumentNullException("Measure name is empty."); } tempMeasures[counter] = new Measure {Id = measureId, Name = measureName}; counter += 1; } measures.Clear(); measures.AddRange(tempMeasures); counter = 0; foreach (DataRow currentRow in dtC.Rows) { int categoryId; string categoryName; if (!int.TryParse(currentRow[0].ToString(), out categoryId)) { throw new ArgumentNullException("Id for a category is not integer."); } categoryName = currentRow[1].ToString(); if (categoryName == string.Empty) { throw new ArgumentNullException("Category name is empty."); } tempCategories[counter] = new Category() { Id = categoryId, Name = categoryName }; counter += 1; } categories.Clear(); categories.AddRange(tempCategories); } } catch (Exception ex) { MessageBox.Show(ex.Message, Attention, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void InsertProduct(object sender, EventArgs e) { string commandString = "INSERT INTO PRODUCTS (NAME, PRICE, VENDOR_ID, MEASURE_ID, CATEGORY_ID)" + "VALUES (:name, :price, :vendor, :measure, :category)"; try { string name = productControl.Product; string priceAsString = productControl.Price; string vendorAsString = productControl.Vendor; string measureAsString = productControl.Measure; string categoryAsString = productControl.Category; decimal price; int vendor; int measure; int category; if (name == string.Empty) { throw new ArgumentNullException("Product name is empty."); } if (!decimal.TryParse(priceAsString, out price)) { throw new FormatException("Product price is not valid." + Environment.NewLine + "Decimal separator is '" + DecimalSeparator + "'."); } if (!int.TryParse(vendorAsString, out vendor)) { throw new FormatException("Product vendor is not valid"); } if (!int.TryParse(measureAsString, out measure)) { throw new FormatException("Product measure is not valid"); } if (!int.TryParse(categoryAsString, out category)) { throw new FormatException("Product category is not valid"); } int result = -1; OracleDBAction oracleAction = new OracleDBAction(); result = oracleAction.InsertNewProduct(commandString, name, price, vendor, measure, category); if (result == 1) { MessageBox.Show(string.Format("Product {0} was added to Products.", name), Attention, MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (result == 0) { MessageBox.Show(string.Format("Product {0} was not added to Products. Check if this measure is already added.", name), Attention, MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (ArgumentNullException nullEx) { MessageBox.Show(nullEx.Message); } catch (FormatException fEx) { MessageBox.Show(fEx.Message); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void InsertVendor(object sender, EventArgs e) { string commandString = "INSERT INTO VENDORS (NAME) VALUES (:name)"; string newVendor = vendorControl.Vendor; if (newVendor != string.Empty) { int result = -1; try { OracleDBAction oracleAction = new OracleDBAction(); result = oracleAction.InsertProductParameter(commandString, ":name", newVendor); SelectData(); } catch (Exception ex) { MessageBox.Show(ex.Message, Attention, MessageBoxButtons.OK, MessageBoxIcon.Error); SelectData(); } if (result == 1) { MessageBox.Show(string.Format("Vendor {0} was added to Vendors.", newVendor), Attention, MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (result == 0) { MessageBox.Show(string.Format("Vendor {0} was not added to Vendors. Check if this measure is already added.", newVendor), Attention, MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show("Enter a vendor."); } }
private void InsertCategory(object sender, EventArgs e) { string commandString = "INSERT INTO CATEGORIES (NAME, PARENT_CATEGORY_ID) VALUES (:name, :category)"; string categoryName = categoryControl.Category; string categoryParent = categoryControl.ParentCategory; if (categoryName != string.Empty) { int result = -1; try { OracleDBAction oracleAction = new OracleDBAction(); result = oracleAction.InsertProductCategory(commandString, ":name", categoryName, ":category", categoryParent); SelectData(); categoryControl.SetParentCategorySource(categories); } catch (Exception ex) { MessageBox.Show(ex.Message, Attention, MessageBoxButtons.OK, MessageBoxIcon.Error); } if (result == 1) { MessageBox.Show(string.Format("Category {0} was added to Categories.", categoryName), Attention, MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (result == 0) { MessageBox.Show(string.Format("Category {0} was not added to Categories. Check if this measure is already added.", categoryName), Attention, MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show("Enter a category."); } }