private void btnAdd_Click(object sender, EventArgs e) { string dateConsumption = month.ToString() + "-" + year.ToString(); if (txtBoxCustomerID.Text != "" && txtBoxCurrentReading.Text != "") { int amount = Convert.ToInt32(txtBoxPrice.Text.Replace(",", "")); if (amount >= 0) { Consumption consumptionEntity = new Consumption { Month = month, Year = year, CurrentReading = Convert.ToInt32(txtBoxCurrentReading.Text), PreviousReading = Convert.ToInt32(txtBoxPreviousReading.Text), MeterID = meterID, ConsumptionEnergy = Convert.ToInt32(txtBoxEnergyUsage.Text), UnitPrice = Convert.ToInt32(txtUnitPrice.Text.Replace(",", "")), Date = Convert.ToDateTime(dateConsumption) }; consumptionService.InsertConsumption(consumptionEntity); if (consumptionEntity.ID > 0) { LoadData(); TotalAmount(); txtBoxCustomerID.Clear(); txtBoxCurrentReading.Clear(); txtBoxCustomerName.Clear(); txtBoxEnergyUsage.Clear(); txtBoxPreviousReading.Clear(); //txtUnitPrice.Clear(); txtBoxPrice.Clear(); } } else { MessageBox.Show("Total Amount cannot be negative", "Error!", MessageBoxButtons.OK); } } else { MessageBox.Show("Customer ID and Current Reading cannot be empty!", "Error!", MessageBoxButtons.OK); } }
private void btnSave_Click(object sender, EventArgs e) { if (STATUS == status.Insert) { if (txtBoxCustomerID.Text != "" && txtBoxCustomerName.Text != "" && txtBoxUnitPrice.Text != "" && txtBoxReading.Text != "" && txtBoxPrevReading1.Text != "") { int previousReading = 0; int currentReading = 0; bool n1 = Int32.TryParse(txtBoxPrevReading1.Text, out previousReading); bool n2 = Int32.TryParse(txtBoxReading.Text, out currentReading); if (n1 && n2 && (previousReading < currentReading)) { Customer customerEntity = new Customer { CustomerKey = txtBoxCustomerID.Text, CustomerName = txtBoxCustomerName.Text, Meter = new Meter { MeterKey = txtBoxCustomerID.Text } }; customerService.InsertCustomer(customerEntity); if (customerEntity.ID > 0) { string dateConsumption = month.ToString() + "-" + year.ToString(); // insert new consumption Consumption consumptionEntity = new Consumption { Month = month, Year = year, CurrentReading = Convert.ToInt16(txtBoxReading.Text), PreviousReading = Convert.ToInt16(txtBoxPrevReading1.Text), MeterID = customerEntity.ID, ConsumptionEnergy = Convert.ToInt16(txtBoxReading.Text) - Convert.ToInt16(txtBoxPrevReading1.Text), UnitPrice = Convert.ToInt16(txtBoxUnitPrice.Text.Replace(",", "")), Date = Convert.ToDateTime(dateConsumption) }; consumptionService.InsertConsumption(consumptionEntity); // load gridview LoadData(); PassData2dgvDeail(); // clear text box txtBoxCustomerID.Clear(); txtBoxCustomerName.Clear(); txtBoxReading.Clear(); FillNo(); } } else { MessageBox.Show("Current Reading must be greater thang Previous Reading", "Error!", MessageBoxButtons.OK); } } else { MessageBox.Show("Customer name or Customer Id cannot be emptpy!", "Error!", MessageBoxButtons.OK); } } if (STATUS == status.Update) { if (txtBoxCustomerID.Text != "" && txtBoxCustomerName.Text != "") { Customer customerEntity = customerService.GetCustomer(id); customerEntity.CustomerKey = txtBoxCustomerID.Text; customerEntity.CustomerName = txtBoxCustomerName.Text; customerEntity.Meter.MeterKey = txtBoxCustomerID.Text; customerService.UpdateCustomer(customerEntity); if (customerEntity.ID > 0) { LoadData(); FillNo(); } } else { MessageBox.Show("Customer name or Customer Id cannot be emptpy!", "Error!", MessageBoxButtons.OK); } } }