WinForms - Entity Framework: Stellar Water Point
WinForms - Entity Framework: Stellar Water Point
Project Start Up
Introduction
This exercise is to create a graphical application for a (fictitious) water company. The application will be created as Windows Forms (WinForms). Probably the most important part, as far as programming is concerned, is the use of the Entity Framework.
Practical Learning: Introducing the Application
namespace StellarWaterPoint52.Models
{
internal static class MsgBox
{
public static void Regular(string message)
{
MsgBox.Regular(message,
"Stellar Water Point",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
public static DialogResult Question(string message)
{
DialogResult result = MsgBox.Regular(message,
"Stellar Water Point",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question);
switch (result)
{
case DialogResult.Yes:
return DialogResult.Yes;
case DialogResult.No:
return DialogResult.No;
case DialogResult.Cancel:
return DialogResult.Cancel;
default:
return DialogResult.OK;
}
}
}
}Introducing the Entity Framework
To create and manage the records of our application, we will use the Entity Framework. If you are working on a Windows Forms application, you must install that framework as a NuGet package.
Practical Learning: Installing the Entity Framework
using System.Data.Entity;
namespace StellarWaterPoint.Models
{
public class StellarContext : DbContext
{
}
}The Main Form of the Application
When you create a Windows Forms application in Microsoft Visual Studio, the studio automatically creates a default or starting form. We will configure that form to be the central object of the application.
Practical Learning: Preparing the Main Form of the Application
Water Meters
Introduction
The business for the type of application we are creating must use water meters to measure the amount of water that is consumed. We will create forms that can be used to manage water meters.
Practical Learning: Displaying Water Meters
using System.ComponentModel.DataAnnotations;
namespace StellarWaterPoint51.Models
{
public class WaterMeter
{
[Key]
public int WaterMeterId { get; set; }
public string? MeterNumber { get; set; }
public string? Make { get; set; }
public string? Model { get; set; }
public string? MeterSize { get; set; }
}
}using System.Data.Entity;
namespace StellarWaterPoint51.Models
{
public class StellarContext : DbContext
{
public DbSet<WaterMeter>? WaterMeters { get; set; }
}
}Displaying Water Meters
Eventually, when the records of water meters will have been created, if the user wants to see a list of water meters, we will create a form for that purpose.
Practical Learning: Displaying Water Meters
| (Name) | Text | Width | TextAlign |
| colWaterMeterId | Id | 40 | |
| colMeterNumber | Meter # | 150 | Center |
| colMake | Make | 300 | |
| colModel | Model | 150 | |
| colMeterSize | Meter Size | 150 |

| Control | (Name) | Other Properties | |
| ListView | lvwWaterMeters | FullRowSelect: True GridLines: True View: Details |
|
using StellarWaterPoint51.Models;
namespace StellarWaterPoint51.WaterMeters
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowWaterMeters()
{
StellarContext db = new();
List<WaterMeter> meters = new();
meters = db.WaterMeters!.ToList();
lvwWaterMeters.Items.Clear();
foreach (WaterMeter meter in meters)
{
ListViewItem lviWaterMeter = new ListViewItem(meter.WaterMeterId.ToString());
lviWaterMeter.SubItems.Add(meter.MeterNumber);
lviWaterMeter.SubItems.Add(meter.Make);
lviWaterMeter.SubItems.Add(meter.Model);
lviWaterMeter.SubItems.Add(meter.MeterSize);
lvwWaterMeters.Items.Add(lviWaterMeter);
}
}
private void Central_Load(object sender, EventArgs e)
{
ShowWaterMeters();
}
}
}| Control | (Name) | Text | Font | |
| Button | btnWaterMeters | &Water Meters... | Times New Roman, 24pt, style=Bold | |
namespace StellarWaterPoint2
{
public partial class StellarManagement : Form
{
public StellarManagement()
{
InitializeComponent();
}
private void btnWaterMeters_Click(object sender, EventArgs e)
{
WaterMeters.Central central = new WaterMeters.Central();
central.Show();
}
}
}A Water Meter Record
For the type of business whose application we are creating, every water meter must be represented with a record. We will have a form that can be used to create a record for each water meter.
Practical Learning: Creating a Water Meter Record

| Control | (Name) | Text | Other Properties | |
| Label | &Meter #: | |||
| MaskedTextBox | mtbMeterNumber | Masked: 000-000-000 | ||
| Label | M&ake: | |||
| TextBox | txtMake | |||
| Label | M&odel: | |||
| TextBox | txtModel | |||
| Label | Me&ter Size: | |||
| TextBox | txtMeterSize | |||
| Button | btnOK | &OK | ||
| Button | btnCancel | &Cancel | ||
FormBorderStyle: FixedDialog Text: Stellar Water Point - Create Water Meter StartPosition: CenterScreen ShowInTaskbar: False

| Control | (Name) | Text | |
| ListView | No Change | No Change | |
| Button | btnCreateWaterMeter | &Create Water Meter... | |
using StellarWaterPoint51.Models;
namespace StellarWaterPoint51.WaterMeters
{
public partial class Central : Form
{
StellarContext db = new();
public Central()
{
InitializeComponent();
}
private void ShowWaterMeters()
{
List<WaterMeter> meters = new();
meters = db.WaterMeters!.ToList();
lvwWaterMeters.Items.Clear();
foreach (WaterMeter meter in meters)
{
ListViewItem lviWaterMeter = new ListViewItem(meter.WaterMeterId.ToString());
lviWaterMeter.SubItems.Add(meter.MeterNumber);
lviWaterMeter.SubItems.Add(meter.Make);
lviWaterMeter.SubItems.Add(meter.Model);
lviWaterMeter.SubItems.Add(meter.MeterSize);
lvwWaterMeters.Items.Add(lviWaterMeter);
}
}
private void Central_Load(object sender, EventArgs e)
{
ShowWaterMeters();
}
private void btnNewWaterMeter_Click(object sender, EventArgs e)
{
Create create = new();
create.ShowDialog();
ShowWaterMeters();
}
}
}private void btnCreateWaterMeter_Click(object sender, EventArgs e)
{
Create create = new Create();
if (create.ShowDialog() == DialogResult.OK)
{
string strMeterNumber = create.mtbMeterNumber.Text.Replace("-", "").Replace(" ", "").Trim();
if (string.IsNullOrEmpty(strMeterNumber))
{
MsgBox.Regular("You must type a meter number. " +
"Otherwise, the water meter cannot be set up.");
return;
}
StellarContext db = new();
WaterMeter meter = new WaterMeter()
{
MeterNumber = create.mtbMeterNumber.Text,
Make = create.txtMake.Text,
Model = create.txtModel.Text,
MeterSize = create.txtMeterSize.Text
};
db.WaterMeters!.Add(meter);
db.SaveChanges();
}
ShowWaterMeters();
}


| Meter # | Make | Model | Meter Size |
| 392-494-572 | Constance Technologies | TG-4822 | 5/8 Inches |
| 938-705-869 | Stan Wood | 66-G | 1 Inch |
| 588-279-663 | Estellano | NCF-226 | 3/4 Inches |

Water Meter Details
To let the user view the details of a water meter, we will create a simple form.
Practical Learning: Creating a Water Meter Record

| Control | (Name) | Text | Enabled | Modifiers | Other Properties | |
| Label | &Meter #: | |||||
| MaskedTextBox | mtbMeterNumber | False | Public | Masked: 000-000-000 | ||
| Button | btnFindWaterMeter | &Find Water Meter | ||||
| Label | Make: | |||||
| TextBox | txtMake | False | Public | |||
| Label | Model: | |||||
| TextBox | txtModel | False | Public | |||
| Label | Meter Size: | |||||
| TextBox | txtMeterSize | False | Public | |||
| Label | Meter Size: | |||||
| TextBox | txtMeterSize | False | Public | |||
| Button | btnClose | &Close | ||||
using StellarWaterPoint14.Models;
namespace StellarWaterPoint14.WaterMeters
{
public partial class Details : Form
{
public Details()
{
InitializeComponent();
}
private void btnFindWaterMeter_Click(object sender, EventArgs e)
{
try
{
string strMeterNumber = mtbMeterNumber.Text.Replace("-", "").Replace(" ", "");
if (string.IsNullOrEmpty(strMeterNumber))
{
MsgBox.Regular("You should first type a valid meter number, " +
"and then click the Find Water Meter button.");
return;
}
StellarContext db = new();
List<WaterMeter> meters = new();
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text);
txtWaterMeterId.Text = meter!.WaterMeterId.ToString();
txtMake.Text = meter.Make;
txtModel.Text = meter.Model;
txtMeterSize.Text = meter.MeterSize;
}
catch (NullReferenceException nre)
{
MsgBox.Regular("The number you typed doesn't match " +
"any of the water meters in our system." + Environment.NewLine +
"The error produced was: " + nre.Message);
}
}
}
}uusing StellarWaterPoint14.Models;
namespace StellarWaterPoint14.WaterMeters
{
public partial class Details : Form
{
public Details()
{
InitializeComponent();
}
private void btnFindWaterMeter_Click(object sender, EventArgs e)
{
// Use Exception Handling in case something would go wrong
try
{
/* We want to check that the user had provided a water meter number.
* Since we are using a masked text box, and since its internal symbols
* are counted as being part of the whole value, we need to remove those
* symbols. We will also "trim" the value to remove the empty spaces. */
string strMeterNumber = mtbMeterNumber.Text.Replace("-", "").Replace(" ", "").Trim();
// Make sure the user typed a (valid) water meter number.
if (string.IsNullOrEmpty(strMeterNumber))
{
MsgBox.Regular("You should first type a valid meter number, " +
"and then click the Find Water Meter button.");
// If the user didn't type a water meter number, don't do nothing.
return;
}
// Get a reference to the EF context class
StellarContext db = new();
// Get a list that can contain the records of water meters
List<WaterMeter> meters = new();
// If some records of water meters exist, access them and store them in the List<> variable
meters = db.WaterMeters!.ToList();
/* If a list of water meters exists, find in it the water meter that has
* the same meter number as the value the user had typed in the masked text box. */
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text);
// If the water meter was found, put its values in the text boxes
txtWaterMeterId.Text = meter!.WaterMeterId.ToString();
txtMake.Text = meter.Make;
txtModel.Text = meter.Model;
txtMeterSize.Text = meter.MeterSize;
}
catch (NullReferenceException nre)
{
/* If the user typed some digits inthe water meter masked text box, find out whether
* that number corresponds to a valid water meter. If the number the user typed doesn't match
* any water meter number, then the application produced a NullReferenceException exception. */
MsgBox.Regular("The number you typed doesn't match " +
"any of the water meters in our system." + Environment.NewLine +
"The error produced was: " + nre.Message);
}
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}| Control | (Name) | Other Properties | |
| ListView | No Change | No Change | |
| Button | No Change | No Change | |
| Button | btnWaterMeterDetails | &View Water Meter... | |
private void btnDetailsWaterMeter_Click(object sender, EventArgs e)
{
Details details = new();
details.Show();
}


Updating a Water Meter
If or when some detail about a water meter changes, we will create a form that can let a user update any piece of information about a water meter.
Practical Learning: Updating a Water Meter

| Control | (Name) | Text | Enabled | Other Properties | |
| Label | &Meter #: | ||||
| MaskedTextBox | mtbMeterNumber | False | Masked: 000-000-000 | ||
| Button | btnFindWaterMeter | &Find Water Meter | |||
| Label | Make: | ||||
| TextBox | txtMake | False | |||
| Label | Model: | ||||
| TextBox | txtModel | False | |||
| Label | Meter Size: | ||||
| TextBox | txtMeterSize | False | |||
| Button | btnFindWateMeter | &Find Wate Meter | |||
| Button | btnClose | &Close | |||
using StellarWaterPoint51.Models;
namespace StellarWaterPoint51.WaterMeters
{
public partial class Editor : Form
{
public Editor()
{
InitializeComponent();
}
private void btnFindWateMeter_Click(object sender, EventArgs e)
{
try {
string strMeterNumber = mtbMeterNumber.Text.Replace("-", "").Trim();
if (string.IsNullOrEmpty(strMeterNumber))
{
MsgBox.Regular("You should first type a valid meter number, " +
"and then click the Find Water Meter button.");
return;
}
StellarContext db = new();
List<WaterMeter> meters = new();
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text);
txtWaterMeterId.Text = meter!.WaterMeterId.ToString();
txtMake.Text = meter!.Make;
txtModel.Text = meter.Model;
txtMeterSize.Text = meter.MeterSize;
}
catch (NullReferenceException nre)
{
MsgBox.Regular("The number you typed doesn't match " +
"any of the water meters in our system." + Environment.NewLine +
"The error produced was: " + nre.Message);
}
}
}
}private void btnUpdateWaterMeter_Click(object sender, EventArgs e)
{
string strMeterNumber = mtbMeterNumber.Text.Replace("-", "").Replace(" ", "").Trim();
if (string.IsNullOrEmpty(strMeterNumber))
{
MsgBox.Regular("You must type a meter number. " +
"Otherwise, the water meter cannot be set up.");
return;
}
StellarContext db = new();
List<WaterMeter> meters = new();
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text);
if (meter is null)
{
MsgBox.Regular("There is no water meter with that number in our system.");
return;
}
meter!.Make = txtMake.Text;
meter.Model = txtModel.Text;
meter.MeterSize = txtMeterSize.Text;
db.SaveChanges();
Close();
}using StellarWaterPoint51.Models;
namespace StellarWaterPoint51.WaterMeters
{
public partial class Editor : Form
{
public Editor()
{
InitializeComponent();
}
private void btnFindWateMeter_Click(object sender, EventArgs e)
{
try {
/* We want to check that the user had provided a water meter number.
* Since we are using a masked text box, and since its internal symbols
* are counted as being part of the whole value, we need to remove those
* symbols. We will also "trim" the value to remove the empty spaces. */
string strMeterNumber = mtbMeterNumber.Text.Replace("-", "").Trim();
// Make sure the user typed a (valid) water meter number.
if (string.IsNullOrEmpty(strMeterNumber))
{
MsgBox.Regular("You should first type a valid meter number, " +
"and then click the Find Water Meter button.");
// If the user didn't type a water meter number, don't do nothing.
return;
}
// Get a reference to the EF context class
StellarContext db = new();
// Get a list that can contain the records of water meters
List<WaterMeter> meters = new();
// If some records of water meters exist, access them and store them in the List<> variable
meters = db.WaterMeters!.ToList();
/* If a list of water meters exists, find in it the water meter that has
* the same meter number as the value the user had typed in the masked text box. */
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text);
// If the water meter was found, put its values in the text boxes
txtWaterMeterId.Text = meter!.WaterMeterId.ToString();
txtMake.Text = meter!.Make;
txtModel.Text = meter.Model;
txtMeterSize.Text = meter.MeterSize;
}
catch (NullReferenceException nre)
{
MsgBox.Regular("The number you typed doesn't match " +
"any of the water meters in our system." + Environment.NewLine +
"The error produced was: " + nre.Message);
}
}
private void btnUpdateWaterMeter_Click(object sender, EventArgs e)
{
/* We want to check that the user had provided a water meter number.
* Since we are using a masked text box, and since its internal symbols
* are counted as being part of the whole value, we need to remove those
* symbols. We will also "trim" the value to remove the empty spaces. */
string strMeterNumber = mtbMeterNumber.Text.Replace("-", "").Trim();
/* Make sure the user provides a meter number
* as the minimum piece of information for a water meter. */
if (string.IsNullOrEmpty(strMeterNumber))
{
MsgBox.Regular("You must type a meter number. " +
"Otherwise, the water meter cannot be set up.");
return;
}
// Declare a variable for the Entity Framework database context
StellarContext db = new();
// Get a list that can contain the records of water meters
List<WaterMeter> meters = new();
// If some records of water meters exist, access them and store them in the List<> variable
meters = db.WaterMeters!.ToList();
/* If a list of water meters exists, find in it the water meter that has
* the same meter number as the value the user had typed in the masked text box. */
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text);
if (meter is null)
{
MsgBox.Regular("There is no water meter with that number in our system.");
return;
}
meter!.Make = txtMake.Text;
meter.Model = txtModel.Text;
meter.MeterSize = txtMeterSize.Text;
// Save the record to the database
db.SaveChanges();
Close();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}| Control | (Name) | Other Properties | |
| ListView | No Change | No Change | |
| Button | No Change | No Change | |
| Button | No Change | No Change | |
| Button | btnEditWaterMeter | &Edit Water Meter... | |
using System.Text.Json;
using StellarWaterPoint2.Models;
namespace StellarWaterPoint2.WaterMeters
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowWaterMeters()
{
. . .
}
private void Central_Load(object sender, EventArgs e)
{
ShowWaterMeters();
}
private void btnNewWaterMeter_Click(object sender, EventArgs e)
{
. . .
}
private void btnViewWaterMeter_Click(object sender, EventArgs e)
{
Details view = new();
view.ShowDialog();
ShowWaterMeters();
}
private void btnEditWaterMeter_Click(object sender, EventArgs e)
{
Editor editor = new();
editor.ShowDialog();
ShowWaterMeters();
}
}
}

| Make: | Stanford Trend | Model: | 266G |
| Meter Size: | 1 1/2 Inches |


Removing a Water Meter from the Database
When a user decides to delete the record of a water meter, we will provide a form for such an operation.
Practical Learning: Deleting a Water Meter Record

| Control | (Name) | Text | Enabled | Other Properties | |
| Label | &Meter #: | ||||
| MaskedTextBox | mtbMeterNumber | False | Masked: 000-000-000 | ||
| Button | btnFindWaterMeter | &Find Water Meter | |||
| Label | Make: | ||||
| TextBox | txtMake | False | |||
| Label | Model: | ||||
| TextBox | txtModel | False | |||
| Label | Meter Size: | ||||
| TextBox | txtMeterSize | False | |||
| Button | btnDeleteWaterMeter | &Delete Water Meter | |||
| Button | btnClose | &Close | |||
using StellarWaterPoint51.Models;
namespace StellarWaterPoint51.WaterMeters
{
public partial class Delete : Form
{
public Delete()
{
InitializeComponent();
}
private void btnFindWateMeter_Click(object sender, EventArgs e)
{
try {
string strMeterNumber = mtbMeterNumber.Text.Replace("-", "").Trim();
if (string.IsNullOrEmpty(strMeterNumber))
{
MsgBox.Regular("You should first type a valid meter number, " +
"and then click the Find Water Meter button.");
return;
}
StellarContext db = new();
List<WaterMeter> meters = new();
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text);
txtMake.Text = meter!.Make;
txtModel.Text = meter.Model;
txtMeterSize.Text = meter.MeterSize;
}
catch (NullReferenceException nre)
{
MsgBox.Regular("The number you typed doesn't match " +
"any of the water meters in our system." + Environment.NewLine +
"The error produced was: " + nre.Message);
}
}
}
}using StellarWaterPoint51.Models;
namespace StellarWaterPoint51.WaterMeters
{
public partial class Delete : Form
{
public Delete()
{
InitializeComponent();
}
private void btnFindWateMeter_Click(object sender, EventArgs e)
{
. . .
}
private void btnDeleteWaterMeter_Click(object sender, EventArgs e)
{
try {
string strMeterNumber = mtbMeterNumber.Text.Replace("-", "").Trim();
if (string.IsNullOrEmpty(strMeterNumber))
{
MsgBox.Regular("You must type a meter number. " +
"Otherwise, the water meter cannot be set up.");
return;
}
StellarContext db = new();
List<WaterMeter> meters = new();
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text);
// Check if the user had provided a valid meter number for an existing water meter
if (meter is null)
{
MsgBox.Regular("You must provide a valid water meter number that exists in our system.");
return;
}
/* If the user had put a valid water meter, ask the user if
* he/she really wants to delete the water meter record. */
string strQuestion = "Are you sure you want to delete this " +
"water meter (the operation cannot be undone)?";
if (MsgBox.Question(strQuestion) == DialogResult.Yes)
{
// If the user answered Yes to the question, delete the water meter record...
db.WaterMeters!.Remove(meter);
// then save the table without the deleted record.
db.SaveChanges();
}
// Return to the Central form of the Water Meters
Close();
}
catch (NullReferenceException nre)
{
MsgBox.Regular("The number you typed doesn't match " +
"any of the water meters in our system." + Environment.NewLine +
"The error produced was: " + nre.Message);
}
Close();
}
}
}using StellarWaterPoint51.Models;
namespace StellarWaterPoint51.WaterMeters
{
public partial class Delete : Form
{
public Delete()
{
InitializeComponent();
}
private void btnFindWateMeter_Click(object sender, EventArgs e)
{
/* We want to check that the user had provided a water meter number.
* Since we are using a masked text box, and since its internal symbols
* are counted as being part of the whole value, we need to remove those
* symbols. We will also "trim" the value to remove the empty spaces. */
string strMeterNumber = mtbMeterNumber.Text.Replace("-", "").Trim();
// Make sure the user typed a (valid) water meter number.
if (string.IsNullOrEmpty(strMeterNumber))
{
MsgBox.Regular("You should first type a valid meter number, " +
"and then click the Find Water Meter button.");
// If the user didn't type a water meter number, don't do nothing.
return;
}
// Get a reference to the EF context class
StellarContext db = new();
// Get a list that can contain the records of water meters
List<WaterMeter> meters = new();
// If some records of water meters exist, access them and store them in the List<> variable
meters = db.WaterMeters!.ToList();
/* If a list of water meters exists, find in it the water meter that has
* the same meter number as the value the user had typed in the masked text box. */
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text);
// If the water meter was found, put its values in the text boxes
txtWaterMeterId.Text = meter!.WaterMeterId.ToString();
txtMake.Text = meter.Make;
txtModel.Text = meter.Model;
txtMeterSize.Text = meter.MeterSize;
}
private void btnDeleteWaterMeter_Click(object sender, EventArgs e)
{
string strMeterNumber = mtbMeterNumber.Text.Replace("-", "").Trim();
if (string.IsNullOrEmpty(strMeterNumber))
{
MsgBox.Regular("If you are planning to delete a water meter record, " +
"first type a valid meter number, " +
"then click the Find Water Meter button, " +
"and the click the Delete Water Meter button.");
// If the user didn't type a water meter number, don't do anything.
return;
}
StellarContext db = new();
List<WaterMeter> meters = new();
// Get the list of water meters and store it in a List<WaterMeter> collection named "meters"
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text);
// Check if the user had provided a valid meter number for an existing water meter
if (meter is not null)
{
/* If the user had put a valid water meter, ask the user if
* he/she really wants to delete the water meter record. */
string strQuestion = "Are you sure you want to delete this water meter (the operation cannot be undone)?";
if (MsgBox.Regular(strQuestion, "Stellar Water Point",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question) == DialogResult.Yes)
{
// If the user answered Yes to the question, delete the water meter record...
db.WaterMeters!.Remove(meter);
// then save the table without the deleted record.
db.SaveChanges();
}
}
// Return to the Central form of the Water Meters
Close();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}| Control | (Name) | Text | Anchor | |
| ListView | lvwWaterMeters | Top, Bottom, Left, Right | ||
| Button | btnNewWaterMeter | &New Water Meter... | FlatStyle: Flat Bottom, Right |
|
| Button | btnViewWaterMeter | &View Water Meter... | FlatStyle: Flat Bottom, Right |
|
| Button | btnEditWaterMeter | &Edit Water Meter... | FlatStyle: Flat Bottom, Right |
|
| Button | btnDeleteWateMeter | &Delete Water Meter... | FlatStyle: Flat Bottom, Right |
|
| Button | btnClose | &Close | FlatStyle: Flat Bottom, Right |
|
using StellarWaterPoint51.Models;
namespace StellarWaterPoint51.WaterMeters
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowWaterMeters()
{
// Get a reference to the database context
StellarContext db = new();
// Prepare a collection variable to water meters
List<WaterMeter> meters = new();
// Get a list of water meters and store in a declared collection variable
meters = db.WaterMeters!.ToList();
/* Whenever you are plaaning to display some records in the list view,
* first remove everything from that list box as if you are refreshing it. */
lvwWaterMeters.Items.Clear();
// Visit each water meter record...
foreach (WaterMeter meter in meters)
{
// ... and create its record as a list view item
ListViewItem lviWaterMeter = new ListViewItem(meter.WaterMeterId.ToString());
lviWaterMeter.SubItems.Add(meter.MeterNumber);
lviWaterMeter.SubItems.Add(meter.Make);
lviWaterMeter.SubItems.Add(meter.Model);
lviWaterMeter.SubItems.Add(meter.MeterSize);
// Add the prepared list view to the items of the list view
lvwWaterMeters.Items.Add(lviWaterMeter);
}
}
private void Central_Load(object sender, EventArgs e)
{
ShowWaterMeters();
}
private void btnNewWaterMeter_Click(object sender, EventArgs e)
{
Create create = new();
create.ShowDialog();
ShowWaterMeters();
}
private void btnDetailsWaterMeter_Click(object sender, EventArgs e)
{
Details details = new();
details.Show();
}
private void btnWaterMeterEditor_Click(object sender, EventArgs e)
{
Editor editor = new();
editor.ShowDialog();
ShowWaterMeters();
}
private void btnDeleteWaterMeter_Click(object sender, EventArgs e)
{
Delete delete = new();
delete.ShowDialog();
ShowWaterMeters();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}






private void btnClose_Click(object sender, EventArgs e)
{
// Close();
StellarContext db = new();
db.WaterMeters!.Add(new WaterMeter() { MeterNumber = "392-494-572", Make = "Constance Technologies", Model = "TG-4822", MeterSize = "5/8 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "938-725-869", Make = "Stanford Trend", Model = "266G", MeterSize = "1 1/2 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "588-279-663", Make = "Estellano", Model = "NCF-226", MeterSize = "4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "186-962-805", Make = "Lansome", Model = "2800", MeterSize = "1 1/2 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "379-386-979", Make = "Planetra", Model = "P-2020", MeterSize = "4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "580-742-825", Make = "Kensa Sons", Model = "KS2000A", MeterSize = "1 3/4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "849-351-444", Make = "Raynes Energica", Model = "a1088", MeterSize = "2 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "208-428-308", Make = "Constance Technologies", Model = "808D", MeterSize = "3/4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "738-588-249", Make = "Warrington", Model = "W4242", MeterSize = "5/8 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "496-813-794", Make = "Estellano", Model = "NCF-226", MeterSize = "3/4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "862-715-006", Make = "Warrington", Model = "W-4040", MeterSize = "1/2 Inch" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "649-358-184", Make = "Raynes Energica", Model = "b1700", MeterSize = "1 1/2 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "928-317-924", Make = "Gongola", Model = "GN1000", MeterSize = "2 Inch" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "595-753-147", Make = "Grass Grill", Model = "CRC-1000", MeterSize = "1 Inch" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "799-528-461", Make = "Kensa Sons", Model = "K-584-L", MeterSize = "3/4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "386-468-057", Make = "Estellano", Model = "NCF-226", MeterSize = "3/4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "938-275-294", Make = "Constance Technologies", Model = "TT-8822", MeterSize = "4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "288-427-585", Make = "Planetra", Model = "P-2020", MeterSize = "1/2 Inch" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "394-835-297", Make = "Raynes Energica", Model = "i2022", MeterSize = "3/4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "847-252-246", Make = "Master Stream", Model = "2000-MS", MeterSize = "1 1/2 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "349-725-848", Make = "Planetra", Model = "P-8000", MeterSize = "4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "713-942-058", Make = "Master Stream", Model = "3366-MS", MeterSize = "3/4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "747-581-379", Make = "Warrington", Model = "W4242", MeterSize = "5/8 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "582-755-263", Make = "Kensa Sons", Model = "KS2000A", MeterSize = "1 Inch" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "827-260-758", Make = "Raynes Energica", Model = "a1088", MeterSize = "1-1/4 Inch" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "837-806-836", Make = "Lansome", Model = "7400", MeterSize = "5/8 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "207-964-835", Make = "Constance Technologies", Model = "TG-6220", MeterSize = "5/8 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "296-837-495", Make = "Raynes Energica", Model = "QG505", MeterSize = "4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "468-359-486", Make = "Grass Grill", Model = "KLP-8822", MeterSize = "1-1/4 Inch" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "931-486-003", Make = "Planetra", Model = "P-2020", MeterSize = "1/2 Inch" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "483-770-648", Make = "Warren", Model = "WWW", MeterSize = "0.1 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "592-824-957", Make = "Kensa Sons", Model = "D-497-H", MeterSize = "3/4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "293-835-704", Make = "Gongola", Model = "GOL1000", MeterSize = "1/2 Inch" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "739-777-749", Make = "Warrington", Model = "W2200W", MeterSize = "3/4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "374-886-284", Make = "Raynes Energica", Model = "i2022", MeterSize = "3/4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "186-959-757", Make = "Kensa Sons", Model = "M-686-G", MeterSize = "1 1/2 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "594-827-359", Make = "Planetra", Model = "P-8000", MeterSize = "1 Inch" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "394-739-242", Make = "Master Stream", Model = "9393-TT", MeterSize = "5/8 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "529-283-752", Make = "Constance Technologies", Model = "404T", MeterSize = "3/4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "295-770-695", Make = "Warrington", Model = "W-2286", MeterSize = "1-1/4 Inch" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "739-749-737", Make = "Kensa Sons", Model = "KS2000A", MeterSize = "1 Inch" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "947-528-317", Make = "Gondola", Model = "GDL-5000", MeterSize = "1 Inch" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "630-207-055", Make = "Lansome", Model = "2800", MeterSize = "3/4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "827-508-248", Make = "Standard Trend", Model = "428T", MeterSize = "3/4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "293-924-869", Make = "Grass Grill", Model = "CRC-2020", MeterSize = "1/2 Inch" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "928-247-580", Make = "Gondola", Model = "GOL2000", MeterSize = "0.34 Inch" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "682-537-380", Make = "Planetra", Model = "P-2020", MeterSize = "1-1/4 Inch" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "470-628-850", Make = "Estellano", Model = "WRT-482", MeterSize = "3/4 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "649-373-505", Make = "Constance Technologies", Model = "BD-7000", MeterSize = "5/8 Inches" });
db.WaterMeters.Add(new WaterMeter() { MeterNumber = "306-842-497", Make = "Lansome", Model = "9000", MeterSize = "3/4 Inches" });
db.SaveChanges();
ShowWaterMeters();
}private void btnClose_Click(object sender, EventArgs e)
{
Close();
}Customers
Introduction
Various types of entities use the services of a water distribution company. We will provide forms to help a user manage the records of the water services consumers.
Practical Learning: Introducing Customers
using System.ComponentModel.DataAnnotations;
namespace StellarWaterPoint5.Models
{
public class Customer
{
[Key]
public int CustomerId { get; set; }
public string? AccountNumber { get; set; }
public string? AccountName { get; set; }
public string? MeterNumber { get; set; }
public string? AccountType { get; set; }
public string? Address { get; set; }
public string? City { get; set; }
public string? County { get; set; }
public string? State { get; set; }
public string? ZIPCode { get; set; }
}
}using System.Data.Entity;
namespace StellarWaterPoint51.Models
{
public class StellarContext : DbContext
{
public DbSet<WaterMeter>? WaterMeters { get; set; }
public DbSet<Customer>? Customers { get; set; }
}
}Displaying Customers
It can be useful for a user to see a list of the consumers of a water services company. We will provide a form for that goal.
Practical Learning: Displaying Customers
| (Name) | Text | TextAlign | Width |
| colCustomerId | Id | 40 | |
| colAccountNumber | Account # | Center | 150 |
| colAccountName | Account Name | 200 | |
| colMeterNumber | Meter # | Center | 100 |
| colAccountType | Account Type | 200 | |
| colAddress | Address | 250 | |
| colCity | City | 125 | |
| colCounty | County | 125 | |
| colState | State | Center | |
| colZIPCode | ZIP-Code | Center | 125 |
| Control | (Name) | Other Properties | |
| ListView | lvwCustomers | BorderStyle: FixedSingle FullRowSelect: True GridLines: True View: Details |
|
using StellarWaterPoint51.Models;
namespace StellarWaterPoint51.Customers
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowCustomers()
{
StellarContext db = new();
List<Customer> clients = new();
clients = db.Customers!.ToList();
lvwCustomers.Items.Clear();
foreach (Customer client in clients)
{
ListViewItem lviCustomer = new ListViewItem(client.CustomerId.ToString());
lviCustomer.SubItems.Add(client.AccountNumber);
lviCustomer.SubItems.Add(client.AccountName);
lviCustomer.SubItems.Add(client.MeterNumber);
lviCustomer.SubItems.Add(client.AccountType);
lviCustomer.SubItems.Add(client.Address);
lviCustomer.SubItems.Add(client.City);
lviCustomer.SubItems.Add(client.County);
lviCustomer.SubItems.Add(client.State);
lviCustomer.SubItems.Add(client.ZIPCode);
lvwCustomers.Items.Add(lviCustomer);
}
}
private void Central_Load(object sender, EventArgs e)
{
ShowCustomers();
}
}
}| Control | (Name) | Text | Font | |
| Button | btnCustomers | C&ustomers... | FlatStyle: Flat Times New Roman, 24pt, style=Bold |
|
using StellarWaterPoint53.Models;
namespace StellarWaterPoint53
{
public partial class StellarManagement : Form
{
public StellarManagement()
{
InitializeComponent();
. . .
}
private void btnWaterBills_Click(object sender, EventArgs e)
{
}
private void btnCustomers_Click(object sender, EventArgs e)
{
Customers.Central central = new();
central.Show();
}
private void btnWaterMeters_Click(object sender, EventArgs e)
{
WaterMeters.Central central = new();
central.Show();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}A New Customer Account
Every entity that consumes the services of a water distribution company must have an account. We will provide a form from which a customer account can be set up.
Practical Learning: Creating a Customer Account

| Control | (Name) | Text | Other Properties | |
| Label | &Account #: | |||
| MaskedTextBox | mtbAccountNumber | Masked: 0000-000-0000 | ||
| Label | &Account Name: | |||
| TextBox | txtAccountName | |||
| Label | &Meter #: | |||
| MaskedTextBox | mtbMeterNumber | Masked: 000-000-000 | ||
| Button | btnFindWaterMeter | &Find Water Meter | FlatStyle: Flat | |
| Label | Meter &Details: | |||
| TextBox | txtMeterDetails | Enabled: False | ||
| Label | &Account Type: |
OTH - Other BUS - General Business RES - Residential Household SGO - Social/Government/Non-Profit Organization UUO - Unidentified or Unclassified Type of Organization WAT - Water Intensive Business (Laudromat, Hair Salon, Restaurant, etc |
||
| ComboBox | cbxAccountsTypes | |||
| Label | &Address: | |||
| TextBox | txtAddress | |||
| Label | C&ity: | |||
| TextBox | txtCity | |||
| Label | C&ounty: | |||
| TextBox | txtCounty | |||
| Label | &State: | |||
| TextBox | txtState | |||
| Label | &ZIP-Code: | |||
| MaskedTextBox | mtbZIPCode | Masked: Zip-Code | ||
| Button | btnSaveCustomerAccount | S&ave Customer Account | FlatStyle: Flat | |
| Button | btnClose | &Close | FlatStyle: Flat | |
FormBorderStyle: FixedDialog Text: Stellar Water Point - Create Customer Account StartPosition: CenterScreen AcceptButton: btnCreateCustomerAccount CancelButton: btnCancel
using StellarWaterPoint53.Models;
namespace StellarWaterPoint53.Customers
{
public partial class Create : Form
{
public Create()
{
InitializeComponent();
}
private void btnFindWateMeter_Click(object sender, EventArgs e)
{
string strMeterNumber = mtbMeterNumber.Text.Replace("-", "").Trim();
if (string.IsNullOrEmpty(strMeterNumber))
{
MsgBox.Regular("You should first type a valid meter number, " +
"and then click the Find Water Meter button.");
return;
}
StellarContext db = new();
List<WaterMeter> meters = new();
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text);
txtMeterDetails.Text = meter!.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
}
}using StellarWaterPoint53.Models;
namespace StellarWaterPoint53.Customers
{
public partial class Create : Form
{
public Create()
{
InitializeComponent();
}
private void btnFindWateMeter_Click(object sender, EventArgs e)
{
. . .
}
private void btnSaveCustomerAccount_Click(object sender, EventArgs e)
{
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Trim();
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You must type a (unique) account number. " +
"Otherwise, the customer account cannot be created.");
return;
}
string strMeterNumber = mtbMeterNumber.Text.Replace("-", "").Trim();
if (string.IsNullOrEmpty(strMeterNumber))
{
MsgBox.Regular("You must type a meter number of the water meter to associate " +
"to the customer account; otherwise, the account cannot be created.");
return;
}
StellarContext db = new();
Customer client = new Customer()
{
AccountNumber = mtbAccountNumber.Text,
AccountName = txtAccountName.Text,
MeterNumber = mtbMeterNumber.Text,
AccountType = cbxAccountsTypes.Text,
Address = txtAddress.Text,
City = txtCity.Text,
County = txtCounty.Text,
State = txtState.Text,
ZIPCode = mtbZIPCode.Text
};
db.Customers!.Add(client);
db.SaveChanges();
Close();
}
}
}using StellarWaterPoint53.Models;
namespace StellarWaterPoint53.Customers
{
public partial class Create : Form
{
public Create()
{
InitializeComponent();
}
private void btnFindWateMeter_Click(object sender, EventArgs e)
{
/* We want to check that the user had provided a water meter number.
* Since we are using a masked text box, and since its internal symbols
* are counted as being part of the whole value, we need to remove those
* symbols. We will also "trim" the value to remove the empty spaces. */
string strMeterNumber = mtbMeterNumber.Text.Replace("-", "").Trim();
// Make sure the user typed a (valid) water meter number.
if (string.IsNullOrEmpty(strMeterNumber))
{
MsgBox.Regular("You should first type a valid meter number, " +
"and then click the Find Water Meter button.");
// If the user didn't type a water meter number, don't do nothing.
return;
}
// Get a reference to the EF context class
StellarContext db = new();
// Get a list that can contain the records of water meters
List<WaterMeter> meters = new();
// If some records of water meters exist, access them and store them in the List<> variable
meters = db.WaterMeters!.ToList();
/* If a list of water meters exists, find in it the water meter that has
* the same meter number as the value the user had typed in the masked text box. */
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text);
// If the water meter was found, put its values in the text boxes
txtMeterDetails.Text = meter!.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
private void btnSaveCustomerAccount_Click(object sender, EventArgs e)
{
/* We want to check that the user had provided a customer account number.
* Since we are using a masked text box, and since its internal symbols
* are counted as being part of the whole value, we need to remove those
* symbols. We will also "trim" the value to remove the empty spaces. */
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Trim();
/* Make sure the user provides an account number. */
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You must type a (unique) account number. " +
"Otherwise, the customer account cannot be created.");
return;
}
string strMeterNumber = mtbMeterNumber.Text.Replace("-", "").Trim();
if (string.IsNullOrEmpty(strMeterNumber))
{
MsgBox.Regular("You must type a meter number of the water meter to associate " +
"to the customer account; otherwise, the account cannot be created.");
return;
}
// Declare a variable for the Entity Framework database context
StellarContext db = new();
/* Get the values for a water meter from the New Water Meter form.
* Create a WaterMeter object using those values. */
Customer client = new Customer()
{
AccountNumber = mtbAccountNumber.Text,
AccountName = txtAccountName.Text,
MeterNumber = mtbMeterNumber.Text,
AccountType = cbxAccountsTypes.Text,
Address = txtAddress.Text,
City = txtCity.Text,
County = txtCounty.Text,
State = txtState.Text,
ZIPCode = mtbZIPCode.Text
};
// Add the created WaterMeter object to the collection of records in the application
db.Customers!.Add(client);
// Save the record to the database
db.SaveChanges();
Close();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}| Control | (Name) | Other Properties | |
| ListView | lvwCustomers | FullRowSelect: True GridLines: True View: Details |
|
| Button | btnNewCustomerAccount | &New Customer Account... | |
using StellarWaterPoint53.Models;
namespace StellarWaterPoint53.Customers
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowCustomers()
{
StellarContext db = new();
List<Customer> clients = new();
clients = db.Customers!.ToList();
lvwCustomers.Items.Clear();
foreach (Customer client in clients)
{
ListViewItem lviCustomer = new ListViewItem(client.CustomerId.ToString());
lviCustomer.SubItems.Add(client.AccountNumber);
lviCustomer.SubItems.Add(client.AccountName);
lviCustomer.SubItems.Add(client.MeterNumber);
lviCustomer.SubItems.Add(client.AccountType);
lviCustomer.SubItems.Add(client.Address);
lviCustomer.SubItems.Add(client.City);
lviCustomer.SubItems.Add(client.County);
lviCustomer.SubItems.Add(client.State);
lviCustomer.SubItems.Add(client.ZIPCode);
lvwCustomers.Items.Add(lviCustomer);
}
}
private void Central_Load(object sender, EventArgs e)
{
ShowCustomers();
}
private void btnCreateCustomerAccount_Click(object sender, EventArgs e)
{
Create create = new();
create.ShowDialog();
ShowCustomers();
}
}
}Account #: 9279-570-8394 Account Name: Thomas Stones Meter #: 799-528-461 Account Type: RES - Residential Household Address: 10252 Broward Ave #D4 City: Frederick County: Frederick State: MD ZIP-Code: 21703-4422
| Account # | Account Name | Meter # | Account Type | Address | City | County | State | ZIP-Code |
| 4086-938-4783 | Hernola Dough | 594-827-359 | UUO - Unidentified or Unclassified Type of Organization | 10 10 Hexagonal Drv | Winston | Yoke | Penn | 11402-4411 |
| 7080-583-5947 | Sunny Yard | 827-508-248 | WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc | 663 Sherry Wood East Street | Shimpstown | Franklin | PA | 17236-2626 |
using StellarWaterPoint53.Models;
using System.Diagnostics.Metrics;
namespace StellarWaterPoint53
{
public partial class StellarManagement : Form
{
public StellarManagement()
{
InitializeComponent();
StellarContext db = new();
Customer client = new();
db.SaveChanges();
}
private void btnWaterBills_Click(object sender, EventArgs e)
{
}
private void btnCustomers_Click(object sender, EventArgs e)
{
Customers.Central central = new();
central.Show();
}
private void btnWaterMeters_Click(object sender, EventArgs e)
{
WaterMeters.Central central = new();
central.Show();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}using StellarWaterPoint53.Models;
using System.Diagnostics.Metrics;
namespace StellarWaterPoint53
{
public partial class StellarManagement : Form
{
public StellarManagement()
{
InitializeComponent();
StellarContext db = new();
/* WaterMeter? meter = null;
db.SaveChanges(); */
}
private void btnWaterBills_Click(object sender, EventArgs e)
{
}
private void btnCustomers_Click(object sender, EventArgs e)
{
Customers.Central central = new();
central.Show();
}
private void btnWaterMeters_Click(object sender, EventArgs e)
{
WaterMeters.Central central = new();
central.Show();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}Customer Account Details
To let a user view the details of an entity that consumes the services of a water distribution company, we will add a simple form.
Practical Learning: Showing Customer Account

| Control | (Name) | Text | Other Properties | |
| Label | &Account #: | |||
| MaskedTextBox | mtbAccountNumber | Masked: 0000-000-0000 | ||
| Label | &Account Name: | |||
| TextBox | txtAccountName | Enabled: False | ||
| Label | Meter &Details: | |||
| TextBox | txtMeterDetails | Enabled: False | ||
| Label | &Account Type: | |||
| TextBox | txtAccountsTypes | Enabled: False | ||
| Label | &Address: | |||
| TextBox | txtAddress | Enabled: False | ||
| Label | C&ity: | |||
| TextBox | txtCity | Enabled: False | ||
| Label | C&ounty: | |||
| TextBox | txtCounty | Enabled: False | ||
| Label | &State: | |||
| TextBox | txtState | Enabled: False | ||
| Label | &ZIP-Code: | |||
| TextBox | txtZIPCode | Enabled: False | ||
| Label | C&ustomer Id: | |||
| TextBox | txtCustomerId | Enabled: False | ||
| Button | btnClose | &Close | FlatStyle: Flat | |
using StellarWaterPoint14.Models;
namespace StellarWaterPoint14.Customers
{
public partial class Details : Form
{
public Details()
{
InitializeComponent();
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Replace(" ", "");
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You should first type a valid customer account number, " +
"and then click the Find Customer Account button.");
return;
}
StellarContext db = new();
List<Customer> clients = new List<Customer>();
clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == mtbAccountNumber.Text);
txtCustomerId.Text = client!.CustomerId.ToString();
txtAccountName.Text = client.AccountName;
string? strMeterNumber = client.MeterNumber;
txtAccountType.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
txtZIPCode.Text = client.ZIPCode;
List<WaterMeter> meters = new();
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == strMeterNumber);
txtMeterDetails.Text = meter!.WaterMeterId.ToString() + " - " +
meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
}
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}| Control | (Name) | Other Properties | |
| ListView | lvwWaterMeters | No Change | |
| Button | No Change | No Change | |
| Button | btnCustomerAccountDetails | FlatStyle: Flat Customer Account &Details... |
|
using System.Data;
using Microsoft.Data.SqlClient;
namespace StellarWaterPoint21.Customers
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowCustomers()
{
. . .
}
private void Central_Load(object sender, EventArgs e)
{
ShowCustomers();
}
private void btnCreateCustomerAccount_Click(object sender, EventArgs e)
{
. . .
}
private void btnCustomerAccountDetails_Click(object sender, EventArgs e)
{
Details details = new();
details.Show();
}
}
}

Editing a Customer Account
If one or more pieces of information of a consumer changes, we will provide a form to assist the user to update the record.
Practical Learning: Editing a Customer Account

| Control | (Name) | Text | Other Properties | |
| Label | &Account #: | |||
| MaskedTextBox | mtbAccountNumber | Masked: 0000-000-0000 | ||
| Button | btnFindCustomerAccount | &Find Customer Account | FlatStyle: Flat | |
| Label | &Account Name: | |||
| TextBox | txtAccountName | |||
| Label | &Meter #: | |||
| MaskedTextBox | mtbMeterNumber | Masked: 000-000-000 | ||
| Button | btnFindWaterMeter | Find &Water Meter | FlatStyle: Flat | |
| Label | Meter &Details: | |||
| TextBox | txtMeterDetails | Enabled: False | ||
| Label | &Account Type: | |||
| ComboBox | cbxAccountsTypes |
OTH - Other BUS - General Business RES - Residential Household SGO - Social/Government/Non-Profit Organization UUO - Unidentified or Unclassified Type of Organization WAT - Water Intensive Business (Laudromat, Hair Salon, Restaurant, etc |
||
| Label | &Address: | |||
| TextBox | txtAddress | |||
| Label | C&ity: | |||
| TextBox | txtCity | |||
| Label | C&ounty: | |||
| TextBox | txtCounty | |||
| Label | &State: | |||
| TextBox | txtState | |||
| Label | &ZIP-Code: | |||
| MaskedTextBox | mtbZIPCode | Masked: Zip-Code | ||
| Label | C&ustomer Id: | |||
| TextBox | txtCustomerId | Enabled: False | ||
| Button | btnUpdateCustomerAccount | &Update Customer Account | FlatStyle: Flat | |
| Button | btnClose | &Close | FlatStyle: Flat | |
using StellarWaterPoint14.Models;
namespace StellarWaterPoint14.Customers
{
public partial class Editor : Form
{
public Editor()
{
InitializeComponent();
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Replace(" ", "");
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You should first type a valid customer account number, " +
"and then click the Find Customer Account button.");
return;
}
StellarContext db = new();
List<Customer> clients = new List<Customer>();
clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == mtbAccountNumber.Text);
txtCustomerId.Text = client!.CustomerId.ToString();
txtAccountName.Text = client.AccountName;
mtbMeterNumber.Text = client.MeterNumber;
cbxAccountsTypes.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
mtbZIPCode.Text = client.ZIPCode;
List<WaterMeter> meters = new List<WaterMeter>();
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text);
txtMeterDetails.Text = meter!.WaterMeterId.ToString() + " - " +
meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
}
}private void btnFindWateMeter_Click(object sender, EventArgs e)
{
string strMeterNumber = mtbMeterNumber.Text.Replace("-", "").Replace(" ", "");
if (string.IsNullOrEmpty(strMeterNumber))
{
MsgBox.Regular("You should first type a valid meter number, " +
"and then click the Find Water Meter button.");
return;
}
StellarContext db = new();
List<WaterMeter> meters = new();
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text);
txtMeterDetails.Text = meter!.WaterMeterId.ToString() + " - " +
meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}private void btnUpdateCustomerAccount_Click(object sender, EventArgs e)
{
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Replace(" ", "");
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You must first type the account number of the customer, " +
"optionally change one or more values on the form, " +
"and then click the Update Customer Account button.");
return;
}
StellarContext db = new();
List<Customer> clients = new();
clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == mtbAccountNumber.Text);
if (client is not null)
{
client.AccountName = txtAccountName.Text;
client.MeterNumber = mtbMeterNumber.Text;
client.AccountType = cbxAccountsTypes.Text;
client.Address = txtAddress.Text;
client.City = txtCity.Text;
client.County = txtCounty.Text;
client.State = txtState.Text;
client.ZIPCode = mtbZIPCode.Text;
db.SaveChanges();
}
Close();
}using StellarWaterPoint14.Models;
namespace StellarWaterPoint14.Customers
{
public partial class Editor : Form
{
public Editor()
{
InitializeComponent();
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Replace(" ", "");
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You should first type a valid customer account number, " +
"and then click the Find Customer Account button.");
return;
}
StellarContext db = new();
List<Customer> clients = new List<Customer>();
clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == mtbAccountNumber.Text);
txtCustomerId.Text = client!.CustomerId.ToString();
txtAccountName.Text = client.AccountName;
mtbMeterNumber.Text = client.MeterNumber;
cbxAccountsTypes.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
mtbZIPCode.Text = client.ZIPCode;
List<WaterMeter> meters = new List<WaterMeter>();
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text);
txtMeterDetails.Text = meter!.WaterMeterId.ToString() + " - " +
meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
private void btnFindWateMeter_Click(object sender, EventArgs e)
{
string strMeterNumber = mtbMeterNumber.Text.Replace("-", "").Replace(" ", "");
if (string.IsNullOrEmpty(strMeterNumber))
{
MsgBox.Regular("You should first type a valid meter number, " +
"and then click the Find Water Meter button.");
return;
}
StellarContext db = new();
List<WaterMeter> meters = new();
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text);
txtMeterDetails.Text = meter!.WaterMeterId.ToString() + " - " +
meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
private void btnUpdateCustomerAccount_Click(object sender, EventArgs e)
{
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Replace(" ", "");
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You must first type the account number of the customer, " +
"optionally change one or more values on the form, " +
"and then click the Update Customer Account button.");
return;
}
StellarContext db = new();
List<Customer> clients = new();
clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == mtbAccountNumber.Text);
if (client is not null)
{
client.AccountName = txtAccountName.Text;
client.MeterNumber = mtbMeterNumber.Text;
client.AccountType = cbxAccountsTypes.Text;
client.Address = txtAddress.Text;
client.City = txtCity.Text;
client.County = txtCounty.Text;
client.State = txtState.Text;
client.ZIPCode = mtbZIPCode.Text;
db.SaveChanges();
}
Close();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}| Control | (Name) | Other Properties | |
| ListView | lvwCustomers | No Change | |
| Button | btnNewCustomerAccount | No Change | |
| Button | btnCustomerAccountDetails | No Change | |
| Button | btnUpdateCustomerAccount | ||
using StellarWaterPoint53.Models;
namespace StellarWaterPoint53.Customers
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowCustomers()
{
. . .
}
private void Central_Load(object sender, EventArgs e)
{
ShowCustomers();
}
private void btnCreateCustomerAccount_Click(object sender, EventArgs e)
{
Create create = new();
create.ShowDialog();
ShowCustomers();
}
private void btnCustomerAccountDetails_Click(object sender, EventArgs e)
{
Details details = new();
details.Show();
}
private void btnEditCustomerAccount_Click(object sender, EventArgs e)
{
Editor editor = new();
editor.ShowDialog();
ShowCustomers();
}
}
}

Account Name: Bernotte Doughnuts Meter #: 580-742-825 and click Find Water Meter Account Type: BUS - General Business Address: 10103 Hexagon Drive City: Winterstown County: York State: PA ZIP-Code: 17402-8828


Deleting a Customer Account from the Database
When a consumer account becomes useless and must be removed from the system, we will create a form from which a user can delete an account.
Practical Learning: Deleting a Customer Account

| Control | (Name) | Text | Other Properties | |
| Label | &Account #: | |||
| MaskedTextBox | mtbAccountNumber | Masked: 0000-000-0000 | ||
| Button | btnFindCustomerAccount | &Find Customer Account | FlatStyle: Flat | |
| Label | &Account Name: | |||
| TextBox | txtAccountName | Enabled: False | ||
| Label | Meter &Details: | |||
| TextBox | txtMeterDetails | Enabled: False | ||
| Label | &Account Type: | |||
| TextBox | txtAccountsTypes | Enabled: False | ||
| Label | &Address: | |||
| TextBox | txtAddress | Enabled: False | ||
| Label | C&ity: | |||
| TextBox | txtCity | Enabled: False | ||
| Label | C&ounty: | |||
| TextBox | txtCounty | Enabled: False | ||
| Label | &State: | |||
| TextBox | txtState | Enabled: False | ||
| Label | &ZIP-Code: | |||
| TextBox | txtZIPCode | Enabled: False | ||
| Label | Customer Id:& | |||
| TextBox | txtCustomerId | Enabled: False | ||
| Button | btnDeleteCustomerAccount | &Delete Customer Account | ||
| Button | btnClose | &Close | ||
using StellarWaterPoint14.Models;
namespace StellarWaterPoint14.Customers
{
public partial class Delete : Form
{
public Delete()
{
InitializeComponent();
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Replace(" ", "");
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You should first type a valid customer account number, " +
"and then click the Find Customer Account button.");
return;
}
StellarContext db = new();
List<Customer> clients = new List<Customer>();
clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == mtbAccountNumber.Text);
txtCustomerId.Text = client!.CustomerId.ToString();
txtAccountName.Text = client.AccountName;
string? strMeterNumber = client.MeterNumber;
txtAccountType.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
txtZIPCode.Text = client.ZIPCode;
List<WaterMeter> meters = new();
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == strMeterNumber);
txtMeterDetails.Text = meter!.WaterMeterId.ToString() + " - " +
meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
}
}private void btnDeleteCustomerAccount_Click(object sender, EventArgs e)
{
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Replace(" ", "");
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You must enter a valid customer account number, " +
"click the Find Customer Account button, " +
"and then click the Delete Customer Account button.");
return;
}
StellarContext db = new();
List<Customer> clients = new();
clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == mtbAccountNumber.Text);
if (client is not null)
{
string strQuestion = "Are you sure you want to delete this " +
"customer's account (the operation cannot be undone)?";
if (MsgBox.Regular(strQuestion) == DialogResult.Yes)
{
db.Customers!.Remove(client);
db.SaveChanges();
}
}
Close();
}
}
}using StellarWaterPoint14.Models;
namespace StellarWaterPoint14.Customers
{
public partial class Delete : Form
{
public Delete()
{
InitializeComponent();
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Replace(" ", "");
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You should first type a valid customer account number, " +
"and then click the Find Customer Account button.");
return;
}
StellarContext db = new();
List<Customer> clients = new List<Customer>();
clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == mtbAccountNumber.Text);
txtCustomerId.Text = client!.CustomerId.ToString();
txtAccountName.Text = client.AccountName;
string? strMeterNumber = client.MeterNumber;
txtAccountType.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
txtZIPCode.Text = client.ZIPCode;
List<WaterMeter> meters = new();
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == strMeterNumber);
txtMeterDetails.Text = meter!.WaterMeterId.ToString() + " - " +
meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
private void btnDeleteCustomerAccount_Click(object sender, EventArgs e)
{
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Replace(" ", "");
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You must enter a valid customer account number, " +
"click the Find Customer Account button, " +
"and then click the Delete Customer Account button.");
return;
}
StellarContext db = new();
List<Customer> clients = new();
clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == mtbAccountNumber.Text);
if (client is not null)
{
string strQuestion = "Are you sure you want to delete this " +
"customer's account (the operation cannot be undone)?";
if (MsgBox.Regular(strQuestion, "Stellar Water Point",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
db.Customers!.Remove(client);
db.SaveChanges();
}
}
Close();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}| Control | (Name) | Other Properties | |
| ListView | lvwCustomers | FullRowSelect: True GridLines: True View: Details Anchor: Top, Bottom, Left, Right |
|
| Button | btnCreateCustomerAccount | FlatStyle: Flat Create Customer &Account.... Anchor: Bottom, Right |
|
| Button | btnCustomerAccountDetails | FlatStyle: Flat Customer Account &Details... Anchor: Bottom, Right |
|
| Button | btnEditCustomerAccount | FlatStyle: Flat &Edit Customer Account... Anchor: Bottom, Right |
|
| Button | btnDeleteCustomerAccount | FlatStyle: Flat &Delete Customer Account... Anchor: Bottom, Right |
|
| Button | btnClose | FlatStyle: Flat &Close Anchor: Bottom, Right |
|
using StellarWaterPoint53.Models;
namespace StellarWaterPoint53.Customers
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowCustomers()
{
StellarContext db = new();
List<Customer> clients = new();
clients = db.Customers!.ToList();
lvwCustomers.Items.Clear();
foreach (Customer client in clients)
{
ListViewItem lviCustomer = new ListViewItem(client.CustomerId.ToString());
lviCustomer.SubItems.Add(client.AccountNumber);
lviCustomer.SubItems.Add(client.AccountName);
lviCustomer.SubItems.Add(client.MeterNumber);
lviCustomer.SubItems.Add(client.AccountType);
lviCustomer.SubItems.Add(client.Address);
lviCustomer.SubItems.Add(client.City);
lviCustomer.SubItems.Add(client.County);
lviCustomer.SubItems.Add(client.State);
lviCustomer.SubItems.Add(client.ZIPCode);
lvwCustomers.Items.Add(lviCustomer);
}
}
private void Central_Load(object sender, EventArgs e)
{
ShowCustomers();
}
private void btnCreateCustomerAccount_Click(object sender, EventArgs e)
{
Create create = new();
create.ShowDialog();
ShowCustomers();
}
private void btnCustomerAccountDetails_Click(object sender, EventArgs e)
{
Details details = new();
details.Show();
}
private void btnEditCustomerAccount_Click(object sender, EventArgs e)
{
Editor editor = new();
editor.ShowDialog();
ShowCustomers();
}
private void btnDeleteCustomerAccount_Click(object sender, EventArgs e)
{
Delete delete = new();
delete.ShowDialog();
ShowCustomers();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}



private void btnClose_Click(object sender, EventArgs e)
{
// Close();
StellarContext db = new();
db.Customers!.Add(new Customer() { AccountNumber = "9279-570-8394", AccountName = "Thomas Stones", MeterNumber = "799-528-461", AccountType = "RES - Residential Household", Address = "10252 Broward Ave #D4", City = "Frederick", County = "Frederick", State = "MD", ZIPCode = "21703-4422" });
db.Customers!.Add(new Customer() { AccountNumber = "4086-938-4783", AccountName = "Bernotte Doughnuts", MeterNumber = "580-742-825", AccountType = "BUS - General Business", Address = "10103 Hexagon Drv", City = "Winterstown", County = "York", State = "PA", ZIPCode = "17402-8818" });
db.Customers!.Add(new Customer() { AccountNumber = "2068-258-9486", AccountName = "Yollanda Training", MeterNumber = "186-962-805", AccountType = "UUO - Unidentified or Unclassified Type of Organization", Address = "4819 East Munk Street", City = "Whitehall", County = "Fulton", State = "PA", ZIPCode = "17340-1188" });
db.Customers!.Add(new Customer() { AccountNumber = "6986-829-3741", AccountName = "Eyes Wide", MeterNumber = "208-428-308", AccountType = "BUS - General Business", Address = "12087 Avencia Court #4D1", City = "Silver Spring", County = "Montgomery", State = "MD", ZIPCode = "20910-2288" });
db.Customers!.Add(new Customer() { AccountNumber = "9947-374-2648", AccountName = "Marianne Harrington", MeterNumber = "862-715-006", AccountType = "RES - Residential Household", Address = "708 Correta Drv", City = "Arlington", State = "VA", ZIPCode = "22222-6060" });
db.Customers!.Add(new Customer() { AccountNumber = "4293-802-8506", AccountName = "Kelly Davids", MeterNumber = "496-813-794", AccountType = "RES - Residential Household", Address = "938 East Panchot Str", City = "Greenwood", County = "Sussex", State = "DE", ZIPCode = "19950-4242" });
db.Customers!.Add(new Customer() { AccountNumber = "6240-857-4965", AccountName = "First Methodist Congregation", MeterNumber = "739-777-749", AccountType = "RES - Residential Household", Address = "7702 Charles Road" });
db.Customers!.Add(new Customer() { AccountNumber = "7928-131-4850", AccountName = "Department of Public Affairs", AccountType = "SGO - Social/Government/Non - Profit Organization", City = "Hyattsville", County = "Prince Georges", State = "MD", ZIPCode = "20783-2277" });
db.Customers!.Add(new Customer() { AccountNumber = "1386-949-2058", AccountName = "Watson Country Buffet", MeterNumber = "296-837-495", AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "4862 Wellington Street", City = "Hammonton", County = "Atlantic ", State = "NJ", ZIPCode = "08037-2828" });
db.Customers!.Add(new Customer() { AccountNumber = "7943-686-9786", AccountName = "Angel Bulzaides", MeterNumber = "394-835-297", AccountType = "RES - Residential Household", Address = "10227 Old Harbor Drv", City = "Elkview", County = "Kanawha", State = "WV", ZIPCode = "25071-5858" });
db.Customers!.Add(new Customer() { AccountNumber = "4820-375-2842", AccountName = "Sun Communal", MeterNumber = "392-494-572", AccountType = "SGO - Social/Government/Non - Profit Organization", Address = "748 Red Hills Rd", City = "Roanoke", State = "VA", ZIPCode = "24012-4824" });
db.Customers!.Add(new Customer() { AccountNumber = "9618-579-2577", AccountName = "Gerald Place", MeterNumber = "847-252-246", AccountType = "UUO - Unidentified or Unclassified Type of Organization", Address = "3666 Hanchor Drv", City = "Granville", County = "Licking", State = "OH", ZIPCode = "43023-2777" });
db.Customers!.Add(new Customer() { AccountNumber = "2037-495-8528", AccountName = "Astral Sequence", AccountType = "BUS - General Business", Address = "12715 Eastern Gateway", City = "Catonsville", County = "Baltimore County" });
db.Customers!.Add(new Customer() { AccountNumber = "6003-386-3955", AccountName = "Mandiakandara Marmoudi", MeterNumber = "374-886-284", AccountType = "OTH - Other", Address = "539 Avalon Court", City = "Greenwood", County = "Sussex", State = "DE", ZIPCode = "19950-5550" });
db.Customers!.Add(new Customer() { AccountNumber = "5294-859-7513", AccountName = "Jeannette Schiller", MeterNumber = "713-942-058", AccountType = "RES - Residential Household", Address = "10110 Winslow Ave", City = "Mercerville", County = "Mercer", State = "NJ", ZIPCode = "08619-7472" });
db.Customers!.Add(new Customer() { AccountNumber = "9249-379-6848", AccountName = "Country West Eatery", MeterNumber = "588-279-663", AccountType = "BUS - General Business", Address = "8280 Sligo North Way", City = "Albright", County = "Preston", State = "WV", ZIPCode = "26519-6620" });
db.Customers!.Add(new Customer() { AccountNumber = "5252-757-9595", AccountName = "Sathyavanthara Khooni", MeterNumber = "379-386-979", AccountType = "RES - Residential Household", Address = "4992 Preston Street", State = "OH" });
db.Customers!.Add(new Customer() { AccountNumber = "7080-583-5947", AccountName = "Sunny Yard", MeterNumber = "827-508-248", AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "663 Sherry Wood East Street", City = "Shimpstown", County = "Franklin", State = "PA", ZIPCode = "17236-2626" });
db.Customers!.Add(new Customer() { AccountNumber = "8027-304-6829", AccountName = "Anthony Clarcksons", MeterNumber = "837-806-836", AccountType = "RES - Residential Household", Address = "904 Augusta Drive", City = "Blackbird", County = "New Castle", State = "DE", ZIPCode = "19734-8822" });
db.Customers!.Add(new Customer() { AccountNumber = "6699-396-2905", AccountName = "Spencer Reuter", MeterNumber = "649-373-505", AccountType = "RES - Residential Household", Address = "2850 Burnsweak Avenue", City = "Silver Spring", County = "Montgomery", State = "MD", ZIPCode = "20910-4044" });
db.Customers!.Add(new Customer() { AccountNumber = "1827-395-0203", AccountName = "Watson Country Buffet", MeterNumber = "470-628-850", AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "10331 Chryswell Road", City = "Washington", State = "DC", ZIPCode = "20008-2426" });
db.Customers!.Add(new Customer() { AccountNumber = "5862-736-9741", AccountName = "Eastern Cage", AccountType = "BUS - General Business", Address = "2039 Night Stand Court", City = "Hammonton", State = "NJ" });
db.Customers!.Add(new Customer() { AccountNumber = "3947-957-4958", AccountName = "Patsil Industries", MeterNumber = "747-581-379", AccountType = "BUS - General Business", Address = "10348 Larrens Drive", City = "Baltimore", County = "Baltimore", State = "MD", ZIPCode = "21215-2222" });
db.Customers!.Add(new Customer() { AccountNumber = "2836-485-9699", AccountName = "Red Oak High School", MeterNumber = "379-386-979", AccountType = "SGO - Social/Government/Non - Profit Organization", Address = "442 Donham Road", City = "Silver Spring", County = "Montgomery", State = "MD", ZIPCode = "20910-8822" });
db.Customers!.Add(new Customer() { AccountNumber = "5938-074-5293", AccountName = "Park and Roll", MeterNumber = "592-824-957", AccountType = "SGO - Social/Government/Non - Profit Organization", Address = "582G Dunhill Avenue", City = "Lanham", County = "Prince Georges", State = "MD", ZIPCode = "20706-8284" });
db.Customers!.Add(new Customer() { AccountNumber = "3028-502-9418", AccountName = "Spencer Kershaw", MeterNumber = "186-959-757", AccountType = "RES - Residential Household", Address = "338C Grayson Street", City = "Gatchellville", County = "York", State = "PA", ZIPCode = "17352-6464" });
db.Customers!.Add(new Customer() { AccountNumber = "9684-759-2227", AccountName = "Country West Eatery", MeterNumber = "928-317-924", AccountType = "BUS - General Business" });
db.Customers!.Add(new Customer() { AccountNumber = "2974-972-8139", AccountName = "Paul Arnette", MeterNumber = "295-770-695", AccountType = "OTH - Other", Address = "8127 Bledsoe Str", City = "Hyattsville", County = "Prince Georges", State = "MD", ZIPCode = "20783-5858" });
db.Customers!.Add(new Customer() { AccountNumber = "2758-493-7249", AccountName = "Hervey Smile", MeterNumber = "293-924-869", AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "12973 Sonaa Street #E42", City = "Silver Spring", County = "Montgomery", State = "MD", ZIPCode = "20910-4488" });
db.Customers!.Add(new Customer() { AccountNumber = "9337-947-3664", AccountName = "Awesome Aid", MeterNumber = "649-358-184", AccountType = "SGO - Social/Government/Non - Profit Organization", City = "Bellefontaine", State = "OH" });
db.Customers!.Add(new Customer() { AccountNumber = "7518-302-6895", AccountName = "Grace Brenner", MeterNumber = "207-964-835", AccountType = "BUS - General Business", Address = "4299 Peachtree Court", City = "Rockville", County = "Montgomery", State = "MD", ZIPCode = "20853-1888" });
db.Customers!.Add(new Customer() { AccountNumber = "2937-947-3008", AccountName = "Jeffrey Maney", AccountType = "RES - Residential Household" });
db.Customers!.Add(new Customer() { AccountNumber = "7028-405-9381", AccountName = "Valley Services", MeterNumber = "306-842-497", AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "613 Meadowhill Road", City = "Alonzaville", County = "Shenandoah", State = "VA", ZIPCode = "22664-8080" });
db.Customers!.Add(new Customer() { AccountNumber = "5293-957-3395", AccountName = "Wellway Community Center", MeterNumber = "386-468-057", AccountType = "RES - Residential Household", Address = "10484 Greenway Avenue", City = "Mt Storm", County = "Grant", State = "WV", ZIPCode = "26739-7700" });
db.Customers!.Add(new Customer() { AccountNumber = "2038-413-9680", AccountName = "Eastern Friandise", MeterNumber = "938-725-869", AccountType = "BUS - General Business", Address = "2075 Rose Hills Avenue", City = "Washington", State = "DC", ZIPCode = "20004-2626" });
db.Customers!.Add(new Customer() { AccountNumber = "7484-744-9708", AccountName = "Amidou Gomah", AccountType = "RES - Residential Household", Address = "14118 Yellow Burrough Blvd", City = "Philadelphia", State = "PA" });
db.Customers!.Add(new Customer() { AccountNumber = "3792-853-6885", AccountName = "Department of Public Affairs", MeterNumber = "595-753-147", City = "Upper Marlboro", County = "Prince George County" });
db.Customers!.Add(new Customer() { AccountNumber = "8282-777-8282", AccountName = "Garland Hotel", MeterNumber = "938-275-294", AccountType = "BUS - General Business", Address = "4222 Extell Ave", City = "Cambridge", State = "MD", ZIPCode = "21613-2288" });
db.Customers!.Add(new Customer() { AccountNumber = "5975-863-7057", AccountName = "Single Connection", MeterNumber = "288-427-585", AccountType = "BUS - General Business", City = "Mansfield", State = "OH", ZIPCode = "44903-3030" });
db.Customers!.Add(new Customer() { AccountNumber = "2499-636-4444", AccountName = "Bryanna Spencer", AccountType = "RES - Residential Household", Address = "6282 Sheppherd Str", County = "Anne Arundel" });
db.Customers!.Add(new Customer() { AccountNumber = "2842-585-7260", AccountName = "District Community Reserves", MeterNumber = "349-725-848", AccountType = "SGO - Social/Government/Non - Profit Organization", Address = "3280 Hopewell Street, NE", City = "Washington" });
db.Customers!.Add(new Customer() { AccountNumber = "9282-794-7937", AccountName = "Yashua Yáñés", MeterNumber = "392-494-572", AccountType = "RES - Residential Household", Address = "10214 Monroe Ave", City = "Easton", State = "MD" });
db.Customers!.Add(new Customer() { AccountNumber = "6837-468-4750", AccountName = "Miguel Altieri", AccountType = "RES - Residential Household", Address = "10941 Patriot Blvd", City = "Crenshaw", County = "Jefferson", State = "PA", ZIPCode = "15824-6628" });
db.Customers!.Add(new Customer() { AccountNumber = "2847-597-2829", AccountName = "Jameson", MeterNumber = "379-386-979", AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "7373 Gold Town Rd", State = "WV" });
db.Customers!.Add(new Customer() { AccountNumber = "6381-748-2222", AccountName = "Up Eyes", AccountType = "BUS - General Business", Address = "4149 Deerfield Str" });
db.Customers!.Add(new Customer() { AccountNumber = "4968-274-9638", AccountName = "Annette Wald", AccountType = "RES - Residential Household", Address = "11441 Eastern Friendshi Rd" });
db.Customers!.Add(new Customer() { AccountNumber = "8384-708-2941", AccountName = "Department of Environment Affairs", AccountType = "SGO - Social/Government/Non - Profit Organization", State = "OH" });
db.Customers!.Add(new Customer() { AccountNumber = "3728-138-2947", AccountName = "Marie Rath", Address = "8802 Atlantic Ave", State = "PA" });
db.Customers!.Add(new Customer() { AccountNumber = "1793-857-9413", AccountName = "Body Care", AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", City = "Ocean City", State = "NJ" });
db.Customers!.Add(new Customer() { AccountNumber = "6028-695-2068", AccountName = "Ronald Glassman", MeterNumber = "468-359-486", AccountType = "BUS - General Business" });
db.Customers!.Add(new Customer() { AccountNumber = "4069-37-49728", AccountName = "Lucette Wash N Dry", AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "8812 Lawrence Ave", State = "NW" });
db.Customers!.Add(new Customer() { AccountNumber = "9616-283-7249", AccountName = "Department of Public Affairs", AccountType = "SGO - Social/Government/Non - Profit Organization", Address = "13006 Blueberry Ave", City = "Greenbelt", State = "MD", ZIPCode = "20770-2040" });
db.Customers!.Add(new Customer() { AccountNumber = "2829-516-8353", AccountName = "Richard Eghert", AccountType = "RES - Residential Household", Address = "662 Placido Road" });
db.SaveChanges();
ShowCustomers();
}private void btnClose_Click(object sender, EventArgs e)
{
Close();
}Water Bills
Introduction
A water distribution company must create water bills to present some information about what consumers had used as a service. Of courses, such bills must be designed, created, and presented. We will take care of that.
Practical Learning: Introducing Customers
using System.ComponentModel.DataAnnotations;
namespace StellarWaterPoint5.Models
{
public class WaterBill
{
[Key]
public int WaterBillId { get; set; }
public int BillNumber { get; set; }
public string? AccountNumber { get; set; }
public DateTime MeterReadingStartDate { get; set; }
public DateTime MeterReadingEndDate { get; set; }
public int BillingDays { get; set; }
public int CounterReadingStart { get; set; }
public int CounterReadingEnd { get; set; }
public int TotalHCF { get; set; }
public int TotalGallons { get; set; }
public double FirstTierConsumption { get; set; }
public double SecondTierConsumption { get; set; }
public double LastTierConsumption { get; set; }
public double WaterCharges { get; set; }
public double SewerCharges { get; set; }
public double EnvironmentCharges { get; set; }
public double ServiceCharges { get; set; }
public double TotalCharges { get; set; }
public double LocalTaxes { get; set; }
public double StateTaxes { get; set; }
public DateTime PaymentDueDate { get; set; }
public double AmountDue { get; set; }
public DateTime LatePaymentDueDate { get; set; }
public double LateAmountDue { get; set; }
}
}using System.Data.Entity;
namespace StellarWaterPoint51.Models
{
public class StellarContext : DbContext
{
public DbSet<WaterMeter>? WaterMeters { get; set; }
public DbSet<Customer>? Customers { get; set; }
public DbSet<WaterBill>? WaterBills { get; set; }
}
}namespace StellarWaterPoint14.Models
{
internal static class WaterBillManager
{
internal static (double a, double b, double c) CalculateTiers(string acnt, double total)
{
(double tier1, double tier2, double tier3) results = (0.00, 0.00, 0.00);
switch (acnt)
{
case "RES":
results.tier1 = total * 39.35 / 10000.00;
results.tier2 = total * 18.25 / 10000.00;
results.tier3 = total * 11.65 / 10000.00;
break;
case "SGO":
results.tier1 = total * 41.38 / 10000.00;
results.tier2 = total * 15.26 / 10000.00;
results.tier3 = total * 8.13 / 10000.00;
break;
case "BUS":
results.tier1 = total * 51.25 / 10000.00;
results.tier2 = total * 34.65 / 10000.00;
results.tier3 = total * 15.10 / 10000.00;
break;
case "UUO":
results.tier1 = total * 25.00 / 10000.00;
results.tier2 = total * 35.00 / 10000.00;
results.tier3 = total * 40.00 / 10000.00;
break;
case "WAT":
results.tier1 = (total / 6) * 3 * 50.00 / 10000.00;
results.tier2 = (total / 6) * 2 * 35.00 / 10000.00;
results.tier3 = total * 15.00 / 10000.00;
break;
default:
results.tier1 = total * (48.00 / 10000.00);
results.tier2 = total * (32.00 / 10000.00);
results.tier3 = total * (20.00 / 10000.00);
break;
}
return results;
}
internal static double CalculateSewerCharges(string acnt, double total)
{
double result;
if (acnt == "RES")
{
result = total * 1.028641 / 100.00;
}
else if (acnt == "SGO")
{
result = total * 4.162522 / 100.00;
}
else if (acnt == "BUS")
{
result = total * 8.446369 / 100.00;
}
else if (acnt == "UUO")
{
result = total * 10.622471 / 100.00;
}
else if (acnt == "WAT")
{
result = total * 12.053152 / 100.00;
}
else // if (acnt == "OTH)"
{
result = total * 9.206252 / 100.00;
}
return result;
}
internal static double CalculateEnvironmentCharges(string acnt, double total)
{
double result;
switch (acnt)
{
case "RES":
result = total * 0.004524;
break;
case "SGO":
result = total * 0.118242;
break;
case "BUS":
result = total * 0.161369;
break;
case "UUO":
result = total * 0.082477;
break;
case "WAT":
result = total * 0.413574;
break;
default:
result = total * 0.221842;
break;
}
return result;
}
internal static double CalculateServiceCharges(string acnt, double total)
{
switch (acnt)
{
case "RES":
return total * 0.006248;
case "SGO":
return total * 0.102246;
case "BUS":
return total * 0.155227;
case "UUO":
return total * 0.186692;
case "WAT":
return total * 0.412628;
default:
return total * 0.210248;
}
}
internal static double CalculateLocalTaxes(string acnt, double total) => acnt switch
{
"RES" => total * 0.035749,
"SGO" => total * 0.044026,
"BUS" => total * 0.122517,
"UUO" => total * 0.105737,
"WAT" => total * 0.153248,
_ => total * 0.125148
};
internal static double CalculateStateTaxes(string acnt, double total) => acnt switch
{
"RES" => total * 0.007124,
"SGO" => total * 0.008779,
"BUS" => total * 0.042448,
"UUO" => total * 0.067958,
"WAT" => total * 0.081622,
_ => total * 0.013746
};
internal static DateTime SetPaymentDueDate(string acnt, DateTime date)
{
TimeSpan tsPaymentDueDate = new TimeSpan(1, 0, 0, 0);
if (acnt == "RES")
{
tsPaymentDueDate = new TimeSpan(15, 0, 0, 0);
}
else if (acnt == "SGO")
{
tsPaymentDueDate = new TimeSpan(20, 0, 0, 0);
}
else if (acnt == "BUS")
{
tsPaymentDueDate = new TimeSpan(30, 0, 0, 0);
}
else if (acnt == "UUO")
{
tsPaymentDueDate = new TimeSpan(15, 0, 0, 0);
}
else if (acnt == "WAT")
{
tsPaymentDueDate = new TimeSpan(40, 0, 0, 0);
}
else
{
tsPaymentDueDate = new TimeSpan(35, 0, 0, 0);
}
return date + tsPaymentDueDate;
}
internal static DateTime SetLatePaymentDueDate(string acnt, DateTime date)
{
switch (acnt)
{
case "RES":
return date + new TimeSpan(30, 0, 0, 0);
case "SGO":
return date + new TimeSpan(40, 0, 0, 0);
case "BUS":
return date + new TimeSpan(50, 0, 0, 0);
case "UUO":
return date + new TimeSpan(60, 0, 0, 0);
case "WAT":
return date + new TimeSpan(65, 0, 0, 0);
default:
return date + new TimeSpan(45, 0, 0, 0);
}
}
internal static double CalculateLateAmountDue(string acnt, double amt) => acnt switch
{
"RES" => amt + 8.95,
"SGO" => amt + (amt / 4.575),
"BUS" => amt + (amt / 12.315),
"UUO" => amt + (amt / 7.425),
"WAT" => amt + (amt / 15.225),
_ => amt + (amt / 6.735)
};
}
}Showing Water Bills
A typical water distribution application has many customers and each customer has a tremendous number of water bills. Some time to time, a user may want to see a list of water bills. We will create a form to make it possible.
Practical Learning: Showing Water Bills
| (Name) | Text | TextAlign | Width |
| colWaterBillId | Id | 40 | |
| colBillNumber | Bill # | Center | 80 |
| colAccountSummary | Account Summary | Center | 550 |
| colStartDate | Start Date | Center | 150 |
| colEndDate | End Date | Center | 150 |
| colBillingDays | Days | Center | |
| colCounterStart | Counter Start | Right | 125 |
| colCounterEnd | Counter End | Right | 125 |
| colTotalHCF | Total HCF | Right | 100 |
| colGallons | Gallons | Right | 95 |
| colPaymentDueDate | Pmt Due Date | Center | 125 |
| colAmountDue | Amt Due | Right | 90 |
| Control | (Name) | Other Properties | |
| ListView | lvwWaterBills | FullRowSelect: True GridLines: True View: Details Anchor: Top, Bottom, Left, Right |
|
using StellarWaterPoint14.Models;
namespace StellarWaterPoint14.WaterBills
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowWaterBills()
{
StellarContext db = new();
List<Customer> clients = [.. db.Customers!];
List<WaterBill> bills = [.. db.WaterBills!];
lvwWaterBills.Items.Clear();
foreach (WaterBill bill in bills)
{
ListViewItem lviWaterBill = new ListViewItem(bill.WaterBillId.ToString());
lviWaterBill.SubItems.Add(bill.BillNumber.ToString());
Customer? client = clients.Find(cl => cl.AccountNumber == bill.AccountNumber);
lviWaterBill.SubItems.Add(client!.CustomerId.ToString() + " - " +
client.AccountNumber + " - " +
client.AccountName +
", Type: " + client.AccountType + //![..3]
", (Mtr #: " + client.MeterNumber + ")");
lviWaterBill.SubItems.Add(bill.MeterReadingStartDate.ToShortDateString());
lviWaterBill.SubItems.Add(bill.MeterReadingEndDate.ToShortDateString());
lviWaterBill.SubItems.Add(bill.BillingDays.ToString());
lviWaterBill.SubItems.Add(bill.CounterReadingStart.ToString());
lviWaterBill.SubItems.Add(bill.CounterReadingEnd.ToString());
lviWaterBill.SubItems.Add(bill.TotalHCF.ToString());
lviWaterBill.SubItems.Add(bill.TotalGallons.ToString());
lviWaterBill.SubItems.Add(bill.PaymentDueDate.ToShortDateString());
lviWaterBill.SubItems.Add(bill.AmountDue.ToString());
lvwWaterBills.Items.Add(lviWaterBill);
}
}
private void Central_Load(object sender, EventArgs e)
{
ShowWaterBills();
}
}
}| Control | (Name) | Text | Font | |
| Button | btnWaterBills | C&Water Bills... | FlatStyle: Flat Times New Roman, 24pt, style=Bold |
|
| Button | btnClose | &Close | FlatStyle: Flat Times New Roman, 24pt, style=Bold |
|
namespace StellarWaterPoint21
{
public partial class StellarManagement : Form
{
public StellarManagement()
{
InitializeComponent();
}
private void btnWaterBills_Click(object o, EventArgs e)
{
WaterBills.Central central = new();
central.Show();
}
private void btnCustomers_Click(object o, EventArgs e)
{
Customers.Central clients = new();
clients.ShowDialog();
}
private void btnWaterMeters_Click(object o, EventArgs e)
{
WaterMeters.Central central = new WaterMeters.Central();
central.ShowDialog();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}A New Water Bill
As mentioned already, a water bill must be created and some other aspects, such as calculations, must be taken into consideration. We will create a form that a user can use for that goal.
Practical Learning: Processing a Water Bill
| Control | Text | Name | Other Properties | |
| Label | &Water Bill #: | |||
| TextBox | txtBillNumber | |||
| GroupBox | Customer Information | |||
| Label | &Account #: | |||
| MaskedTextBox | mtbAccountNumber | Mask: 0000-000-0000 | ||
| Button | Find Customer &Account | btnFindCustomerAccount | FlatStyle: Flat | |
| Label | Account Name: | |||
| TextBox | txtAccountName | |||
| Label | Account Type: | |||
| TextBox | txtAccountType | |||
| Label | Address: | |||
| TextBox | txtAddress | |||
| TextBox | txtCity | |||
| TextBox | txtCounty | |||
| TextBox | txtState | |||
| TextBox | txtZIPCode | |||
| Label | _________________________________________________ | |||
| Label | Meter Details: | |||
| TextBox | txtMeterDetails | |||
| GroupBox | Meter Reading | |||
| Label | Meter &Reading Start Date: | |||
| Date Time Picker | dtpMeterReadingStartDate | |||
| Label | Meter Reading &End Date: | |||
| Date Time Picker | dtpMeterReadingEndDate | |||
| Label | Coun&ter Reading Start: | |||
| TextBox | txtCounterReadingStart | |||
| Label | Counter Readi&ng End: | |||
| TextBox | txtCounterReadingEnd | |||
| Button | &Evaluate Water Bill | btnEvaluateWaterBill | FlatStyle: Flat Times New Roman, 24pt, style=Bold |
|
| GroupBox | Meter Result | |||
| Label | Billing Days: | |||
| TextBox | txtBillingDays | |||
| Label | Total HCF: | |||
| TextBox | txtTotalHCF | |||
| Label | Total Gallons: | |||
| TextBox | txtTotalGallons | |||
| Label | First Tier Consumption: | |||
| TextBox | txtFirstTierConsumption | |||
| Label | Second Tier: | |||
| TextBox | txtSecondTierConsumption | |||
| Label | Last Tier: | |||
| TextBox | txtLastTierConsumption | |||
| GroupBox | Consumption Charges | |||
| Label | Water Charges: | |||
| TextBox | txtWaterCharges | |||
| Label | Sewer Charges: | |||
| TextBox | txtSewerCharges | |||
| Label | Environment Charges: | |||
| TextBox | txtEnvironmentCharges | |||
| Label | Service Charges: | |||
| TextBox | txtServiceCharges | |||
| Label | Total Charges: | |||
| TextBox | txtTotalCharges | |||
| GroupBox | Taxes | |||
| Label | Local Taxes: | |||
| TextBox | txtLocalTaxes | |||
| Label | State Taxes: | |||
| TextBox | txtStateTaxes | |||
| GroupBox | Water Bill Payment | |||
| Label | Payment Due Date: | |||
| Date Time Picker | dtpPaymentDueDate | |||
| Label | Amount Due: | |||
| TextBox | txtAmountDue | |||
| Label | Late Payment Due Date: | |||
| Date Time Picker | dtpLatePaymentDueDate | |||
| Label | &Late Amount Due: | |||
| TextBox | txtLateAmountDue | |||
| Button | Save Water Bill | btnSaveWaterBill | FlatStyle: Flat | |
| Button | &Close | btnClose | FlatStyle: Flat | |
Form Properties
| Form Property | Value |
| FormBorderStyle | FixedDialog |
| Text | Stellar Water Point - Water Bill Processing |
| StartPosition | CenterScreen |
| MaximizeBox | False |
| MinimizeBox | False |
using StellarWaterPoint14.Models;
namespace StellarWaterPoint14.WaterBills
{
public partial class Create : Form
{
public Create()
{
InitializeComponent();
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Trim();
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You should first type a valid customer account number, " +
"and then click the Find Customer Account button.");
return;
}
StellarContext db = new();
List<Customer> clients = new();
clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == mtbAccountNumber.Text);
txtAccountName.Text = client!.AccountName;
string? strMeterNumber = client.MeterNumber;
txtAccountType.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
txtZIPCode.Text = client.ZIPCode;
List<WaterMeter> meters = new();
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == strMeterNumber);
txtMeterDetails.Text = meter!.WaterMeterId.ToString() + " - " +
meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
}
}private void dtpMeterReadingEndDate_ValueChanged(object sender, EventArgs e)
{
TimeSpan tsDays = dtpMeterReadingEndDate.Value - dtpMeterReadingStartDate.Value;
txtBillingDays.Text = (tsDays.Days + 1).ToString();
}private void btnEvaluateWaterBill_Click(object sender, EventArgs e)
{
double counterStart = 0, counterEnd = 0;
try
{
counterStart = double.Parse(txtCounterReadingStart.Text);
}
catch (FormatException feCRStart)
{
MsgBox.Regular("You must enter a valid value in the Counter Reading Start text box. " +
"The error produced is: " + feCRStart.Message,
"Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
try
{
counterEnd = double.Parse(txtCounterReadingEnd.Text);
}
catch (FormatException feCREnd)
{
MsgBox.Regular("You must enter a valid value in the Counter Reading End text box. " +
"The error produced is: " + feCREnd.Message,
"Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
double consumption = counterEnd - counterStart;
double gallons = consumption * 748.05;
string strAccountType = txtAccountType.Text[..3];
(double first, double second, double last) tiers = WaterBillManager.CalculateTiers(strAccountType, gallons);
double waterCharges = tiers.first + tiers.second + tiers.last;
double sewerCharges = WaterBillManager.CalculateSewerCharges(strAccountType, waterCharges);
double envCharges = WaterBillManager.CalculateEnvironmentCharges(strAccountType, waterCharges);
double srvCharges = WaterBillManager.CalculateServiceCharges(strAccountType, waterCharges);
double totalCharges = waterCharges + sewerCharges + envCharges + srvCharges;
double localTaxes = WaterBillManager.CalculateLocalTaxes(strAccountType, waterCharges);
double stateTaxes = WaterBillManager.CalculateStateTaxes(strAccountType, waterCharges);
double amtDue = totalCharges + localTaxes + stateTaxes;
txtTotalHCF.Text = consumption.ToString();
txtTotalGallons.Text = ((int)(Math.Ceiling(gallons))).ToString();
txtFirstTierConsumption.Text = tiers.first.ToString("F");
txtSecondTierConsumption.Text = tiers.second.ToString("F");
txtLastTierConsumption.Text = tiers.last.ToString("F");
txtWaterCharges.Text = waterCharges.ToString("F");
txtSewerCharges.Text = sewerCharges.ToString("F");
txtEnvironmentCharges.Text = envCharges.ToString("F");
txtServiceCharges.Text = srvCharges.ToString("F");
txtTotalCharges.Text = totalCharges.ToString("F");
txtLocalTaxes.Text = localTaxes.ToString("F");
txtStateTaxes.Text = stateTaxes.ToString("F");
dtpPaymentDueDate.Value = WaterBillManager.SetPaymentDueDate(strAccountType, dtpMeterReadingEndDate.Value);
txtAmountDue.Text = amtDue.ToString("F");
dtpLatePaymentDueDate.Value = WaterBillManager.SetLatePaymentDueDate(strAccountType, dtpMeterReadingEndDate.Value);
txtLateAmountDue.Text = WaterBillManager.CalculateLateAmountDue(strAccountType, amtDue).ToString("F");
}
}
}using StellarWaterPoint53.Models;
namespace StellarWaterPoint53.WaterBills
{
public partial class Create : Form
{
public Create()
{
InitializeComponent();
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Trim();
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You should first type a valid customer account number, " +
"and then click the Find Customer Account button.");
return;
}
StellarContext db = new();
List<Customer> clients = new();
clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == mtbAccountNumber.Text);
txtAccountName.Text = client!.AccountName;
string? strMeterNumber = client.MeterNumber;
txtAccountType.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
txtZIPCode.Text = client.ZIPCode;
List<WaterMeter> meters = new();
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == strMeterNumber);
txtMeterDetails.Text = meter!.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
private void dtpMeterReadingEndDate_ValueChanged(object sender, EventArgs e)
{
TimeSpan tsDays = dtpMeterReadingEndDate.Value - dtpMeterReadingStartDate.Value;
txtBillingDays.Text = (tsDays.Days + 1).ToString();
}
private void btnEvaluateWaterBill_Click(object sender, EventArgs e)
{
double counterStart = 0, counterEnd = 0;
try
{
counterStart = double.Parse(txtCounterReadingStart.Text);
}
catch (FormatException feCRStart)
{
MsgBox.Regular("You must enter a valid value in the Counter Reading Start text box. " +
"The error produced is: " + feCRStart.Message,
"Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
try
{
counterEnd = double.Parse(txtCounterReadingEnd.Text);
}
catch (FormatException feCREnd)
{
MsgBox.Regular("You must enter a valid value in the Counter Reading End text box. " +
"The error produced is: " + feCREnd.Message,
"Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
double consumption = counterEnd - counterStart;
double gallons = consumption * 748.05;
string strAccountType = txtAccountType.Text[..3];
(double first, double second, double last) tiers = WaterBillManager.CalculateTiers(strAccountType, gallons);
double waterCharges = tiers.first + tiers.second + tiers.last;
double sewerCharges = WaterBillManager.CalculateSewerCharges(strAccountType, waterCharges);
double envCharges = WaterBillManager.CalculateEnvironmentCharges(strAccountType, waterCharges);
double srvCharges = WaterBillManager.CalculateServiceCharges(strAccountType, waterCharges);
double totalCharges = waterCharges + sewerCharges + envCharges + srvCharges;
double localTaxes = WaterBillManager.CalculateLocalTaxes(strAccountType, waterCharges);
double stateTaxes = WaterBillManager.CalculateStateTaxes(strAccountType, waterCharges);
double amtDue = totalCharges + localTaxes + stateTaxes;
txtTotalHCF.Text = consumption.ToString();
txtTotalGallons.Text = ((int)(Math.Ceiling(gallons))).ToString();
txtFirstTierConsumption.Text = tiers.first.ToString("F");
txtSecondTierConsumption.Text = tiers.second.ToString("F");
txtLastTierConsumption.Text = tiers.last.ToString("F");
txtWaterCharges.Text = waterCharges.ToString("F");
txtSewerCharges.Text = sewerCharges.ToString("F");
txtEnvironmentCharges.Text = envCharges.ToString("F");
txtServiceCharges.Text = srvCharges.ToString("F");
txtTotalCharges.Text = totalCharges.ToString("F");
txtLocalTaxes.Text = localTaxes.ToString("F");
txtStateTaxes.Text = stateTaxes.ToString("F");
dtpPaymentDueDate.Value = WaterBillManager.SetPaymentDueDate(strAccountType, dtpMeterReadingEndDate.Value);
txtAmountDue.Text = amtDue.ToString("F");
dtpLatePaymentDueDate.Value = WaterBillManager.SetLatePaymentDueDate(strAccountType, dtpMeterReadingEndDate.Value);
txtLateAmountDue.Text = WaterBillManager.CalculateLateAmountDue(strAccountType, amtDue).ToString("F");
}
private void btnSaveWaterBill_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtBillNumber.Text))
{
MsgBox.Regular("You must type a (unique) bill number for the " +
"water bill you are processing.");
return;
}
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Trim();
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You should first type a valid customer account number, " +
"and then click the Find Customer Account button.");
return;
}
StellarContext db = new();
WaterBill bill = new()
{
BillNumber = int.Parse(txtBillNumber.Text),
AccountNumber = mtbAccountNumber.Text,
MeterReadingStartDate = dtpMeterReadingStartDate.Value,
MeterReadingEndDate = dtpMeterReadingEndDate.Value,
BillingDays = int.Parse(txtBillingDays.Text),
CounterReadingStart = int.Parse(txtCounterReadingStart.Text),
CounterReadingEnd = int.Parse(txtCounterReadingEnd.Text),
TotalHCF = int.Parse(txtTotalHCF.Text),
TotalGallons = int.Parse(txtTotalGallons.Text),
FirstTierConsumption = double.Parse(txtFirstTierConsumption.Text),
SecondTierConsumption = double.Parse(txtSecondTierConsumption.Text),
LastTierConsumption = double.Parse(txtLastTierConsumption.Text),
WaterCharges = double.Parse(txtWaterCharges.Text),
SewerCharges = double.Parse(txtSewerCharges.Text),
EnvironmentCharges = double.Parse(txtEnvironmentCharges.Text),
ServiceCharges = double.Parse(txtServiceCharges.Text),
TotalCharges = double.Parse(txtTotalCharges.Text),
LocalTaxes = double.Parse(txtLocalTaxes.Text),
StateTaxes = double.Parse(txtStateTaxes.Text),
PaymentDueDate = dtpPaymentDueDate.Value,
AmountDue = double.Parse(txtAmountDue.Text),
LatePaymentDueDate = dtpLatePaymentDueDate.Value,
LateAmountDue = double.Parse(txtLateAmountDue.Text)
};
db.WaterBills!.Add(bill);
db.SaveChanges();
Close();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}| Control | (Name) | Other Properties | |
| ListView | lvwWaterBills | No Change | |
| Button | btnProcessWaterBill | FlatStyle: Flat &Process Water Bill... |
|
using System.Data;
using Microsoft.Data.SqlClient;
namespace StellarWaterPoint21.WaterBills
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowWaterBills()
{
. . .
}
private void Central_Load(object sender, EventArgs e)
{
ShowWaterBills();
}
private void btnProcessWaterBill_Click(object sender, EventArgs e)
{
Create create = new();
create.ShowDialog();
ShowWaterBills();
}
}
}| Water Bill # | Account # | Reading Start Date | Reading End Date | Counter Reading Start | Counter Reading End |
| 451474 | 2068-258-9486 | 01/11/2010 | 04/12/2010 | 103943 | 103956 |
| 923633 | 5293-957-3395 | 01/17/2010 | 08/18/2010 | 256945 | 256972 |
| 917829 | 9279-570-8394 | 02/15/2010 | 05/14/2010 | 5205 | 5222 |
| 202666 | 6986-829-3741 | 03/08/2010 | 06/06/2010 | 5679 | 5690 |
Water Bill Details
As mentioned already, a water bill includes many pieces of information. To make it possible for a user to simply view that information, we will create a form that can be used to view the details of a water bill.
Practical Learning: Showing a Water Bill

| Control | Text | Name | Other Properties | ||
| Label | &Water Bill #: | ||||
| TextBox | txtBillNumber | ||||
| Button | &Find Water Bill | btnFindWaterBill | FlatStyle: Flat | ||
| GroupBox | Customer Information | ||||
| Label | &Account #: | ||||
| TextBox | txtAccountNumber | Enabled: False | |||
| Label | Account Name: | ||||
| TextBox | txtAccountName | Enabled: False | |||
| Label | Account Type: | ||||
| TextBox | txtAccountType | ||||
| Label | Address: | Enabled: False | |||
| TextBox | txtAddress | Enabled: False | |||
| TextBox | txtCity | ||||
| TextBox | txtCounty | Enabled: False | |||
| TextBox | txtState | Enabled: False | |||
| TextBox | txtZIPCode | Enabled: False | |||
| Label | _________________________________________________ | ||||
| Label | Meter Details: | ||||
| TextBox | txtMeterDetails | Enabled: False | |||
| GroupBox | Meter Reading | ||||
| Label | Meter &Reading Start Date: | ||||
| Text Box | txtMeterReadingStartDate | Enabled: False | |||
| Label | Meter Reading &End Date: | ||||
| Text Box | txtMeterReadingEndDate | Enabled: False | |||
| Label | Coun&ter Reading Start: | ||||
| TextBox | txtCounterReadingStart | Enabled: False | |||
| Label | Counter Readi&ng End: | ||||
| TextBox | txtCounterReadingEnd | Enabled: False | |||
| GroupBox | Meter Result | ||||
| Label | Billing Days: | ||||
| TextBox | txtBillingDays | Enabled: False | |||
| Label | Total HCF: | ||||
| TextBox | txtTotalHCF | Enabled: False | |||
| Label | Total Gallons: | ||||
| TextBox | txtTotalGallons | Enabled: False | |||
| Label | First Tier Consumption: | ||||
| TextBox | txtFirstTierConsumption | Enabled: False | |||
| Label | Second Tier: | ||||
| TextBox | txtSecondTierConsumption | Enabled: False | |||
| Label | Last Tier: | ||||
| TextBox | txtLastTierConsumption | Enabled: False | |||
| GroupBox | Consumption Charges | ||||
| Label | Water Charges: | ||||
| TextBox | txtWaterCharges | Enabled: False | |||
| Label | Sewer Charges: | ||||
| TextBox | txtSewerCharges | Enabled: False | |||
| Label | Environment Charges: | ||||
| TextBox | txtEnvironmentCharges | Enabled: False | |||
| Label | Service Charges: | ||||
| TextBox | txtServiceCharges | Enabled: False | |||
| Label | Total Charges: | ||||
| TextBox | txtTotalCharges | Enabled: False | |||
| GroupBox | Taxes | ||||
| Label | Local Taxes: | ||||
| TextBox | txtLocalTaxes | Enabled: False | |||
| Label | State Taxes: | ||||
| TextBox | txtStateTaxes | Enabled: False | |||
| GroupBox | Water Bill Payment | ||||
| Label | Payment Due Date: | ||||
| Date Time Picker | dtpPaymentDueDate | Enabled: False | |||
| Label | Amount Due: | ||||
| TextBox | txtAmountDue | Enabled: False | |||
| Label | Late Payment Due Date: | ||||
| Text Box | txtLatePaymentDueDate | Enabled: False | |||
| Label | &Late Amount Due: | ||||
| TextBox | txtLateAmountDue | ||||
| Button | &Close | btnClose | FlatStyle: Flat | ||
Form Properties
| Form Property | Value |
| FormBorderStyle | FixedDialog |
| Text | Stellar Water Point - Water Bill Processing |
| StartPosition | CenterScreen |
| MaximizeBox | False |
| MinimizeBox | False |
using StellarWaterPoint14.Models;
namespace StellarWaterPoint14.WaterBills
{
public partial class Details : Form
{
public Details()
{
InitializeComponent();
}
private void btnFindWaterBill_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtWaterBillNumber.Text))
{
MsgBox.Regular("You must type a (valid) water bill number, " +
"and then click the Find Water Bill button.");
return;
}
StellarContext db = new();
try
{
int billNbr = int.Parse(txtWaterBillNumber.Text);
List<WaterBill> bills = db.WaterBills!.ToList();
try
{
WaterBill? bill = bills.Find(bl => bl.BillNumber == billNbr);
txtWaterBillId.Text = bill!.WaterBillId.ToString();
txtAccountNumber.Text = bill.AccountNumber;
txtMeterReadingStartDate.Text = bill.MeterReadingStartDate.ToLongDateString();
txtMeterReadingEndDate.Text = bill.MeterReadingEndDate.ToLongDateString();
txtBillingDays.Text = bill.BillingDays.ToString();
txtCounterReadingStart.Text = bill.CounterReadingStart.ToString();
txtCounterReadingEnd.Text = bill.CounterReadingEnd.ToString();
txtTotalHCF.Text = bill.TotalHCF.ToString();
txtTotalGallons.Text = bill.TotalGallons.ToString();
txtFirstTierConsumption.Text = bill.FirstTierConsumption.ToString();
txtSecondTierConsumption.Text = bill.SecondTierConsumption.ToString();
txtLastTierConsumption.Text = bill.LastTierConsumption.ToString();
txtWaterCharges.Text = bill.WaterCharges.ToString();
txtSewerCharges.Text = bill.SewerCharges.ToString();
txtEnvironmentCharges.Text = bill.EnvironmentCharges.ToString();
txtServiceCharges.Text = bill.ServiceCharges.ToString();
txtTotalCharges.Text = bill.TotalCharges.ToString();
txtLocalTaxes.Text = bill.LocalTaxes.ToString();
txtStateTaxes.Text = bill.StateTaxes.ToString();
txtAmountDue.Text = bill.AmountDue.ToString();
txtPaymentDueDate.Text = bill.PaymentDueDate.ToLongDateString();
txtLateAmountDue.Text = bill.LateAmountDue.ToString();
txtLatePaymentDueDate.Text = bill.LatePaymentDueDate.ToLongDateString();
List<Customer> clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == txtAccountNumber.Text);
txtAccountName.Text = client!.AccountName;
string? strMeterNumber = client.MeterNumber;
txtAccountType.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
txtZIPCode.Text = client.ZIPCode;
List<WaterMeter> meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == strMeterNumber);
txtMeterDetails.Text = meter!.WaterMeterId.ToString() + " - " +
meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
catch (NullReferenceException nre)
{
MsgBox.Regular("The number you typed doesn't match " +
"any of the water bill numbers in our system." + Environment.NewLine +
"The error produced was: " + nre.Message);
}
}
catch (FormatException fe)
{
MsgBox.Regular("You must type a valid water bill number." + Environment.NewLine +
"The error produced was: " + fe.Message);
}
catch (Exception ex)
{
MsgBox.Regular("Something went wrong trying to get water bill." + Environment.NewLine +
"The error produced was: " + ex.Message);
}
}
}
}private void btnClose_Click(object sender, EventArgs e)
{
Close();
}| Control | (Name) | Other Properties | |
| ListView | lvwWaterBills | No Change | |
| Button | No Change | No Change | |
| Button | btnWaterBillDetails | FlatStyle: Flat Water Bill &Details... |
|
using System.Data;
using Microsoft.Data.SqlClient;
namespace StellarWaterPoint21.WaterBills
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowWaterBills()
{
. . .
}
private void Central_Load(object sender, EventArgs e)
{
ShowWaterBills();
}
private void btnProcessWaterBill_Click(object sender, EventArgs e)
{
Create create = new();
create.ShowDialog();
ShowWaterBills();
}
private void btnViewWaterBill_Click(object sender, EventArgs e)
{
Details details = new();
details.Show();
}
}
}A Water Bill Update
There can be errors or inconsistencies in a water bill. When necessary, such errors or inconsistencies must be corrected. We will create a form that can be used for that.
Practical Learning: Creating a Water Bill Editor
| Control | (Name) | Text | Additional Properties | |
| Button | btnFindWaterBill | &Find | ||
| Button | btnUpdateWaterBill | &Update Water Bill | ||
using StellarWaterPoint14.Models;
namespace StellarWaterPoint14.WaterBills
{
public partial class Editor : Form
{
public Editor()
{
InitializeComponent();
}
private void btnFindWaterBill_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtWaterBillNumber.Text))
{
MsgBox.Regular("You must type a (valid) water bill number, " +
"and then click the Find Water Bill button.");
return;
}
StellarContext db = new();
try
{
int billNbr = int.Parse(txtWaterBillNumber.Text);
List<WaterBill> bills = db.WaterBills!.ToList();
try
{
WaterBill? bill = bills.Find(bl => bl.BillNumber == billNbr);
txtWaterBillId.Text = bill!.WaterBillId.ToString();
mtbAccountNumber.Text = bill.AccountNumber;
dtpMeterReadingStartDate.Value = bill.MeterReadingStartDate;
dtpMeterReadingEndDate.Value = bill.MeterReadingEndDate;
txtBillingDays.Text = bill.BillingDays.ToString();
txtCounterReadingStart.Text = bill.CounterReadingStart.ToString();
txtCounterReadingEnd.Text = bill.CounterReadingEnd.ToString();
txtTotalHCF.Text = bill.TotalHCF.ToString();
txtTotalGallons.Text = bill.TotalGallons.ToString();
txtFirstTierConsumption.Text = bill.FirstTierConsumption.ToString();
txtSecondTierConsumption.Text = bill.SecondTierConsumption.ToString();
txtLastTierConsumption.Text = bill.LastTierConsumption.ToString();
txtWaterCharges.Text = bill.WaterCharges.ToString();
txtSewerCharges.Text = bill.SewerCharges.ToString();
txtEnvironmentCharges.Text = bill.EnvironmentCharges.ToString();
txtServiceCharges.Text = bill.ServiceCharges.ToString();
txtTotalCharges.Text = bill.TotalCharges.ToString();
txtLocalTaxes.Text = bill.LocalTaxes.ToString();
txtStateTaxes.Text = bill.StateTaxes.ToString();
txtAmountDue.Text = bill.AmountDue.ToString();
dtpPaymentDueDate.Value = bill.PaymentDueDate;
txtLateAmountDue.Text = bill.LateAmountDue.ToString();
dtpLatePaymentDueDate.Value = bill.LatePaymentDueDate;
List<Customer> clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == mtbAccountNumber.Text);
txtAccountName.Text = client!.AccountName;
string? strMeterNumber = client.MeterNumber;
txtAccountType.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
txtZIPCode.Text = client.ZIPCode;
List<WaterMeter> meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == strMeterNumber);
txtMeterDetails.Text = meter!.WaterMeterId.ToString() + " - " +
meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
catch (NullReferenceException nre)
{
MsgBox.Regular("The number you typed doesn't match " +
"any of the water bill numbers in our system." + Environment.NewLine +
"The error produced was: " + nre.Message);
}
}
catch (FormatException fe)
{
MsgBox.Regular("You must type a valid water bill number." + Environment.NewLine +
"The error produced was: " + fe.Message);
}
catch (Exception ex)
{
MsgBox.Regular("Something went wrong trying to get water bill." + Environment.NewLine +
"The error produced was: " + ex.Message);
}
}
}
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Trim();
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You should first type a valid customer account number, " +
"and then click the Find Customer Account button.");
return;
}
StellarContext db = new();
List<Customer> clients = new();
clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == mtbAccountNumber.Text);
txtAccountName.Text = client!.AccountName;
string? strMeterNumber = client.MeterNumber;
txtAccountType.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
txtZIPCode.Text = client.ZIPCode;
List<WaterMeter> meters = new();
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == strMeterNumber);
txtMeterDetails.Text = meter!.WaterMeterId.ToString() + " - " +
meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}private void dtpMeterReadingEndDate_ValueChanged(object sender, EventArgs e)
{
TimeSpan tsDays = dtpMeterReadingEndDate.Value - dtpMeterReadingStartDate.Value;
txtBillingDays.Text = (tsDays.Days + 1).ToString();
}private void btnEvaluateWaterBill_Click(object sender, EventArgs e)
{
double counterStart = 0, counterEnd = 0;
try
{
counterStart = double.Parse(txtCounterReadingStart.Text);
}
catch (FormatException feCRStart)
{
MsgBox.Regular("You must enter a valid value in the Counter Reading Start text box. " +
"The error produced is: " + feCRStart.Message);
}
try
{
counterEnd = double.Parse(txtCounterReadingEnd.Text);
}
catch (FormatException feCREnd)
{
MsgBox.Regular("You must enter a valid value in the Counter Reading End text box. " +
"The error produced is: " + feCREnd.Message);
}
double consumption = counterEnd - counterStart;
double gallons = consumption * 748.05;
string strAccountType = txtAccountType.Text[..3];
(double first, double second, double last) tiers = WaterBillManager.CalculateTiers(strAccountType, gallons);
double waterCharges = tiers.first + tiers.second + tiers.last;
double sewerCharges = WaterBillManager.CalculateSewerCharges(strAccountType, waterCharges);
double envCharges = WaterBillManager.CalculateEnvironmentCharges(strAccountType, waterCharges);
double srvCharges = WaterBillManager.CalculateServiceCharges(strAccountType, waterCharges);
double totalCharges = waterCharges + sewerCharges + envCharges + srvCharges;
double localTaxes = WaterBillManager.CalculateLocalTaxes(strAccountType, waterCharges);
double stateTaxes = WaterBillManager.CalculateStateTaxes(strAccountType, waterCharges);
double amtDue = totalCharges + localTaxes + stateTaxes;
txtTotalHCF.Text = consumption.ToString();
txtTotalGallons.Text = ((int)(Math.Ceiling(gallons))).ToString();
txtFirstTierConsumption.Text = tiers.first.ToString("F");
txtSecondTierConsumption.Text = tiers.second.ToString("F");
txtLastTierConsumption.Text = tiers.last.ToString("F");
txtWaterCharges.Text = waterCharges.ToString("F");
txtSewerCharges.Text = sewerCharges.ToString("F");
txtEnvironmentCharges.Text = envCharges.ToString("F");
txtServiceCharges.Text = srvCharges.ToString("F");
txtTotalCharges.Text = totalCharges.ToString("F");
txtLocalTaxes.Text = localTaxes.ToString("F");
txtStateTaxes.Text = stateTaxes.ToString("F");
dtpPaymentDueDate.Value = WaterBillManager.SetPaymentDueDate(strAccountType, dtpMeterReadingEndDate.Value);
txtAmountDue.Text = amtDue.ToString("F");
dtpLatePaymentDueDate.Value = WaterBillManager.SetLatePaymentDueDate(strAccountType, dtpMeterReadingEndDate.Value);
txtLateAmountDue.Text = WaterBillManager.CalculateLateAmountDue(strAccountType, amtDue).ToString("F");
}private void btnUpdateWaterBill_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtWaterBillNumber.Text))
{
MsgBox.Regular("You must type a (unique) bill number for the " +
"water bill you are processing.");
return;
}
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Trim();
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You should first type a valid customer account number, " +
"and then click the Find Customer Account button.");
return;
}
int billNbr = int.Parse(txtWaterBillNumber.Text);
StellarContext db = new();
List<WaterBill> bills = db.WaterBills!.ToList();
WaterBill? bill = bills.Find(bl => bl.BillNumber == billNbr);
if (bill is null)
{
MsgBox.Regular("There is no water bill with the bill number you typed.");
return;
}
bill.AccountNumber = mtbAccountNumber.Text;
bill.MeterReadingStartDate = dtpMeterReadingStartDate.Value;
bill.MeterReadingEndDate = dtpMeterReadingEndDate.Value;
bill.BillingDays = int.Parse(txtBillingDays.Text);
bill.CounterReadingStart = int.Parse(txtCounterReadingStart.Text);
bill.CounterReadingEnd = int.Parse(txtCounterReadingEnd.Text);
bill.TotalHCF = int.Parse(txtTotalHCF.Text);
bill.TotalGallons = int.Parse(txtTotalGallons.Text);
bill.FirstTierConsumption = double.Parse(txtFirstTierConsumption.Text);
bill.SecondTierConsumption = double.Parse(txtSecondTierConsumption.Text);
bill.LastTierConsumption = double.Parse(txtLastTierConsumption.Text);
bill.WaterCharges = double.Parse(txtWaterCharges.Text);
bill.SewerCharges = double.Parse(txtSewerCharges.Text);
bill.EnvironmentCharges = double.Parse(txtEnvironmentCharges.Text);
bill.ServiceCharges = double.Parse(txtServiceCharges.Text);
bill.TotalCharges = double.Parse(txtTotalCharges.Text);
bill.LocalTaxes = double.Parse(txtLocalTaxes.Text);
bill.StateTaxes = double.Parse(txtStateTaxes.Text);
bill.PaymentDueDate = dtpPaymentDueDate.Value;
bill.AmountDue = double.Parse(txtAmountDue.Text);
bill.LatePaymentDueDate = dtpLatePaymentDueDate.Value;
bill.LateAmountDue = double.Parse(txtLateAmountDue.Text);
db.SaveChanges();
Close();
}using StellarWaterPoint14.Models;
namespace StellarWaterPoint14.WaterBills
{
public partial class Editor : Form
{
public Editor()
{
InitializeComponent();
}
private void btnFindWaterBill_Click(object sender, EventArgs e)
{
/* Before doing anything, first find out whether the user entered
* some value in the bill number text box. */
if (string.IsNullOrEmpty(txtWaterBillNumber.Text))
{
// If the Bill Number text box is empty, ...
MsgBox.Regular("You must type a (valid) water bill number, " +
"and then click the Find Water Bill button.");
// There is nothing to do. So, stop!
return;
}
// Get a reference to the database context
StellarContext db = new();
/* Use Exception Handling in anticipation of bad occurrences.
* Check the "catch" sections. */
try
{
// Get the value that the user typed. Consider it an integer
int billNbr = int.Parse(txtWaterBillNumber.Text);
/* Get a list of water bills, convert it to a List<> object,
* and store it in a List<> variable named bills. */
List<WaterBill> bills = db.WaterBills!.ToList();
try
{
/* Using the list of water bills, find a water bill
* whose number is the same as the number the user typed. */
WaterBill? bill = bills.Find(bl => bl.BillNumber == billNbr);
// If a water bill was found, get its values and display them in the controls on the form.
txtWaterBillId.Text = bill!.WaterBillId.ToString();
mtbAccountNumber.Text = bill.AccountNumber;
dtpMeterReadingStartDate.Value = bill.MeterReadingStartDate;
dtpMeterReadingEndDate.Value = bill.MeterReadingEndDate;
txtBillingDays.Text = bill.BillingDays.ToString();
txtCounterReadingStart.Text = bill.CounterReadingStart.ToString();
txtCounterReadingEnd.Text = bill.CounterReadingEnd.ToString();
txtTotalHCF.Text = bill.TotalHCF.ToString();
txtTotalGallons.Text = bill.TotalGallons.ToString();
txtFirstTierConsumption.Text = bill.FirstTierConsumption.ToString();
txtSecondTierConsumption.Text = bill.SecondTierConsumption.ToString();
txtLastTierConsumption.Text = bill.LastTierConsumption.ToString();
txtWaterCharges.Text = bill.WaterCharges.ToString();
txtSewerCharges.Text = bill.SewerCharges.ToString();
txtEnvironmentCharges.Text = bill.EnvironmentCharges.ToString();
txtServiceCharges.Text = bill.ServiceCharges.ToString();
txtTotalCharges.Text = bill.TotalCharges.ToString();
txtLocalTaxes.Text = bill.LocalTaxes.ToString();
txtStateTaxes.Text = bill.StateTaxes.ToString();
txtAmountDue.Text = bill.AmountDue.ToString();
dtpPaymentDueDate.Value = bill.PaymentDueDate;
txtLateAmountDue.Text = bill.LateAmountDue.ToString();
dtpLatePaymentDueDate.Value = bill.LatePaymentDueDate;
// Get the details of the customer whose bill this is, and display them in the text boxes
List<Customer> clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == mtbAccountNumber.Text);
txtAccountName.Text = client!.AccountName;
string? strMeterNumber = client.MeterNumber;
txtAccountType.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
txtZIPCode.Text = client.ZIPCode;
// Get the details of the water meter that the cusomter is using, ...
List<WaterMeter> meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == strMeterNumber);
// and display those details in the Meter Details text box
txtMeterDetails.Text = meter!.WaterMeterId.ToString() + " - " +
meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
catch (NullReferenceException nre)
{
/* If the user typed some digits, find out whether that number corresponds to
* a valid water bill number. If the number the user typed doesn't match
* any bill number, then the application produced a NullReferenceException exception */
MsgBox.Regular("The number you typed doesn't match " +
"any of the water bill numbers in our system." + Environment.NewLine +
"The error produced was: " + nre.Message);
}
}
catch (FormatException fe)
{
/* The bill number is an integer. If the user typed anything other
* than digits, then the application produced a FormatException exception. */
MsgBox.Regular("You must type a valid water bill number." + Environment.NewLine +
"The error produced was: " + fe.Message);
}
catch (Exception ex)
{
MsgBox.Regular("Something went wrong trying to get water bill." + Environment.NewLine +
"The error produced was: " + ex.Message);
}
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Trim();
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You should first type a valid customer account number, " +
"and then click the Find Customer Account button.");
return;
}
StellarContext db = new();
List<Customer> clients = new();
clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == mtbAccountNumber.Text);
txtAccountName.Text = client!.AccountName;
string? strMeterNumber = client.MeterNumber;
txtAccountType.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
txtZIPCode.Text = client.ZIPCode;
List<WaterMeter> meters = new();
meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == strMeterNumber);
txtMeterDetails.Text = meter!.WaterMeterId.ToString() + " - " +
meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
private void dtpMeterReadingEndDate_ValueChanged(object sender, EventArgs e)
{
TimeSpan tsDays = dtpMeterReadingEndDate.Value - dtpMeterReadingStartDate.Value;
txtBillingDays.Text = (tsDays.Days + 1).ToString();
}
private void btnEvaluateWaterBill_Click(object sender, EventArgs e)
{
double counterStart = 0, counterEnd = 0;
try
{
counterStart = double.Parse(txtCounterReadingStart.Text);
}
catch (FormatException feCRStart)
{
MsgBox.Regular("You must enter a valid value in the Counter Reading Start text box. " +
"The error produced is: " + feCRStart.Message);
}
try
{
counterEnd = double.Parse(txtCounterReadingEnd.Text);
}
catch (FormatException feCREnd)
{
MsgBox.Regular("You must enter a valid value in the Counter Reading End text box. " +
"The error produced is: " + feCREnd.Message);
}
double consumption = counterEnd - counterStart;
double gallons = consumption * 748.05;
string strAccountType = txtAccountType.Text[..3];
(double first, double second, double last) tiers = WaterBillManager.CalculateTiers(strAccountType, gallons);
double waterCharges = tiers.first + tiers.second + tiers.last;
double sewerCharges = WaterBillManager.CalculateSewerCharges(strAccountType, waterCharges);
double envCharges = WaterBillManager.CalculateEnvironmentCharges(strAccountType, waterCharges);
double srvCharges = WaterBillManager.CalculateServiceCharges(strAccountType, waterCharges);
double totalCharges = waterCharges + sewerCharges + envCharges + srvCharges;
double localTaxes = WaterBillManager.CalculateLocalTaxes(strAccountType, waterCharges);
double stateTaxes = WaterBillManager.CalculateStateTaxes(strAccountType, waterCharges);
double amtDue = totalCharges + localTaxes + stateTaxes;
txtTotalHCF.Text = consumption.ToString();
txtTotalGallons.Text = ((int)(Math.Ceiling(gallons))).ToString();
txtFirstTierConsumption.Text = tiers.first.ToString("F");
txtSecondTierConsumption.Text = tiers.second.ToString("F");
txtLastTierConsumption.Text = tiers.last.ToString("F");
txtWaterCharges.Text = waterCharges.ToString("F");
txtSewerCharges.Text = sewerCharges.ToString("F");
txtEnvironmentCharges.Text = envCharges.ToString("F");
txtServiceCharges.Text = srvCharges.ToString("F");
txtTotalCharges.Text = totalCharges.ToString("F");
txtLocalTaxes.Text = localTaxes.ToString("F");
txtStateTaxes.Text = stateTaxes.ToString("F");
dtpPaymentDueDate.Value = WaterBillManager.SetPaymentDueDate(strAccountType, dtpMeterReadingEndDate.Value);
txtAmountDue.Text = amtDue.ToString("F");
dtpLatePaymentDueDate.Value = WaterBillManager.SetLatePaymentDueDate(strAccountType, dtpMeterReadingEndDate.Value);
txtLateAmountDue.Text = WaterBillManager.CalculateLateAmountDue(strAccountType, amtDue).ToString("F");
}
private void btnUpdateWaterBill_Click(object sender, EventArgs e)
{
// Make sure the user typed a (valid) bill number
if (string.IsNullOrEmpty(txtWaterBillNumber.Text))
{
MsgBox.Regular("You must type the bill number for the " +
"water bill you are processing.");
return;
}
// Make sure the user provided a customer's account number
string strAccountNumber = mtbAccountNumber.Text.Replace("-", "").Trim();
if (string.IsNullOrEmpty(strAccountNumber))
{
MsgBox.Regular("You should first type a valid customer account number, " +
"and then click the Find Customer Account button.");
return;
}
// Get the value that the user typed. Consider it an integer
int billNbr = int.Parse(txtWaterBillNumber.Text);
StellarContext db = new();
List<WaterBill> bills = db.WaterBills!.ToList();
/* Using the list of water bills, find a water bill
* whose number is the same as the number the user typed. */
WaterBill? bill = bills.Find(bl => bl.BillNumber == billNbr);
// Check whether the user provided a valid bill number
if (bill is null)
{
MsgBox.Regular("There is no water bill with the bill number you typed.");
// If the provided bill number did not lead to a valid water bill, don't do anything
return;
}
/* Since the water bill was found, if the user changed
* its values, update each value of water bille*/
bill.AccountNumber = mtbAccountNumber.Text;
bill.MeterReadingStartDate = dtpMeterReadingStartDate.Value;
bill.MeterReadingEndDate = dtpMeterReadingEndDate.Value;
bill.BillingDays = int.Parse(txtBillingDays.Text);
bill.CounterReadingStart = int.Parse(txtCounterReadingStart.Text);
bill.CounterReadingEnd = int.Parse(txtCounterReadingEnd.Text);
bill.TotalHCF = int.Parse(txtTotalHCF.Text);
bill.TotalGallons = int.Parse(txtTotalGallons.Text);
bill.FirstTierConsumption = double.Parse(txtFirstTierConsumption.Text);
bill.SecondTierConsumption = double.Parse(txtSecondTierConsumption.Text);
bill.LastTierConsumption = double.Parse(txtLastTierConsumption.Text);
bill.WaterCharges = double.Parse(txtWaterCharges.Text);
bill.SewerCharges = double.Parse(txtSewerCharges.Text);
bill.EnvironmentCharges = double.Parse(txtEnvironmentCharges.Text);
bill.ServiceCharges = double.Parse(txtServiceCharges.Text);
bill.TotalCharges = double.Parse(txtTotalCharges.Text);
bill.LocalTaxes = double.Parse(txtLocalTaxes.Text);
bill.StateTaxes = double.Parse(txtStateTaxes.Text);
bill.PaymentDueDate = dtpPaymentDueDate.Value;
bill.AmountDue = double.Parse(txtAmountDue.Text);
bill.LatePaymentDueDate = dtpLatePaymentDueDate.Value;
bill.LateAmountDue = double.Parse(txtLateAmountDue.Text);
/* If/Since one or more values of the water bill
* had been changed, save the updated water bill. */
db.SaveChanges();
// Return to the WaterBills.Central form to display an updated list of water bills
Close();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}| Control | (Name) | Text | |
| ListView | lvwWaterBills | ||
| Button | btnNewWaterBill | ||
| Button | btnViewWaterBill | ||
| Button | btnEditWaterBill | &Edit Water Bill... | |
using System.Data;
using Microsoft.Data.SqlClient;
namespace StellarWaterPoint21.WaterBills
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowWaterBills()
{
. . .
}
private void Central_Load(object sender, EventArgs e)
{
ShowWaterBills();
}
private void btnProcessWaterBill_Click(object sender, EventArgs e)
{
Create create = new();
create.ShowDialog();
ShowWaterBills();
}
private void btnViewWaterBill_Click(object sender, EventArgs e)
{
Details details = new();
details.Show();
}
private void btnEditWaterBill_Click(object sender, EventArgs e)
{
}
}
}Account #: 9249-379-6848 and click Find Customer Account Meter Reading Start Date: 1/19/2010 Meter Reading End Date: 4/17/2010 Counter Reading Start: 256953 Counter Reading End: 256966
For any reason that is judged valuable, it may become desirable or necessary to cancel a water bill. We will create a form that can be used to delete a water bill.
Practical Learning: Editing/Updating a Record
| Control | (Name) | Text | |
| Button | btnDeleteWaterBill | FlatStyle: Flat &Delete Water Bill |
|
using StellarWaterPoint53.Models;
namespace StellarWaterPoint53.WaterBills
{
public partial class Delete : Form
{
public Delete()
{
InitializeComponent();
}
private void btnFindWaterBill_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtBillNumber.Text))
{
// If the Bill Number text box is empty, ...
MsgBox.Regular("You must type a (valid) water bill number, " +
"and then click the Find Water Bill button.");
// There is nothing to do. So, stop!
return;
}
/* Use Exception Handling in anticipation of bad occurrences.
* Check the "catch" sections. */
try
{
// Get a reference to the database context
StellarContext db = new();
// Get the value that the user typed. Consider it an integer
int billNbr = int.Parse(txtBillNumber.Text);
/* Get a list of water bills, convert it to a List<> object,
* and store it in a List<> variable named bills. */
List<WaterBill> bills = db.WaterBills!.ToList();
try
{
/* Using the list of water bills, find a water bill
* whose number is the same as the number the user typed. */
WaterBill? bill = bills.Find(bl => bl.BillNumber == billNbr);
// If a water bill was found, get its values and display them in the text boxes on the form.
txtAccountNumber.Text = bill!.AccountNumber;
txtMeterReadingStartDate.Text = bill.MeterReadingStartDate.ToLongDateString();
txtMeterReadingEndDate.Text = bill.MeterReadingEndDate.ToLongDateString();
txtBillingDays.Text = bill.BillingDays.ToString();
txtCounterReadingStart.Text = bill.CounterReadingStart.ToString();
txtCounterReadingEnd.Text = bill.CounterReadingEnd.ToString();
txtTotalHCF.Text = bill.TotalHCF.ToString();
txtTotalGallons.Text = bill.TotalGallons.ToString();
txtFirstTierConsumption.Text = bill.FirstTierConsumption.ToString();
txtSecondTierConsumption.Text = bill.SecondTierConsumption.ToString();
txtLastTierConsumption.Text = bill.LastTierConsumption.ToString();
txtWaterCharges.Text = bill.WaterCharges.ToString();
txtSewerCharges.Text = bill.SewerCharges.ToString();
txtEnvironmentCharges.Text = bill.EnvironmentCharges.ToString();
txtServiceCharges.Text = bill.ServiceCharges.ToString();
txtTotalCharges.Text = bill.TotalCharges.ToString();
txtLocalTaxes.Text = bill.LocalTaxes.ToString();
txtStateTaxes.Text = bill.StateTaxes.ToString();
txtAmountDue.Text = bill.AmountDue.ToString();
txtPaymentDueDate.Text = bill.PaymentDueDate.ToLongDateString();
txtLateAmountDue.Text = bill.LateAmountDue.ToString();
txtLatePaymentDueDate.Text = bill.LatePaymentDueDate.ToLongDateString();
// Get the details of the customer who bill this is, and display them in the text boxes
List<Customer> clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == txtAccountNumber.Text);
txtAccountName.Text = client!.AccountName;
string? strMeterNumber = client.MeterNumber;
txtAccountType.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
txtZIPCode.Text = client.ZIPCode;
// Get the details of the water meter that the cusomter is using, ...
List<WaterMeter> meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == strMeterNumber);
// and display those details in the Meter Details text box
txtMeterDetails.Text = meter!.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
catch (NullReferenceException nre)
{
/* If the user typed some digits, find out whether that number corresponds to
* a valid water bill number. If the number the user typed doesn't match
* any bill number, then the application produced a NullReferenceException exception */
MsgBox.Regular("The number you typed doesn't match " +
"any of the water bill numbers in our system." + Environment.NewLine +
"The error produced was: " + nre.Message);
}
}
catch (FormatException fe)
{
/* The bill number is an integer. If the user typed anything other
*than digits, then the application produced a FormatException exception. */
MsgBox.Regular("You must type a valid water bill number." + Environment.NewLine +
"The error produced was: " + fe.Message);
}
catch (Exception ex)
{
MsgBox.Regular("Something went wrong trying to get water bill." + Environment.NewLine +
"The error produced was: " + ex.Message);
}
}
}
}using StellarWaterPoint53.Models;
namespace StellarWaterPoint53.WaterBills
{
public partial class Delete : Form
{
public Delete()
{
InitializeComponent();
}
private void btnFindWaterBill_Click(object sender, EventArgs e)
{
. . .
}
private void btnDeleteWaterBill_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtBillNumber.Text))
{
MsgBox.Regular("You must type the bill number of the water bill you " +
"want to delete. Then click the Delete Water Bill button.");
return;
}
int billNbr = int.Parse(txtBillNumber.Text);
StellarContext db = new();
List<WaterBill> bills = db.WaterBills!.ToList();
WaterBill? bill = bills.Find(bl => bl.BillNumber == billNbr);
if (bill is null)
{
MsgBox.Regular("There is no water bill with the bill number you typed.");
return;
}
string strQuestion = "Are you sure you want to delete this " +
"water bill (the operation cannot be undone)?";
if (MsgBox.Question(strQuestion) == DialogResult.Yes)
{
db.WaterBills!.Remove(bill);
db.SaveChanges();
}
Close();
}
}
}using StellarWaterPoint53.Models;
namespace StellarWaterPoint53.WaterBills
{
public partial class Delete : Form
{
public Delete()
{
InitializeComponent();
}
private void btnFindWaterBill_Click(object sender, EventArgs e)
{
/* Before doing anything, first find out whether the user entered
* some value in the bill number text box. */
if (string.IsNullOrEmpty(txtBillNumber.Text))
{
// If the Bill Number text box is empty, ...
MsgBox.Regular("You must type a (valid) water bill number, " +
"and then click the Find Water Bill button.");
// There is nothing to do. So, stop!
return;
}
/* Use Exception Handling in anticipation of bad occurrences.
* Check the "catch" sections. */
try
{
// Get a reference to the database context
StellarContext db = new();
// Get the value that the user typed. Consider it an integer
int billNbr = int.Parse(txtBillNumber.Text);
/* Get a list of water bills, convert it to a List<> object,
* and store it in a List<> variable named bills. */
List<WaterBill> bills = db.WaterBills!.ToList();
try
{
/* Using the list of water bills, find a water bill
* whose number is the same as the number the user typed. */
WaterBill? bill = bills.Find(bl => bl.BillNumber == billNbr);
// If a water bill was found, get its values and display them in the text boxes on the form.
txtAccountNumber.Text = bill!.AccountNumber;
txtMeterReadingStartDate.Text = bill.MeterReadingStartDate.ToLongDateString();
txtMeterReadingEndDate.Text = bill.MeterReadingEndDate.ToLongDateString();
txtBillingDays.Text = bill.BillingDays.ToString();
txtCounterReadingStart.Text = bill.CounterReadingStart.ToString();
txtCounterReadingEnd.Text = bill.CounterReadingEnd.ToString();
txtTotalHCF.Text = bill.TotalHCF.ToString();
txtTotalGallons.Text = bill.TotalGallons.ToString();
txtFirstTierConsumption.Text = bill.FirstTierConsumption.ToString();
txtSecondTierConsumption.Text = bill.SecondTierConsumption.ToString();
txtLastTierConsumption.Text = bill.LastTierConsumption.ToString();
txtWaterCharges.Text = bill.WaterCharges.ToString();
txtSewerCharges.Text = bill.SewerCharges.ToString();
txtEnvironmentCharges.Text = bill.EnvironmentCharges.ToString();
txtServiceCharges.Text = bill.ServiceCharges.ToString();
txtTotalCharges.Text = bill.TotalCharges.ToString();
txtLocalTaxes.Text = bill.LocalTaxes.ToString();
txtStateTaxes.Text = bill.StateTaxes.ToString();
txtAmountDue.Text = bill.AmountDue.ToString();
txtPaymentDueDate.Text = bill.PaymentDueDate.ToLongDateString();
txtLateAmountDue.Text = bill.LateAmountDue.ToString();
txtLatePaymentDueDate.Text = bill.LatePaymentDueDate.ToLongDateString();
// Get the details of the customer who bill this is, and display them in the text boxes
List<Customer> clients = db.Customers!.ToList();
Customer? client = clients.Find(cl => cl.AccountNumber == txtAccountNumber.Text);
txtAccountName.Text = client!.AccountName;
string? strMeterNumber = client.MeterNumber;
txtAccountType.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
txtZIPCode.Text = client.ZIPCode;
// Get the details of the water meter that the cusomter is using, ...
List<WaterMeter> meters = db.WaterMeters!.ToList();
WaterMeter? meter = meters.Find(mtr => mtr.MeterNumber == strMeterNumber);
// and display those details in the Meter Details text box
txtMeterDetails.Text = meter!.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
catch (NullReferenceException nre)
{
/* If the user typed some digits, find out whether that number corresponds to
* a valid water bill number. If the number the user typed doesn't match
* any bill number, then the application produced a NullReferenceException exception */
MsgBox.Regular("The number you typed doesn't match " +
"any of the water bill numbers in our system." + Environment.NewLine +
"The error produced was: " + nre.Message);
}
}
catch (FormatException fe)
{
/* The bill number is an integer. If the user typed anything other
*than digits, then the application produced a FormatException exception. */
MsgBox.Regular("You must type a valid water bill number." + Environment.NewLine +
"The error produced was: " + fe.Message);
}
catch (Exception ex)
{
MsgBox.Regular("Something went wrong trying to get water bill." + Environment.NewLine +
"The error produced was: " + ex.Message);
}
}
private void btnDeleteWaterBill_Click(object sender, EventArgs e)
{
// Make sure the user typed a (valid) bill number
if (string.IsNullOrEmpty(txtBillNumber.Text))
{
MsgBox.Regular("You must type the bill number of the water bill you " +
"want to delete. Then click the Delete Water Bill button.");
return;
}
// Get the value that the user typed. Consider it an integer
int billNbr = int.Parse(txtBillNumber.Text);
StellarContext db = new();
List<WaterBill> bills = db.WaterBills!.ToList();
/* Using the list of water bills, find a water bill
* whose number is the same as the number the user typed. */
WaterBill? bill = bills.Find(bl => bl.BillNumber == billNbr);
// Check whether the user provided a valid bill number
if (bill is null)
{
MsgBox.Regular("There is no water bill with the bill number you typed.");
// If the provided bill number did not lead to a valid water bill, don't do anything.
return;
}
string strQuestion = "Are you sure you want to delete this " +
"water bill (the operation cannot be undone)?";
// If the user really wants to delete the water bill, ...
if (MsgBox.Question(strQuestion) == DialogResult.Yes)
{
//... then delete that water bill
db.WaterBills!.Remove(bill);
// Now that the table has changed, save it
db.SaveChanges();
}
// Return to the WaterBills.Central form to display an updated list of water bills
Close();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}| Control | (Name) | Text | Other Properties | |
| ListView | lvwWaterBills | Anchor: Bottom, Top, Bottom, Left, Right | ||
| Button | btnProcessWaterBill | &Process Water Bill... | FlatStyle: Flat Anchor: Bottom, Right |
|
| Button | btnViewWaterBill | &View Water Bill... | FlatStyle: Flat Anchor: Bottom, Right |
|
| Button | btnEditWaterBill | &Edit Water Bill... | FlatStyle: Flat Anchor: Bottom, Right |
|
| Button | btnDeleteWaterBill | &Delete Water Bill... | FlatStyle: Flat Anchor: Bottom, Right |
|
| Button | btnClose | &Close | FlatStyle: Flat Anchor: Bottom, Right |
|
using StellarWaterPoint53.Models;
namespace StellarWaterPoint53.WaterBills
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowWaterBills()
{
// Get a reference to the database context
StellarContext db = new();
// Get the customers records and store them in a List<> list named clients
List<Customer> clients = [.. db.Customers!];
// Get the water bills and store them in a List<> list named bills
List<WaterBill> bills = [.. db.WaterBills!];
/* If you are planning to display the water bills records in
* the list view, first remove everything from that list view.*/
lvwWaterBills.Items.Clear();
// Visit each record from the list of water bills
foreach (WaterBill bill in bills)
{
// Access the WaterBillId value and store it in the first column
ListViewItem lviWaterBill = new ListViewItem(bill.WaterBillId.ToString());
// Display the bill number in the second column
lviWaterBill.SubItems.Add(bill.BillNumber.ToString());
// Find the customer whose water bill is being accessed
Customer? client = clients.Find(cl => cl.AccountNumber == bill.AccountNumber);
/* We will display a short summary of the customer account.
* We will display the account number, the account name, the account type,
* and the meter number of water meter associated with the account. */
lviWaterBill.SubItems.Add(bill.AccountNumber + " - " +
client!.AccountName +
", Type: " + client.AccountType![..3] +
", (Mtr #: " + client.MeterNumber + ")");
/* Because a water bill contains many pieces of information, in the list view,
* we will display only some information such as the meter reading start date,
* the meter reading end date, the number of days included in the bill,
* the counter reading start number, the counter reading end number,
* the total HCF value, the total number of gallons consumed,
* the payment due date, and the amount due. */
lviWaterBill.SubItems.Add(bill.MeterReadingStartDate.ToShortDateString());
lviWaterBill.SubItems.Add(bill.MeterReadingEndDate.ToShortDateString());
lviWaterBill.SubItems.Add(bill.BillingDays.ToString());
lviWaterBill.SubItems.Add(bill.CounterReadingStart.ToString());
lviWaterBill.SubItems.Add(bill.CounterReadingEnd.ToString());
lviWaterBill.SubItems.Add(bill.TotalHCF.ToString());
lviWaterBill.SubItems.Add(bill.TotalGallons.ToString());
lviWaterBill.SubItems.Add(bill.PaymentDueDate.ToShortDateString());
lviWaterBill.SubItems.Add(bill.AmountDue.ToString());
lvwWaterBills.Items.Add(lviWaterBill);
}
}
private void Central_Load(object sender, EventArgs e)
{
ShowWaterBills();
}
private void btnProcessWaterBill_Click(object sender, EventArgs e)
{
Create create = new Create();
create.ShowDialog();
ShowWaterBills();
}
private void btnViewWaterBill_Click(object sender, EventArgs e)
{
Details details = new Details();
details.Show();
}
private void btnEditWaterBill_Click(object sender, EventArgs e)
{
Editor editor = new Editor();
editor.ShowDialog();
ShowWaterBills();
}
private void btnDeleteWaterBill_Click(object sender, EventArgs e)
{
Delete delete = new Delete();
delete.ShowDialog();
ShowWaterBills();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}|
|
|||
| Home | Copyright © 2003-2026, FunctionX | Tuesday 20 May 2025, 07:36 | |
|
|
|||