private void btnSave_Click(object sender, EventArgs e) { if (itemsId.Length == 1) { txtCost.Text = Format.formatToPeso(cost); String typeId = null, fromId = null; if (String.IsNullOrEmpty(cbType.Text)) { cbType.Focus(); } else if (String.IsNullOrEmpty(cbFrom.Text)) { cbFrom.Focus(); } else { DialogResult result = MessageBox.Show(null, "You want to save the changes?", "Save Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == DialogResult.No) { return; } //Type if (cbType.SelectedIndex < 0) { types.TypeName = cbType.Text; Database.set("SELECT type_id FROM tbltype WHERE type_name = @typeName;", new String[] { cbType.Text }); String id = Database.executeString(); if (String.IsNullOrEmpty(id)) { if (types.addType()) { typeId = types.getLastinserted(); cbType.Items.Add(new ComboBoxItem(cbType.Text, typeId)); cbType.SelectedIndex = cbType.Items.Count - 1; } } else { typeId = id; } } else { typeId = ((ComboBoxItem)cbType.SelectedItem).HiddenValue; } //From if (cbFrom.SelectedIndex < 0) { from.FromName = cbFrom.Text; if (from.addFrom()) { fromId = from.getLastInserted(); cbFrom.Items.Add(new ComboBoxItem(cbFrom.Text, fromId)); cbFrom.SelectedIndex = cbFrom.Items.Count - 1; } } else { fromId = ((ComboBoxItem)cbFrom.SelectedItem).HiddenValue; } using (MySql.Data.MySqlClient.MySqlConnection connection = Database.getConnection()) { try { connection.Open(); int isDonated = 0; if (rbDonated.Checked == true) { isDonated = 1; } else { isDonated = 0; } //format date procured String dateProcured = dtpProcured.Value.ToString("yyyy-MM-dd"); string itemId = itemsId[0]; if (depreciationId == "") { depreciationId = null; } String sql = "UPDATE tblitem SET isdonated = @isDonated,price = @cost,details = @details,depreciation_id = @deprecId, date_procured = @dateProcured, from_id = @fromId,property_no = @propertyNo WHERE item_id = @itemId;"; MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand(sql, connection); command.Parameters.AddWithValue("@isDonated", isDonated); command.Parameters.AddWithValue("@cost", Format.formatToDecimal(txtCost.Text)); command.Parameters.AddWithValue("@details", rtDetails.Text); command.Parameters.AddWithValue("@deprecId", depreciationId); command.Parameters.AddWithValue("@dateProcured", dateProcured); command.Parameters.AddWithValue("@fromId", fromId); command.Parameters.AddWithValue("@propertyNo", txtPropertyNo.Text); command.Parameters.AddWithValue("@itemId", itemId); if (command.ExecuteNonQuery() == 1) { Database.set("SELECT itemtype_id FROM tblitemtype WHERE item_id = @itemId;", new String[] { itemId }); String itemtypeid = Database.executeString(); if (String.IsNullOrEmpty(itemtypeid)) { Database.set("INSERT INTO tblitemtype VALUES (null,@typeId,@itemId);", new String[] { typeId, itemId }); } else { Database.set("UPDATE tblitemtype SET type_id = @typeId WHERE item_id = @itemId;", new String[] { typeId, itemId }); } if (Database.executeNonQuery()) { Database.set("UPDATE tblbagitem SET property_no = @propertyNo WHERE item_id = @itemId;", new String[] { txtPropertyNo.Text, itemId }); if (Database.executeNonQuery()) { Database.set("UPDATE tbldispose SET property_no = @propertyNo WHERE item_id = @itemId;", new String[] { txtPropertyNo.Text, itemId }); if (Database.executeNonQuery()) { Database.set("UPDATE tblinventory SET property_no = @propertyNo WHERE item_id = @itemId;", new String[] { txtPropertyNo.Text, itemId }); if (Database.executeNonQuery()) { Database.set("UPDATE tbltechbag SET property_no = @propertyNo WHERE item_id = @itemId;", new String[] { txtPropertyNo.Text, itemId }); if (Database.executeNonQuery()) { MessageBox.Show(null, "Changes in the item was Successfully saved.", "Save Changes", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); inventory.loadItems(); } } } } } } } catch (Exception exception) { MessageBox.Show(exception.Message); } finally { connection.Close(); } } } } //Multiple update else { if (String.IsNullOrEmpty(cbType.Text)) { cbType.Focus(); } else { DialogResult result = MessageBox.Show(null, "You want to save the changes?", "Save Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == DialogResult.No) { return; } String typeId = null; //Type if (cbType.SelectedIndex < 0) { types.TypeName = cbType.Text; Database.set("SELECT type_id FROM tbltype WHERE type_name = @typeName;", new String[] { cbType.Text }); String id = Database.executeString(); if (String.IsNullOrEmpty(id)) { if (types.addType()) { typeId = types.getLastinserted(); cbType.Items.Add(new ComboBoxItem(cbType.Text, typeId)); cbType.SelectedIndex = cbType.Items.Count - 1; } } else { typeId = id; } } else { typeId = ((ComboBoxItem)cbType.SelectedItem).HiddenValue; } foreach (String itemId in itemsId) { Database.set("SELECT itemtype_id FROM tblitemtype WHERE item_id = @itemId;", new String[] { itemId }); String itemtypeid = Database.executeString(); if (String.IsNullOrEmpty(itemtypeid)) { Database.set("INSERT INTO tblitemtype VALUES (null,@typeId,@itemId);", new String[] { typeId, itemId }); } else { Database.set("UPDATE tblitemtype SET type_id = @typeId WHERE item_id = @itemId;", new String[] { typeId, itemId }); } Database.executeNonQuery(); } MessageBox.Show(null, "Changes of all items was Successfully saved.", "Save Changes", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); inventory.loadItems(); } } }
private void btnAddItem_Click(object sender, EventArgs e) { //Variables String typeId = null, fromId = null; String isDonated = null; String dateProcured = null; if (String.IsNullOrEmpty(cbType.Text)) { cbType.Focus(); } else if (String.IsNullOrEmpty(cbFrom.Text)) { cbFrom.Focus(); } else if (String.IsNullOrEmpty(rtDetails.Text)) { rtDetails.Focus(); } else if (String.IsNullOrEmpty(txtPropertyNo.Text)) { txtPropertyNo.Focus(); } else { DialogResult result = MessageBox.Show(null, "Are you sure you want to add this item?", "Add item", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == DialogResult.No) { return; } //Type if (cbType.SelectedIndex < 0) { types.TypeName = cbType.Text; if (types.addType()) { typeId = types.getLastinserted(); cbType.Items.Add(new ComboBoxItem(cbType.Text, typeId)); cbType.SelectedIndex = cbType.Items.Count - 1; } } else { typeId = ((ComboBoxItem)cbType.SelectedItem).HiddenValue; } //From if (cbFrom.SelectedIndex < 0) { from.FromName = cbFrom.Text; if (from.addFrom()) { fromId = from.getLastInserted(); cbFrom.Items.Add(new ComboBoxItem(cbFrom.Text, fromId)); cbFilterFrom.Items.Add(new ComboBoxItem(cbFrom.Text, fromId)); cbFrom.SelectedIndex = cbFrom.Items.Count - 1; } } else { fromId = ((ComboBoxItem)cbFrom.SelectedItem).HiddenValue; } //IsDonated if (rbDonated.Checked == true) { isDonated = "1"; } else { isDonated = "0"; } //format date procured dateProcured = dtpProcured.Value.ToString("yyyy-MM-dd"); //Add item if (items.addItem(isDonated, price.ToString(), rtDetails.Text, depreciationId, dateProcured, fromId, txtPropertyNo.Text)) { if (types.insertItemType(typeId, items.getLastInserted())) { MessageBox.Show(null, "New item was successfully added.", "Add item", MessageBoxButtons.OK, MessageBoxIcon.Information); loadItems(); txtCost.Enabled = true; btnDepreciation.BackColor = Color.Transparent; btnDepreciation.Enabled = true; depreciationId = null; rtDetails.Text = string.Empty; txtCost.Text = "0.00"; txtPropertyNo.Text = String.Empty; } } } }