private void btnDelete_Click(object sender, EventArgs e) { using (InventoryEntities invEntities = new InventoryEntities()) { /// ///delete transaction master /// var master = (transaction_master)invEntities.transaction_master.Where(m => m.transaction_master_id == TransactionMasterId).FirstOrDefault(); if (master != null) { /// /// delete transaction detail /// var details = from d in invEntities.transaction_detail.Where(m => m.transaction_master_id == TransactionMasterId) select d; invEntities.transaction_detail.RemoveRange(details); invEntities.SaveChanges(); invEntities.transaction_master.Remove(master); invEntities.SaveChanges(); } /// ///reset form value /// TransactionMasterId = Int64.MinValue; } }
private void InsertDate() { using (InventoryEntities invEntities = new InventoryEntities()) { var new_Master_Id = Int64.MinValue; #region insert or update master var udm = from m in invEntities.transaction_master where m.transaction_master_id == this.TransactionMasterId select m; transaction_master obj = udm.SingleOrDefault(); if (obj == null) obj = new transaction_master(); ///set data to object obj.transaction_code = txtTransaction_code.Text.Trim(); obj.transaction_type_id = 1; obj.transaction_date = dtpTransaction_date.Value; if ((Int64)cboReceived_warehouse.SelectedValue != Int64.MinValue) obj.received_warehouse_id = (Int64)cboReceived_warehouse.SelectedValue; obj.enabled = true; obj.receiver = txtReceiver.Text.Trim(); obj.deliver = txtDeliver.Text.Trim(); if ((Int64)cboSupplier.SelectedValue != Int64.MinValue) obj.supplier_id = (Int64)cboSupplier.SelectedValue; obj.created_date = DateTime.Now; obj.create_user = txtCreate_user.Text.Trim(); obj.update_date = DateTime.Now; obj.update_user = txtCreate_user.Text.Trim(); obj.description = txtDescription.Text.Trim(); if (obj.transaction_master_id == 0) invEntities.transaction_master.Add(obj); invEntities.SaveChanges(); new_Master_Id = obj.transaction_master_id; #endregion end insert master; #region insert detail and item for (int i = 0; i < dtTransaction_detail.Rows.Count; i++) { if (dtTransaction_detail.Rows[i].RowState == DataRowState.Added || dtTransaction_detail.Rows[i].RowState == DataRowState.Modified) { /// ///Insert or update item /// var temmId = string.IsNullOrEmpty(dtTransaction_detail.Rows[i][colItemId.ColumnName].ToString()) ? Int64.MinValue : (Int64)dtTransaction_detail.Rows[i][colItemId.ColumnName]; var udi = from it in invEntities.items where it.item_id == temmId select it; item objItem = udi.SingleOrDefault(); if (objItem == null) objItem = new item(); objItem.item_code = dtTransaction_detail.Rows[i][colItemCode.ColumnName].ToString(); objItem.item_name = dtTransaction_detail.Rows[i][colItemName.ColumnName].ToString(); objItem.unit_id = (Int64)dtTransaction_detail.Rows[i][colUnitId.ColumnName]; objItem.category_item_id = (Int64)dtTransaction_detail.Rows[i][colCategoryItemId.ColumnName]; objItem.specifications = dtTransaction_detail.Rows[i][colSpecifications.ColumnName].ToString(); objItem.features = dtTransaction_detail.Rows[i][colFeatures.ColumnName].ToString(); objItem.enabled = true; objItem.create_user = obj.create_user; objItem.update_user = obj.create_user; objItem.created_date = DateTime.Now; objItem.update_date = DateTime.Now; if (objItem.item_id == 0) invEntities.items.Add(objItem); invEntities.SaveChanges(); /// /// insert or update transaction_detail /// var detailId = string.IsNullOrEmpty(dtTransaction_detail.Rows[i][coltransaction_detail_id.DataPropertyName].ToString()) ? Int64.MinValue : (Int64)dtTransaction_detail.Rows[i][coltransaction_detail_id.DataPropertyName]; var udd = from d in invEntities.transaction_detail where d.transaction_detail_id == detailId select d; transaction_detail objDetail = udd.SingleOrDefault(); if (objDetail == null) objDetail = new transaction_detail(); objDetail.transaction_master_id = obj.transaction_master_id; objDetail.category_item_id = (Int64)dtTransaction_detail.Rows[i][colCategoryItemId.ColumnName]; objDetail.item_id = objItem.item_id; if (!string.IsNullOrEmpty(dtTransaction_detail.Rows[i][colQuantity.ColumnName].ToString())) objDetail.quantity = (decimal)dtTransaction_detail.Rows[i][colQuantity.ColumnName]; if (!string.IsNullOrEmpty(dtTransaction_detail.Rows[i][colUnitId.ColumnName].ToString())) objDetail.unit_id = (Int64)dtTransaction_detail.Rows[i][colUnitId.ColumnName]; objDetail.description = dtTransaction_detail.Rows[i][colDescription.ColumnName].ToString(); objDetail.create_user = obj.create_user; objDetail.update_user = obj.create_user; objDetail.created_date = DateTime.Now; objDetail.update_date = DateTime.Now; if (objDetail.transaction_detail_id == 0) { invEntities.transaction_detail.Add(objDetail); } invEntities.SaveChanges(); } else if (dtTransaction_detail.Rows[i].RowState == DataRowState.Deleted) { } } #endregion end insert detail and item #region reload data if (new_Master_Id != TransactionMasterId) { this.TransactionMasterId = new_Master_Id; } else { getDataCombo(); getDataComboForGridView(); GetDataById(); } #endregion end reload data } }