Application Setup

Introduction

.

Practical LearningPractical Learning: Introducing the Application

  1. Start Microsoft Visual Studio
  2. In the Visual Studio 2022 dialog box, click Create a New Project
  3. In the Create a New Project dialog box, in the Languages combo box, select C#
  4. In the list of projects templates, click Windows Forms App
  5. Click Next
  6. Change the Project Name to StellarWaterPoint4
  7. In the Framework combo box, select the highest version: .NET 9.0 (Standard Term Suppot)
  8. Click Create
  9. To create a folder, in the Solution Explorer -> Add -> Folder
  10. Type Models as the name of the folder
  11. To create a class, in the Solution Explorer, right-click Models -> Add -> Class...
  12. On the main menu, click Project -> Add Class...
  13. Replace the name with AccountType
  14. Click Add
  15. Change the code as follows:
    namespace StellarWaterPoint4.Models
    {
        internal readonly record struct AccountType
        {
            public readonly string? TypeCode       { get; init; }
            public readonly string? TypeDecription { get; init; }
        }
    }
  16. In the Solution Explorer, right-click Models -> Add -> Class
  17. Set the file Name to Repository
  18. Click Add

The Main Form of the Application

.

Practical LearningPractical Learning: Preparing the Main Form of the Application

  1. In the Solution Explorer, right-click Form1.cs and click Rename
  2. Type WaterDistribution (to get WaterDistribution.cs) and press Enter
  3. Read the message on the message box and click Yes
  4. Click the body of the form to make sure it is selected.
    In the Properties window, change the following characteristics:
    Text: Stellar Water Point
    StartPosition: CenterScreen
    MaximizeBox: False
  5. Double-click an unoccupied area of the form to generate its Load event
  6. Implement the event as follows:
    namespace StellarWaterPoint4
    {
        public partial class WaterDistribution : Form
        {
            public WaterDistribution()
            {
                InitializeComponent();
            }
    
            private void WaterDistribution_Load(object sender, EventArgs e)
            {
                Directory.CreateDirectory(@"E:\Stellar Water Point2");
    
                Repository.CreateAccountsTypes();
            }
        }
    }

Water Meters

Introduction

.

Practical LearningPractical Learning: Displaying Water Meters

  1. To create a class, in the Solution Explorer, right-click Models -> Add -> Class...
  2. On the main menu, click Project -> Add Class...
  3. Replace the name with WaterMeter
  4. Click Add
  5. Change the code as follows:
    namespace StellarWaterPoint4.Models
    {
        internal record WaterMeter
        {
            public string? MeterNumber { get; set; }
            public string? Make        { get; set; }
            public string? Model       { get; set; }
            public string? MeterSize   { get; set; }
        }
    }
  6. In the Solution Explorer, right-click StellarWaterPoint1 -> Add -> New Folder
  7. Type WaterMeters as the name of the folder

Displaying Water Meters

To let the user see a list of the water meters in the database, we will use a form equipped with a list view.

Practical LearningPractical Learning: Displaying Water Meters

  1. To create a form, in the Solution Explorer, right-click WaterMeters -> Add -> Form (Windows Forms)...
  2. For the Name of the form, type Central
  3. Click Add
  4. In the Toolbox, click the ListView button and click the form
  5. On the form, right-click the list view and click Edit Columns...
  6. Create the columns as follows:
    (Name) Text Width TextAlign
    colWaterMeterId Id 40  
    colMeterNumber Meter # 150 Center
    colMake Make 300  
    colModel Model 150  
    colMeterSize Meter Size 150  
  7. Click OK
  8. Position and resize the list view on the form as follows:

    Stellar Water Point - Water Meters

    Control (Name) Other Properties
    ListView List View lvwWaterMeters FullRowSelect: True
    GridLines: True
    View: Details
  9. Doubte-click an unoccupied area of the form to generate its Load event
  10. Change the document as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.WaterMeters
    {
        public partial class Central : Form
        {
            public Central()
            {
                InitializeComponent();
            }
    
            private void ShowWaterMeters()
            {
                string strWaterMeters = string.Empty;
                List<WaterMeter> waterMeters = new List<WaterMeter>();
                string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                if (fiWaterMeters.Exists == true)
                {
                    using (TextReader trWaterMeters = new StreamReader(fiWaterMeters.FullName))
                    {
                        strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                        waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                        int counter = 1;
    
                        lvwWaterMeters.Items.Clear();
    
                        foreach (WaterMeter wm in waterMeters)
                        {
                            ListViewItem lviWaterMeter = new ListViewItem(counter++.ToString());
    
                            lviWaterMeter.SubItems.Add(wm.MeterNumber);
                            lviWaterMeter.SubItems.Add(wm.Make);
                            lviWaterMeter.SubItems.Add(wm.Model);
                            lviWaterMeter.SubItems.Add(wm.MeterSize);
    
                            lvwWaterMeters.Items.Add(lviWaterMeter);
                        }
                    }
                }
            }
    
            private void Central_Load(object sender, EventArgs e)
            {
                ShowWaterMeters();
            }
        }
    }
  11. In the Solution Explorer, double-click WaterDistribution.cs to display the main form of the application
  12. From the Toolbox, add a button to the form
  13. From the Properties window, change the characteristics of the button as follows:

    Stellar Water Point

    Control (Name) Text Font
    Button Button btnWaterMeters &Water Meters... Times New Roman, 24pt, style=Bold
  14. Double-click the &Water Meters button
  15. Impliment the event as follows:
    namespace StellarWaterPoint3
    {
        public partial class WaterDistribution : Form
        {
            public WaterDistribution()
            {
                InitializeComponent();
            }
    
            private void btnWaterMeters_Click(object sender, EventArgs e)
            {
                WaterMeters.Central central = new WaterMeters.Central();
    
                central.ShowDialog();
            }
        }
    }

A Water Meter Record

.

Practical LearningPractical Learning: Creating a Water Meter Record

  1. To create a form, in the Solution Explorer, right-click the WaterMeters folder -> Add -> Form (Windows Forms)...
  2. Type Create
  3. Design the form as follows:

    Stellar Water Point - New Water Meter

    Control (Name) Text Other Properties
    Label Label   &Meter #:  
    MaskedTextBox Masked Text Box mtbMeterNumber   Masked: 000-000-000
    Label Label   M&ake:  
    TextBox Text Box txtMake   Modifiers: Public
    Label Label   M&odel:  
    TextBox Text Box txtModel   Modifiers: Public
    Label Label   Me&ter Size:  
    TextBox Text Box txtMeterSize   Modifiers: Public
    Button Button btnOK &OK DialogResult: OK
    Button Button btnCancel &Cancel DialogResult: Cancel
  4. Using the Properties window, change some characteristics of the form as follows:
    FormBorderStyle: FixedDialog
    Text:            Stellar Water Point - Water Meter Setup
    StartPosition:   CenterScreen
    AcceptButton:    btnOK
    CancelButton:    btnCancel
  5. In the Solution Explorer, below the WaterMeters folder, double-click Central.cs
  6. From the Toolbox, add a button to the form below the list view
  7. Change the characteristics of the button as follows:

    Stellar Water Point - Water Meters

    Control (Name) Other Properties
    ListView List View No Change No Change
    Button Button btnNewWaterMeter Text: &New Water Meter...
  8. On the Central form, double-click the New Water Meter button
  9. Implement the event as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.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)
            {
                Create create = new();
    
                if (create.ShowDialog() == DialogResult.OK)
                {
                    if (string.IsNullOrEmpty(create.mtbMeterNumber.Text))
                    {
                        MessageBox.Show("You must type a meter number. " +
                                        "Otherwise, the water meter cannot be set up.",
                                        "Stellar Water Point", MessageBoxButtons.OK);
                        return;
                    }
    
                    string strWaterMeters = string.Empty;
                    List<WaterMeter> waterMeters = new List<WaterMeter>();
                    string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                    FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                    if (fiWaterMeters.Exists == true)
                    {
                        strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                        waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
                    }
    
                    WaterMeter wm = new WaterMeter()
                    {
                        MeterNumber = create.mtbMeterNumber.Text,
                        Make = create.txtMake.Text,
                        Model = create.txtModel.Text,
                        MeterSize = create.txtMeterSize.Text
                    };
    
                    waterMeters.Add(wm);
    
                    JsonSerializerOptions options = new JsonSerializerOptions();
                    options.WriteIndented = true;
    
                    string jsWaterMeters = JsonSerializer.Serialize(waterMeters, typeof(List<WaterMeter>), options);
                    File.WriteAllText(fiWaterMeters.FullName, jsWaterMeters);
                }
    
                ShowWaterMeters();
            }
        }
    }
  10. To execute the application, on the main menu, click Debug -> Start Without Debugging

    Stellar Water Point

  11. On the Central form, click the Water Meters button:

    Stellar Water Point - Water Meters

  12. Click the New Water Meter button:

    Stellar Water Point - New Water Meter

  13. Enter the value for each of the following records and click OK (or press Enter) for each:
    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

    Stellar Water Point - Water Meters

  14. Close the forms and return to your programming environment
  15. In the Solution Explorer, double-click WaterDistribution.cs to access the primary form of the application
  16. Double-click an unoccupied area of the form to access its code
  17. Comment the call of the AccountType class as follows:
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4
    {
        public partial class WaterDistribution : Form
        {
            public WaterDistribution()
            {
                InitializeComponent();
            }
    
            private void WaterDistribution_Load(object sender, EventArgs e)
            {
                Directory.CreateDirectory(@"E:\Stellar Water Point2");
    
                // Repository.CreateAccountsTypes();
            }
    
            private void btnWaterMeters_Click(object sender, EventArgs e)
            {
                WaterMeters.Central central = new();
    
                central.Show();
            }
        }
    }

Water Meter Details

.

Practical LearningPractical Learning: Creating a Water Meter Record

  1. To create a form, in the Solution Explorer, right-click WaterMeters -> Add -> Form (Windows Forms)...
  2. For the Name of the form, type Details
  3. Press Enter
  4. Design the form as follows:

    Stellar Water Point - New Water Meter

    Control (Name) Text Enabled Modifiers Other Properties
    Label Label   &Meter #:      
    MaskedTextBox Masked Text Box mtbMeterNumber   False Public Masked: 000-000-000
    Button Button btnFindWaterMeter &Find Water Meter      
    Label Label   Make:      
    TextBox Text Box txtMake   False Public  
    Label Label   Model:      
    TextBox Text Box txtModel   False Public  
    Label Label   Meter Size:      
    TextBox Text Box txtMeterSize   False Public  
    Button Button btnClose &Close      
  5. On the form, double-click the Find Water Meter button
  6. Return to the form and double-click the Close button
  7. Change the document as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.WaterMeters
    {
        public partial class Details : Form
        {
            public Details()
            {
                InitializeComponent();
            }
    
            private void btnFindWateMeter_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(mtbMeterNumber.Text))
                {
                    MessageBox.Show("You must type a valid meter number, " +
                                    "and then click the Find Water Meter button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strWaterMeters = string.Empty;
                List<WaterMeter> waterMeters = new List<WaterMeter>();
                string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                if (fiWaterMeters.Exists == true)
                {
                    strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                    waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                    foreach (WaterMeter meter in waterMeters)
                    {
                        if (meter.MeterNumber == mtbMeterNumber.Text)
                        {
                            txtMake.Text = meter.Make;
                            txtModel.Text = meter.Model;
                            txtMeterSize.Text = meter.MeterSize;
                        }
                    }
                }
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
  8. In the Solution Explorer, below the WaterMeters folder, double-click Central.cs to open its form
  9. From the Toolbox, add a button to the form below the list view and to the right of the New Water Meter button
  10. Change the characteristics of the button as follows:

    Stellar Water Point - Water Meters

    Control (Name) Other Properties
    ListView List View No Change No Change
    Button Button No Change No Change
    Button Button btnViewWaterMeter &View Water Meter...
  11. Double-click the View Water Meter button
  12. Change the document as follows:
    using System.Data;
    using System.json;
    using Microsoft.Data.SqlClient;
    
    namespace StellarWaterPoint30.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 lvwWaterMeters_DoubleClick(object sender, EventArgs e)
            {
                . . .
            }
    
    
            private void btnViewWaterMeter_Click(object sender, EventArgs e)
            {
                Details view = new();
                
                view.ShowDialog();
                
                ShowWaterMeters();
            }
        }
    }
  13. To execute the application, on the main menu, click Debug -> Start Without Debugging

    Stellar Water Point

  14. On the Water Distribution form, click the Water Meters button
  15. On the Central form of water meters, click the View Water Meter button:

    Stellar Water Point - View Water Meter

  16. In the Meter # text, type 392-494-572
  17. Click the Find Water Button button:

    Stellar Water Point - View Water Meter

  18. Close the Details form
  19. On the Water Meters form, double-click the third record
  20. Close the forms and return to your programming environment

Updating a Water Meter Details

One of the routine operations performed on a database is to change the details of a record. To support this operation for a water meter, we will create a form that can be used to update the information of a water meter.

Practical LearningPractical Learning: Updating a Water Meter

  1. To create a form, in the Solution Explorer, right-click WaterMeters -> Add -> Form (Windows Forms)...
  2. For the Name of the file, type Editor as the name of the form
  3. Click Add
  4. Design the form as follows:

    Stellar Water Point - New Water Meter

    Control (Name) Text Enabled Other Properties
    Label Label   &Meter #:    
    MaskedTextBox Masked Text Box mtbMeterNumber   False Masked: 000-000-000
    Button Button btnFindWaterMeter &Find Water Meter      
    Label Label   Make:    
    TextBox Text Box txtMake   False  
    Label Label   Model:    
    TextBox Text Box txtModel   False  
    Label Label   Meter Size:      
    TextBox Text Box txtMeterSize   False  
    Button Button btnFindWateMeter &Find Wate Meter    
    Button Button btnClose &Close    
  5. On the form, double-click the Find Water Meter button
  6. Change the document as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.WaterMeters
    {
        public partial class Editor : Form
        {
            public Editor()
            {
                InitializeComponent();
            }
    
            private void btnFindWateMeter_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(mtbMeterNumber.Text))
                {
                    MessageBox.Show("You must type a valid meter number, " +
                                    "and then click the Find Water Meter button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strWaterMeters = string.Empty;
                List<WaterMeter> waterMeters = new List<WaterMeter>();
                string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                if (fiWaterMeters.Exists == true)
                {
                    strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                    waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                    WaterMeter meter = waterMeters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text)!;
    
                    txtMake.Text = meter.Make;
                    txtModel.Text = meter.Model;
                    txtMeterSize.Text = meter.MeterSize;
                }
            }    
        }
    }
  7. Return to the form and double-click the Update Water Meter button
  8. Return to the form and double-click the Close button
  9. Change the document as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.WaterMeters
    {
        public partial class Editor : Form
        {
            public Editor()
            {
                InitializeComponent();
            }
    
            private void btnFindWateMeter_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(mtbMeterNumber.Text))
                {
                    MessageBox.Show("You must type a valid meter number, " +
                                    "and then click the Find Water Meter button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strWaterMeters = string.Empty;
                List<WaterMeter> waterMeters = new List<WaterMeter>();
                string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                if (fiWaterMeters.Exists == true)
                {
                    strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                    waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                    WaterMeter meter = waterMeters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text)!;
    
                    txtMake.Text = meter.Make;
                    txtModel.Text = meter.Model;
                    txtMeterSize.Text = meter.MeterSize;
                }
            }
    
            private void btnUpdateWaterMeter_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(mtbMeterNumber.Text))
                {
                    MessageBox.Show("You must type a valid meter number, " +
                                    "and then click the Find Water Meter button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strWaterMeters = string.Empty;
                List<WaterMeter> waterMeters = new List<WaterMeter>();
                string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                if (fiWaterMeters.Exists == true)
                {
                    strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                    waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                    WaterMeter? meter = waterMeters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text);
    
                    if (meter is not null)
                    {
                        meter.Make = txtMake.Text;
                        meter.Model = txtModel.Text;
                        meter.MeterSize = txtMeterSize.Text;
    
                        JsonSerializerOptions options = new JsonSerializerOptions();
                        options.WriteIndented = true;
    
                        string jsWaterMeters = JsonSerializer.Serialize(waterMeters, typeof(List<WaterMeter>), options);
                        File.WriteAllText(fiWaterMeters.FullName, jsWaterMeters);
                    }
                }
    
                Close();
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
  10. In the Solution Explorer, below the WaterMeters folder, double-click Central.cs
  11. From the Toolbox, add a button to the form below the list view and on the right side of the View Water Meter button
  12. Change the characteristics of the button as follows:

    Stellar Water Point - Water Meters

    Control (Name) Other Properties
    ListView List View No Change No Change
    Button Button No Change No Change
    Button Button No Change No Change
    Button Button btnEditWaterMeter &Edit Water Meter...
    Anchor: Bottom, Right
  13. Display the Central form of the WaterMeters folder
  14. Double-click the Update Water Meter button
  15. Change the document as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.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();
            }
        }
    }
  16. To execute the application, on the main menu, click Debug -> Start Without Debugging
  17. On the Central form, click the Water Meters button
  18. Click the Edit Water Meter button:

    Stellar Water Point - Water Meter Editor

  19. In the Meter # text, type 938-705-869
  20. Click the Find button

    Stellar Water Point - Water Meter Editor

  21. Change the values as follows:
    Make: Stanford Trend
    Model: 266G
    Meter Size: 1 1/2 Inches

    Stellar Water Point - New Water Meter

  22. Click the Update button:

    Stellar Water Point - Water Meters

  23. Close the forms and return to your programming environment

Removing a Water Meter from the Database

If a record is not necessary anymore on a database, the user may want to remove it. To assist the user with this operation, we will create a form withe necessary buttons.

Practical LearningPractical Learning: Deleting a Water Meter Record

  1. To create a form, in the Solution Explorer, right-click WaterMeters -> Add -> Form(Windows Forms)...
  2. In the Name text box, replace the string with Delete as the name of the form
  3. Press Enter
  4. Design the form as follows:

    Stellar Water Point - New Water Meter

    Control (Name) Text Enabled Other Properties
    Label Label   &Meter #:    
    MaskedTextBox Masked Text Box mtbMeterNumber   False Masked: 000-000-000
    Label Label   Make:    
    TextBox Text Box txtMake   False  
    Label Label   Model:    
    TextBox Text Box txtModel   False  
    Label Label   Meter Size:    
    TextBox Text Box txtMeterSize   False  
    Button Button btnClose &Close    
  5. On the form, double-click the Find Water Meter button
  6. Change the document as tollows:
    using System.Data;
    using Microsoft.Data.SqlClient;
    
    namespace StellarWaterPoint30.WaterMeters
    {
        public partial class Delete : Form
        {
            public Delete()
            {
                InitializeComponent();
            }
    
            private void btnFindWateMeter_Click(object sender, EventArgs e)
            {
                using (SqlConnection scWaterDistribution = new SqlConnection("Data Source=(local);" +
                                                                             "Initial Catalog='StellarWaterPoint30';" +
                                                                             "TrustServerCertificate=True;" +
                                                                             "Integrated Security=True;"))
                {
                    SqlCommand cmdWaterMeters = new SqlCommand("SELECT Make,           " +
                                                               "       Model,          " +
                                                               "       MeterSize       " +
                                                               "FROM   Management.WaterMeters " +
                                                               "WHERE  MeterNumber = N'" + mtbMeterNumber.Text + "';",
                                                               scWaterDistribution);
    
                    scWaterDistribution.Open();
    
                    SqlDataAdapter sdaWaterMeters = new SqlDataAdapter(cmdWaterMeters);
    
                    DataSet dsWaterMeters = new("WaterMetersSet");
                    sdaWaterMeters.Fill(dsWaterMeters);
    
                    foreach (DataRow drWaterMeter in dsWaterMeters.Tables[0].Rows!)
                    {
                        txtMake.Text = drWaterMeter[0].ToString();
                        txtModel.Text = drWaterMeter[1].ToString();
                        txtMeterSize.Text = drWaterMeter[2].ToString();
                    }
                }
            }
        }
    }
  7. Return to the form and double-click the Delete Water Meter button
  8. Return to the form and double-click the Close button
  9. Change the document as tollows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.WaterMeters
    {
        public partial class Delete : Form
        {
            public Delete()
            {
                InitializeComponent();
            }
    
            private void btnFindWateMeter_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(mtbMeterNumber.Text))
                {
                    MessageBox.Show("You must type a valid meter number, " +
                                    "and then click the Find Water Meter button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strWaterMeters = string.Empty;
                List<WaterMeter> waterMeters = new List<WaterMeter>();
                string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                if (fiWaterMeters.Exists == true)
                {
                    strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                    waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                    WaterMeter meter = waterMeters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text)!;
    
                    txtMake.Text = meter.Make;
                    txtModel.Text = meter.Model;
                    txtMeterSize.Text = meter.MeterSize;
                }
            }
    
            private void btnDeleteWaterMeter_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(mtbMeterNumber.Text))
                {
                    MessageBox.Show("You must type a valid meter number, " +
                                    "and then click the Find Water Meter button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strWaterMeters = string.Empty;
                List<WaterMeter> waterMeters = new List<WaterMeter>();
                string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                if (fiWaterMeters.Exists == true)
                {
                    strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                    waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                    WaterMeter meter = waterMeters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text)!;
    
                    if (meter is not null)
                    {
                        if (MessageBox.Show("Are you sure you want to delete this water meter " +
                                          "(you cannot undo the action)?",
                                          "Stellar Water Point", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                        {
                            waterMeters.Remove(meter);
    
                            JsonSerializerOptions options = new JsonSerializerOptions();
                            options.WriteIndented = true;
    
                            string jsWaterMeters = JsonSerializer.Serialize(waterMeters, typeof(List<WaterMeter>), options);
                            File.WriteAllText(fiWaterMeters.FullName, jsWaterMeters);
    
                            MessageBox.Show("The water meter has been removed from our database.",
                                          "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
    
                Close();
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
  10. In the Solution Explorer, below the WaterMeters folder, double-click Central.cs
  11. From the Toolbox, add two buttons to the form below the list view and on the right side of the Edit Water Meter button
  12. Change the characteristics of the buttons as follows:

    Stellar Water Point - Water Meters

    Control (Name) Text Anchor
    ListView List View lvwWaterMeters   Top, Bottom, Left, Right
    Button Button btnNewWaterMeter &New Water Meter... Bottom, Right
    Button Button btnViewWaterMeter &View Water Meter... Bottom, Right
    Button Button btnEditWaterMeter &Edit Water Meter... Bottom, Right
    Button Button btnDeleteWateMeter &Delete Water Meter... Bottom, Right
    Button Button btnClose &Close Bottom, Right
  13. On the form, double-click the Delete Water Meter button
  14. Return to the Central form of the water meters and double-click the Close button
  15. Change the document as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.WaterMeters
    {
        public partial class Central : Form
        {
            public Central()
            {
                InitializeComponent();
            }
    
            private void ShowWaterMeters()
            {
                string strWaterMeters = string.Empty;
                List<WaterMeter> waterMeters = new List<WaterMeter>();
                string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                if (fiWaterMeters.Exists == true)
                {
                    using (TextReader trWaterMeters = new StreamReader(fiWaterMeters.FullName))
                    {
                        strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                        waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                        int counter = 1;
    
                        lvwWaterMeters.Items.Clear();
    
                        foreach (WaterMeter meter in waterMeters)
                        {
                            ListViewItem lviWaterMeter = new ListViewItem(counter++.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();
    
                if (create.ShowDialog() == DialogResult.OK)
                {
                    if (string.IsNullOrEmpty(create.mtbMeterNumber.Text))
                    {
                        MessageBox.Show("You must type a meter number. " +
                                        "Otherwise, the water meter cannot be set up.",
                                        "Stellar Water Point", MessageBoxButtons.OK);
                        return;
                    }
    
                    string strWaterMeters = string.Empty;
                    List<WaterMeter> waterMeters = new List<WaterMeter>();
                    string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                    FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                    if (fiWaterMeters.Exists == true)
                    {
                        strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                        waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
                    }
    
                    WaterMeter meter = new WaterMeter()
                    {
                        MeterNumber = create.mtbMeterNumber.Text,
                        Make = create.txtMake.Text,
                        Model = create.txtModel.Text,
                        MeterSize = create.txtMeterSize.Text
                    };
    
                    waterMeters.Add(meter);
    
                    JsonSerializerOptions options = new JsonSerializerOptions();
                    options.WriteIndented = true;
    
                    string jsWaterMeters = JsonSerializer.Serialize(waterMeters, typeof(List<WaterMeter>), options);
                    File.WriteAllText(fiWaterMeters.FullName, jsWaterMeters);
                }
    
                ShowWaterMeters();
            }
    
            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();
            }
    
            private void btnDeleteWateMeter_Click(object sender, EventArgs e)
            {
                Delete delete = new();
    
                delete.ShowDialog();
    
                ShowWaterMeters();
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
  16. To execute the application, on the main menu, click Debug -> Start Without Debugging:

    Stellar Water Point - Water Meters

  17. On the Central form, click the Water Meters button:

    Stellar Water Point - Water Meters

  18. On the Central form of the water meters, click the Delete Water Meter button:

    Stellar Water Point - Water Meter Deletion

  19. In the Meter # text, type 588-279-663
  20. Click the Find button:

    Stellar Water Point - Water Meter Deletion

  21. Click the Delete button:

    Stellar Water Point - Water Meter Deletion

  22. Read the text on the message box:

    Stellar Water Point - Water Meter Deletion

    On the message box, click Yes

    Stellar Water Point - Water Meters

  23. In the same way, delete the other two records
  24. Close the forms and return to your programming environment
  25. In the Colution Eplorer, below Models, double-click Repository.cs to open its class
  26. In the Repository class, add the following method:
    using StellarWaterPoint4.WaterMeters;
    using System.Text.Json;
    
    namespace StellarWaterPoint4.Models
    {
        public static class Repository
        {
            public static void CreateWaterMeters()
            {
                List<WaterMeter> waterMeters = new List<WaterMeter>();
    
                waterMeters!.Add(new WaterMeter() { MeterNumber = "392-494-572", Make = "Constance Technologies", Model = "TG-4822", MeterSize = "5/8 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "938-725-869", Make = "Stanford Trend", Model = "266G", MeterSize = "1 1/2 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "588-279-663", Make = "Estellano", Model = "NCF-226", MeterSize = "4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "186-962-805", Make = "Lansome", Model = "2800", MeterSize = "1 1/2 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "379-386-979", Make = "Planetra", Model = "P-2020", MeterSize = "4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "580-742-825", Make = "Kensa Sons", Model = "KS2000A", MeterSize = "1 3/4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "849-351-444", Make = "Raynes Energica", Model = "a1088", MeterSize = "2 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "208-428-308", Make = "Constance Technologies", Model = "808D", MeterSize = "3/4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "738-588-249", Make = "Warrington", Model = "W4242", MeterSize = "5/8 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "496-813-794", Make = "Estellano", Model = "NCF-226", MeterSize = "3/4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "862-715-006", Make = "Warrington", Model = "W-4040", MeterSize = "1/2 Inch" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "649-358-184", Make = "Raynes Energica", Model = "b1700", MeterSize = "1 1/2 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "928-317-924", Make = "Gongola", Model = "GN1000", MeterSize = "2 Inch" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "595-753-147", Make = "Grass Grill", Model = "CRC-1000", MeterSize = "1 Inch" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "799-528-461", Make = "Kensa Sons", Model = "K-584-L", MeterSize = "3/4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "386-468-057", Make = "Estellano", Model = "NCF-226", MeterSize = "3/4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "938-275-294", Make = "Constance Technologies", Model = "TT-8822", MeterSize = "4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "288-427-585", Make = "Planetra", Model = "P-2020", MeterSize = "1/2 Inch" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "394-835-297", Make = "Raynes Energica", Model = "i2022", MeterSize = "3/4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "847-252-246", Make = "Master Stream", Model = "2000-MS", MeterSize = "1 1/2 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "349-725-848", Make = "Planetra", Model = "P-8000", MeterSize = "4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "713-942-058", Make = "Master Stream", Model = "3366-MS", MeterSize = "3/4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "747-581-379", Make = "Warrington", Model = "W4242", MeterSize = "5/8 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "582-755-263", Make = "Kensa Sons", Model = "KS2000A", MeterSize = "1 Inch" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "827-260-758", Make = "Raynes Energica", Model = "a1088", MeterSize = "1-1/4 Inch" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "837-806-836", Make = "Lansome", Model = "7400", MeterSize = "5/8 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "207-964-835", Make = "Constance Technologies", Model = "TG-6220", MeterSize = "5/8 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "296-837-495", Make = "Raynes Energica", Model = "QG505", MeterSize = "4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "468-359-486", Make = "Grass Grill", Model = "KLP-8822", MeterSize = "1-1/4 Inch" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "931-486-003", Make = "Planetra", Model = "P-2020", MeterSize = "1/2 Inch" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "483-770-648", Make = "Warren", Model = "WWW", MeterSize = "0.1 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "592-824-957", Make = "Kensa Sons", Model = "D-497-H", MeterSize = "3/4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "293-835-704", Make = "Gongola", Model = "GOL1000", MeterSize = "1/2 Inch" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "739-777-749", Make = "Warrington", Model = "W2200W", MeterSize = "3/4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "374-886-284", Make = "Raynes Energica", Model = "i2022", MeterSize = "3/4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "186-959-757", Make = "Kensa Sons", Model = "M-686-G", MeterSize = "1 1/2 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "594-827-359", Make = "Planetra", Model = "P-8000", MeterSize = "1 Inch" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "394-739-242", Make = "Master Stream", Model = "9393-TT", MeterSize = "5/8 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "529-283-752", Make = "Constance Technologies", Model = "404T", MeterSize = "3/4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "295-770-695", Make = "Warrington", Model = "W-2286", MeterSize = "1-1/4 Inch" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "739-749-737", Make = "Kensa Sons", Model = "KS2000A", MeterSize = "1 Inch" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "947-528-317", Make = "Gondola", Model = "GDL-5000", MeterSize = "1 Inch" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "630-207-055", Make = "Lansome", Model = "2800", MeterSize = "3/4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "827-508-248", Make = "Standard Trend", Model = "428T", MeterSize = "3/4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "293-924-869", Make = "Grass Grill", Model = "CRC-2020", MeterSize = "1/2 Inch" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "928-247-580", Make = "Gondola", Model = "GOL2000", MeterSize = "0.34 Inch" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "682-537-380", Make = "Planetra", Model = "P-2020", MeterSize = "1-1/4 Inch" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "470-628-850", Make = "Estellano", Model = "WRT-482", MeterSize = "3/4 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "649-373-505", Make = "Constance Technologies", Model = "BD-7000", MeterSize = "5/8 Inches" });
                waterMeters.Add(new WaterMeter()  { MeterNumber = "306-842-497", Make = "Lansome", Model = "9000", MeterSize = "3/4 Inches" });
    
                string strWaterMeters         = string.Empty;
                string fileWaterMeters        = @"E:\Stellar Water Point2\WaterMeters.json";
    
                JsonSerializerOptions options = new JsonSerializerOptions();
                options.WriteIndented         = true;
    
                FileInfo fiWaterMeters        = new FileInfo(fileWaterMeters);
                string jsWaterMeters          = JsonSerializer.Serialize(waterMeters, typeof(List<WaterMeter>), options);
                File.WriteAllText(fiWaterMeters.FullName, jsWaterMeters);
            }
        }
    }
  27. In the Solution Explorer, double-click WaterDistribution.cs
  28. Double-click an unoccupied area of the form
  29. Class the function as follows:
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4
    {
        public partial class WaterDistribution : Form
        {
            public WaterDistribution()
            {
                InitializeComponent();
            }
    
            private void WaterDistribution_Load(object sender, EventArgs e)
            {
                Directory.CreateDirectory(@"E:\Stellar Water Point2");
    
                // Repository.CreateAccountsTypes();
                Repository.CreateWaterMeters();
            }
    
            private void btnWaterMeters_Click(object sender, EventArgs e)
            {
                WaterMeters.Central central = new();
    
                central.Show();
            }
        }
    }
  30. To execute the application, on the main menu, click Debug -> Start Without Debugging
  31. Close the forms and return to your programming environment

Customers

Introduction

.

Practical LearningPractical Learning: Introducing Customers

  1. To create a class, in the Solution Explorer, right-click Models -> Add -> Class...
  2. On the main menu, click Project -> Add Class...
  3. Replace the name with Customer
  4. Click Add
  5. Change the code as follows:
    namespace StellarWaterPoint4.Models
    {
        public class Customer
        {
            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; }
        }
    }
  6. To create a folder, in the Solution Explorer, right-click the StellarWaterPoint4 project -> Add -> New Folder
  7. Type Customers as the name of the folder

Displaying Customers

.

Practical LearningPractical Learning: Displaying Customers

  1. To create a form, in the Solution Explorer, right-click Customers -> Add -> Form (Windows Forms)...
  2. For the Name of the form, type Central
  3. Click Add
  4. In the Toolbox, click the ListView button and click the form
  5. On the form, right-click the list view and click Edit Columns...
  6. Create the columns as follows:
    (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
  7. Click OK
  8. Position and resize the list view on the form as follows:

    Stellar Water Point - Customers

    Control (Name) Other Properties
    ListView List View lvwCustomers FullRowSelect: True
    GridLines: True
    View: Details
  9. Double-click an unoccupied area of the form to generate its Load event
  10. Change the document as follows:
    using StellarWaterPoint4.Models;
    using System.Text.Json;
    
    namespace StellarWaterPoint4.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();
            }
        }
    }
  11. In the Solution Explorer, double-click WaterDistribution.cs to display the main form of the application
  12. From the Toolbox, add a button to the form
  13. From the Properties window, change the characteristics of the button as follows:

    Stellar Water Point

    Control (Name) Text Font
    Button Button btnCustomers C&ustomers... Times New Roman, 24pt, style=Bold
  14. Double-click the C&ustomers
  15. Impliment the event as follows:
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4
    {
        public partial class WaterDistribution : Form
        {
            public WaterDistribution()
            {
                InitializeComponent();
            }
    
            private void WaterDistribution_Load(object sender, EventArgs e)
            {
                Directory.CreateDirectory(@"E:\Stellar Water Point2");
    
                // Repository.CreateAccountsTypes();
                // Repository.CreateWaterMeters();
            }
    
            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();
            }
        }
    }

A New Customer Account

.

Practical LearningPractical Learning: Creating a Customer Account

  1. To create a form, in the Solution Explorer, right-click the WaterMeters folder -> Add -> Form (Windows Forms)...
  2. For the Name of the file, type Create as the name of the form
  3. Click Add
  4. Design the form as follows:

    Stellar Water Point - New Customer Account

    Control (Name) Text Other Properties
    Label Label   &Account #:  
    MaskedTextBox Masked Text Box mtbAccountNumber   Masked: 0000-000-0000
    Label Label   &Account Name:  
    TextBox Text Box txtAccountName    
    Label Label   &Meter #:  
    MaskedTextBox Masked Text Box mtbMeterNumber   Masked: 000-000-000
    Button Button btnFindWaterMeter &Find Water Meter  
    Label Label   Meter &Details:  
    TextBox Text Box txtMeterDetails   Enabled: False
    Label Label   &Account Type:  
    ComboBox Combo Box cbxAccountsTypes  
    Label Label   &Address:  
    TextBox Text Box txtAddress    
    Label Label   C&ity:  
    TextBox Text Box txtCity    
    Label Label   C&ounty:  
    TextBox Text Box txtCounty    
    Label Label   &State:  
    TextBox Text Box txtState    
    Label Label   &ZIP-Code:  
    MaskedTextBox Masked Text Box mtbZIPCode   Masked: Zip-Code
    Button Button btnSaveCustomerAccount S&ave Customer Account DialogResult: OK
    Button Button btnClose &Close DialogResult: Cancel
  5. Using the Properties window, change some characteristics of the form as follows:
    FormBorderStyle: FixedDialog
    Text:            Stellar Water Point - Create Customer Account
    StartPosition:   CenterScreen
    AcceptButton:    btnCreateCustomerAccount
    CancelButton:    btnCancel
  6. Double-click an unoccupied area of the form to generate its Load event
  7. Change the document as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.Customers
    {
        public partial class Create : Form
        {
            public Create()
            {
                InitializeComponent();
            }
    
            private void Create_Load(object sender, EventArgs e)
            {
                string strAccountsTypes    = string.Empty;
                List<AccountType> types    = new List<AccountType>();
                string fileAccountsTypes   = @"E:\Stellar Water Point2\AccountsTypes.json";
    
                FileInfo fiAccountsTypes   = new FileInfo(fileAccountsTypes);
    
                if (fiAccountsTypes.Exists == true)
                {
                    strAccountsTypes       = File.ReadAllText(fiAccountsTypes.FullName);
                    types = JsonSerializer.Deserialize<List<AccountType>>(strAccountsTypes)!;
    
                    foreach (AccountType type in types)
                    {
                        cbxAccountsTypes.Items.Add(type.TypeCode + " - " + type.TypeDecription);
                    }
                }
            }
        }
    }
  8. Return to the form and double-click the Find Water Meter button
  9. Implement the event as tollows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.Customers
    {
        public partial class Create : Form
        {
            public Create()
            {
                InitializeComponent();
            }
    
            private void Create_Load(object sender, EventArgs e)
            {
                string strAccountsTypes = string.Empty;
                List<AccountType> types = new List<AccountType>();
                string fileAccountsTypes = @"E:\Stellar Water Point2\AccountsTypes.json";
    
                FileInfo fiAccountsTypes = new FileInfo(fileAccountsTypes);
    
                if (fiAccountsTypes.Exists == true)
                {
                    strAccountsTypes = File.ReadAllText(fiAccountsTypes.FullName);
                    types = JsonSerializer.Deserialize<List<AccountType>>(strAccountsTypes)!;
    
                    foreach (AccountType type in types)
                    {
                        cbxAccountsTypes.Items.Add(type.TypeCode + " - " + type.TypeDecription);
                    }
                }
            }
    
            private void btnFindWateMeter_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(mtbMeterNumber.Text))
                {
                    MessageBox.Show("You must type a valid meter number, " +
                                    "and then click the Find Water Meter button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strWaterMeters = string.Empty;
                List<WaterMeter> waterMeters = new List<WaterMeter>();
                string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                if (fiWaterMeters.Exists == true)
                {
                    strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                    waterMeters    = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                    foreach (WaterMeter meter in waterMeters)
                    {
                        if (meter.MeterNumber == mtbMeterNumber.Text)
                        {
                            txtMeterDetails.Text = meter.Make + " " + 
                                                   meter.Model + 
                                                   " (Meter Size: " + meter.MeterSize + ")";
                        }
                    }
                }
            }
        }
    }
  10. In the Solution Explorer, below the Customers folder, double-click Central.cs
  11. From the Toolbox, add a button to the form below the list view
  12. Change the characteristics of the button as follows:

    Stellar Water Point - Water Meters

    Control (Name) Other Properties
    ListView List View lvwCustomers FullRowSelect: True
    GridLines: True
    View: Details
    Button Button btnNewCustomerAccount &New Customer Account...
  13. On the Central form, double-click the New Customer Account button
  14. Implement the event as follows:
    using StellarWaterPoint4.Models;
    using System.Text.Json;
    
    namespace StellarWaterPoint4.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();
    
                if (create.ShowDialog() == DialogResult.OK)
                {
                    if (string.IsNullOrEmpty(create.mtbAccountNumber.Text))
                    {
                        MessageBox.Show("You must provide an account number for a new customer. " +
                                        "Otherwise, the account cannot be created.",
                                        "Stellar Water Point", MessageBoxButtons.OK);
                        return;
                    }
    
                    if (string.IsNullOrEmpty(create.mtbMeterNumber.Text))
                    {
                        MessageBox.Show("You must type a valid meter number to associate " +
                                        "a water meter to a customer's account. After providing " +
                                        "a meter number, click the Find Water Meter button.",
                                        "Stellar Water Point", MessageBoxButtons.OK);
                        return;
                    }
    
                    string strCustomers    = string.Empty;
                    List<Customer> clients = new List<Customer>();
                    string fileCustomers   = @"E:\Stellar Water Point2\Customers.json";
    
                    FileInfo fiCustomers   = new FileInfo(fileCustomers);
    
                    if (fiCustomers.Exists == true)
                    {
                        strCustomers       = File.ReadAllText(fiCustomers.FullName);
                        clients            = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
                    }
    
                    Customer client        = new Customer()
                    {
                        AccountNumber      = create.mtbAccountNumber.Text,
                        AccountName        = create.txtAccountName.Text,
                        MeterNumber        = create.mtbMeterNumber.Text,
                        AccountType        = create.cbxAccountsTypes.Text,
                        Address            = create.txtAddress.Text,
                        City               = create.txtCity.Text,
                        County             = create.txtCounty.Text,
                        State              = create.txtState.Text,
                        ZIPCode            = create.mtbZIPCode.Text
                    };
    
                    clients.Add(client);
    
                    JsonSerializerOptions options = new JsonSerializerOptions();
                    options.WriteIndented = true;
    
                    string jsCustomers = JsonSerializer.Serialize(clients, typeof(List<Customer>), options);
                    File.WriteAllText(fiCustomers.FullName, jsCustomers);
                }
    
                ShowCustomers();
            }
        }
    }
  15. To execute the application, on the main menu, click Debug -> Start Without Debugging:

    Stellar Water Point - New Customer Account

  16. On the Water Distribution form, click the Customers button:

    Stellar Water Point - Customers Accounts

  17. On the Central form of the customers, click the Create Customer Account button:

    Stellar Water Point - Customers Accounts

  18. In the account # text box, type 9279-570-8394
  19. In the meter # text box, type 799-528-461
  20. Click the Find water meter button
  21. Enter the other values as follows:
    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

    Stellar Water Point - New Customer Account

  22. Click Save Customer Account
  23. In the same way, create the following two records:
    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

    Stellar Water Point - Customers

  24. Close the forms and return to your programming environment

Customer Account Details

.

Practical LearningPractical Learning: Showing Customer Account

  1. To create a form, in the Solution Explorer, right-click Customers -> Add -> Form (Windows Forms)...
  2. For the Name of the form, type Details
  3. Press Enter
  4. Design the form as follows:

    Stellar Water Point - New Customer Account

    Control (Name) Text Other Properties
    Label Label   &Account #:  
    MaskedTextBox Masked Text Box mtbAccountNumber   Masked: 0000-000-0000
    Label Label   &Account Name:  
    TextBox Text Box txtAccountName   Enabled: False
    Label Label   Meter &Details:  
    TextBox Text Box txtMeterDetails   Enabled: False
    Label Label   &Account Type:  
    TextBox Combo Box txtAccountsTypes   Enabled: False
    Label Label   &Address:  
    TextBox Text Box txtAddress   Enabled: False
    Label Label   C&ity:  
    TextBox Text Box txtCity   Enabled: False
    Label Label   C&ounty:  
    TextBox Text Box txtCounty   Enabled: False
    Label Label   &State:  
    TextBox Text Box txtState   Enabled: False
    Label Label   &ZIP-Code:  
    TextBox Masked Text Box txtZIPCode   Enabled: False
    Button Button btnClose &Close  
  5. On the form, double-click the Find Customer Account button
  6. Return to the form and double-click the Close button
  7. Change the document as follows:
    using StellarWaterPoint4.Models;
    using System.Text.Json;
    
    namespace StellarWaterPoint4.Customers
    {
        public partial class Details : Form
        {
            public Details()
            {
                InitializeComponent();
            }
    
            private void btnFindCustomerAccount_Click(object sender, EventArgs e)
            {
                
                if (string.IsNullOrEmpty(mtbAccountNumber.Text))
                {
                    MessageBox.Show("You must type a valid account number of a customer, " +
                                    "and then click the Find Customer Account button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strCustomers = string.Empty;
                List<Customer> clients = new List<Customer>();
                string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
    
                FileInfo fiCustomers = new FileInfo(fileCustomers);
    
                string? strMeterNumber = string.Empty;
    
                if (fiCustomers.Exists == true)
                {
                    using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
                    {
                        strCustomers = File.ReadAllText(fiCustomers.FullName);
                        clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
    
                        IEnumerable<Customer> customers = from   consumer
                                                          in     clients
                                                          where  consumer.AccountNumber == mtbAccountNumber.Text
                                                          select consumer;
    
                        foreach (Customer client in customers)
                        {
                            txtAccountName.Text = client.AccountName;
                            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;
                        }
                    }
                }
    
                if (strMeterNumber!.Length > 0)
                {
                    string strWaterMeters = string.Empty;
                    List<WaterMeter> waterMeters = new List<WaterMeter>();
                    string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                    FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                    if (fiWaterMeters.Exists == true)
                    {
                        strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                        waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                        IEnumerable<WaterMeter> meters = from   measure
                                                         in     waterMeters
                                                         where  measure.MeterNumber == strMeterNumber
                                                         select measure;
    
                        foreach (WaterMeter meter in waterMeters)
                        {
                            txtMeterDetails.Text = meter.Make + " " +
                                                   meter.Model +
                                                   " (Meter Size: " + meter.MeterSize + ")";
                        }
                    }
                }
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
  8. In the Solution Explorer, below the Customers folder, double-click Central.cs to open its form
  9. From the Toolbox, add a button to the form below the list view and to the right of the New Customer Account button
  10. Change the characteristics of the button as follows:

    Stellar Water Point - Water Meters

    Control (Name) Other Properties
    ListView List View lvwWaterMeters No Change
    Button Button No Change No Change
    Button Button btnCustomerAccountDetails Customer Account &Details...
    Anchor: Bottom, Right
  11. Double-click the Customer Account &Details button
  12. Change the document as follows:
    using System.Data;
    using Microsoft.Data.SqlClient;
    
    namespace StellarWaterPoint31.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.ShowDialog();
    
                ShowCustomers();
            }
        }
    }
  13. To execute the application, on the main menu, click Debug -> Start Without Debugging

    Stellar Water Point

  14. On the Water Distribution form, click the Customers button
  15. On the Central form of the customers form, click the Customer Account &Details button:

    Stellar Water Point - View Water Meter

  16. In the Account # text, type 4086-938-4783
  17. Click the Find Customer Account button:

    Stellar Water Point - View Water Meter

  18. Close the forms and return to your programming environment

Editing a Customer Account

.

Practical LearningPractical Learning: Editing a Customer Account

  1. To create a form, in the Solution Explorer, right-click WaterMeters -> Add -> Form (Windows Forms)...
  2. For the Name of the file, type Editor as the name of the form
  3. Click Add
  4. Design the form as follows:

    Stellar Water Point - Customer Account Editor

    Control (Name) Text Other Properties
    Label Label   &Account #:  
    MaskedTextBox Masked Text Box mtbAccountNumber   Masked: 0000-000-0000
    Button Button btnFindCustomerAccount &Find Customer Account  
    Label Label   &Account Name:  
    TextBox Text Box txtAccountName    
    Label Label   &Meter #:  
    MaskedTextBox Masked Text Box mtbMeterNumber   Masked: 000-000-000
    Button Button btnFindWaterMeter Find &Water Meter  
    Label Label   Meter &Details:  
    TextBox Text Box txtMeterDetails   Enabled: False
    Label Label   &Account Type:  
    ComboBox Combo Box cbxAccountsTypes  
    Label Label   &Address:  
    TextBox Text Box txtAddress    
    Label Label   C&ity:  
    TextBox Text Box txtCity    
    Label Label   C&ounty:  
    TextBox Text Box txtCounty    
    Label Label   &State:  
    TextBox Text Box txtState    
    Label Label   &ZIP-Code:  
    MaskedTextBox Masked Text Box mtbZIPCode   Masked: Zip-Code
    Button Button btnUpdateCustomerAccount &Update Customer Account DialogResult: OK
    Button Button btnClose &Close DialogResult: Cancel
  5. Double-click an unoccupied area of the form to generate its Load event
  6. Change the document as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.Customers
    {
        public partial class Editor : Form
        {
            public Editor()
            {
                InitializeComponent();
            }
    
            private void Editor_Load(object sender, EventArgs e)
            {
                string strAccountsTypes = string.Empty;
                List<AccountType> types = new List<AccountType>();
                string fileAccountsTypes = @"E:\Stellar Water Point2\AccountsTypes.json";
    
                FileInfo fiAccountsTypes = new FileInfo(fileAccountsTypes);
    
                if (fiAccountsTypes.Exists == true)
                {
                    strAccountsTypes = File.ReadAllText(fiAccountsTypes.FullName);
                    types = JsonSerializer.Deserialize<List<AccountType>>(strAccountsTypes)!;
    
                    foreach (AccountType type in types)
                    {
                        cbxAccountsTypes.Items.Add(type.TypeCode + " - " + type.TypeDecription);
                    }
                }
            }
        }
    }
  7. Return to the form, double-click the Find Customer Account button
  8. Change the document as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.Customers
    {
        public partial class Editor : Form
        {
            public Editor()
            {
                InitializeComponent();
            }
    
            private void Editor_Load(object sender, EventArgs e)
            {
                . . .
            }
    
            private void btnFindCustomerAccount_Click(object sender, EventArgs e)
            {
                string strCustomers = string.Empty;
                List<Customer> clients = new List<Customer>();
                string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
    
                FileInfo fiCustomers = new FileInfo(fileCustomers);
    
                if (fiCustomers.Exists == true)
                {
                    using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
                    {
                        strCustomers = File.ReadAllText(fiCustomers.FullName);
                        clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
    
                        IEnumerable<Customer> customers = from consumer
                                                          in clients
                                                          where consumer.AccountNumber == mtbAccountNumber.Text
                                                          select consumer;
    
                        foreach (Customer client in customers)
                        {
                            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;
                        }
                    }
                }
    
                if( !string.IsNullOrEmpty(mtbMeterNumber.Text) )
                {
                    string strWaterMeters = string.Empty;
                    List<WaterMeter> waterMeters = new List<WaterMeter>();
                    string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                    FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
                    
                    if (fiWaterMeters.Exists == true)
                    {
                        strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                        waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
                        
                        foreach (WaterMeter meter in waterMeters)
                        {
                            if (meter.MeterNumber == mtbMeterNumber.Text)
                            {
                                txtMeterDetails.Text = meter.Make + " " +
                                                       meter.Model +
                                                       " (Meter Size: " + meter.MeterSize + ")";
                            }
                        }
                    }
                }
            }
        }
    }
  9. Return to the form and double-click the Find Water Meter button
  10. Change the document as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.Customers
    {
        public partial class Editor : Form
        {
            public Editor()
            {
                InitializeComponent();
            }
    
            private void Editor_Load(object sender, EventArgs e)
            {
                . . .
            }
    
            private void btnFindCustomerAccount_Click(object sender, EventArgs e)
            {
                . . .
            }
    
            private void btnFindWateMeter_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(mtbMeterNumber.Text))
                {
                    MessageBox.Show("You must type a valid meter number, " +
                                    "and then click the Find Water Meter button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strWaterMeters = string.Empty;
                List<WaterMeter> waterMeters = new List<WaterMeter>();
                string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                if (fiWaterMeters.Exists == true)
                {
                    strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                    waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                    foreach (WaterMeter meter in waterMeters)
                    {
                        if (meter.MeterNumber == mtbMeterNumber.Text)
                        {
                            txtMeterDetails.Text = meter.Make  + " " +
                                                   meter.Model +
                                                   " (Meter Size: " + meter.MeterSize + ")";
                        }
                    }
                }
            }
        }
    }
  11. Return to the form and double-click the Update Water Meter button
  12. Return to the form and double-click the Close button
  13. Change the document as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    using StellarWaterPoint4.WaterMeters;
    
    namespace StellarWaterPoint4.Customers
    {
        public partial class Editor : Form
        {
            public Editor()
            {
                InitializeComponent();
            }
    
            private void Editor_Load(object sender, EventArgs e)
            {
                string strAccountsTypes = string.Empty;
                List<AccountType> types = new List<AccountType>();
                string fileAccountsTypes = @"E:\Stellar Water Point2\AccountsTypes.json";
    
                FileInfo fiAccountsTypes = new FileInfo(fileAccountsTypes);
    
                if (fiAccountsTypes.Exists == true)
                {
                    strAccountsTypes = File.ReadAllText(fiAccountsTypes.FullName);
                    types = JsonSerializer.Deserialize<List<AccountType>>(strAccountsTypes)!;
    
                    foreach (AccountType type in types)
                    {
                        cbxAccountsTypes.Items.Add(type.TypeCode + " - " + type.TypeDecription);
                    }
                }
            }
    
            private void btnFindCustomerAccount_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(mtbAccountNumber.Text))
                {
                    MessageBox.Show("Please an account number of an existing customer. " +
                                    "Then click the Find Customer Account button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
    
                string strCustomers = string.Empty;
                List<Customer> clients = new List<Customer>();
                string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
    
                FileInfo fiCustomers = new FileInfo(fileCustomers);
    
                if (fiCustomers.Exists == true)
                {
                    using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
                    {
                        strCustomers = File.ReadAllText(fiCustomers.FullName);
                        clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
    
                        IEnumerable<Customer> customers = from consumer
                                                          in clients
                                                          where consumer.AccountNumber == mtbAccountNumber.Text
                                                          select consumer;
    
                        foreach (Customer client in customers)
                        {
                            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;
                        }
                    }
                }
    
                if (!string.IsNullOrEmpty(mtbMeterNumber.Text))
                {
                    string strWaterMeters = string.Empty;
                    List<WaterMeter> waterMeters = new List<WaterMeter>();
                    string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                    FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                    if (fiWaterMeters.Exists == true)
                    {
                        strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                        waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                        foreach (WaterMeter meter in waterMeters)
                        {
                            if (meter.MeterNumber == mtbMeterNumber.Text)
                            {
                                txtMeterDetails.Text = meter.Make + " " +
                                                       meter.Model +
                                                       " (Meter Size: " + meter.MeterSize + ")";
                            }
                        }
                    }
                }
            }
    
            private void btnFindWateMeter_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(mtbMeterNumber.Text))
                {
                    MessageBox.Show("You must type a valid meter number, " +
                                    "and then click the Find Water Meter button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strWaterMeters = string.Empty;
                List<WaterMeter> waterMeters = new List<WaterMeter>();
                string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                if (fiWaterMeters.Exists == true)
                {
                    strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                    waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                    foreach (WaterMeter meter in waterMeters)
                    {
                        if (meter.MeterNumber == mtbMeterNumber.Text)
                        {
                            txtMeterDetails.Text = meter.Make + " " +
                                                   meter.Model +
                                                   " (Meter Size: " + meter.MeterSize + ")";
                        }
                    }
                }
            }
    
            private void btnUpdateCustomerAccount_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(mtbAccountNumber.Text))
                {
                    MessageBox.Show("Please enter an account number for a customer. " +
                                    "You can then click the Find Customer Account button " +
                                    "to indicate the customer whose account you want o update.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                if (string.IsNullOrEmpty(mtbMeterNumber.Text))
                {
                    MessageBox.Show("You must type a valid meter number, " +
                                    "and then click the Find Water Meter button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strCustomers = string.Empty;
                List<Customer> clients = new List<Customer>();
                string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
    
                FileInfo fiCustomers = new FileInfo(fileCustomers);
    
                if (fiCustomers.Exists == true)
                {
                    strCustomers = File.ReadAllText(fiCustomers.FullName);
                    clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
    
                    Customer? customer = clients.Find(cust => cust.AccountNumber == mtbAccountNumber.Text);
                                
                    if(customer is not null)
                    {
                        customer.AccountName = txtAccountName.Text;
                        customer.MeterNumber = mtbMeterNumber.Text;
                        customer.AccountType = cbxAccountsTypes.Text;
                        customer.Address = txtAddress.Text;
                        customer.City = txtCity.Text;
                        customer.County = txtCounty.Text;
                        customer.State = txtState.Text;
                        customer.ZIPCode = mtbZIPCode.Text;
    
                        JsonSerializerOptions options = new JsonSerializerOptions();
                        options.WriteIndented = true;
    
                        string jsCustomers = JsonSerializer.Serialize(clients, typeof(List<Customer>), options);
                        File.WriteAllText(fiCustomers.FullName, jsCustomers);
                    };
                }
    
                Close();
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
  14. In the Solution Explorer, below the Customers folder, double-click Central.cs
  15. From the Toolbox, add a button to the form below the list view and on the right side of the Customer Account Editor button
  16. Change the characteristics of the button as follows:

    Stellar Water Point - Water Meters

    Control (Name) Other Properties
    ListView List View lvwCustomers No Change
    Button Button btnNewCustomerAccount No Change
    Button Button btnCustomerAccountDetails No Change
    Button Button btnUpdateCustomerAccount &Update Customer Account...
    Anchor: Bottom, Right
  17. Display the Central form of the Customers folder
  18. Double-click the Update Customer Account button
  19. Change the document as follows:
    using System.Data;
    using Microsoft.Data.SqlClient;
    
    namespace StellarWaterPoint31.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.ShowDialog();
    
                ShowCustomers();
            }
    
            private void btnEditCustomerAccount_Click(object sender, EventArgs e)
            {
                Editor editor = new();
    
                editor.ShowDialog();
    
                ShowCustomers();
            }
        }
    }
  20. To execute the application, on the main menu, click Debug -> Start Without Debugging

    Stellar Water Point

  21. On the main form of the application, click the Customers button:

    Stellar Water Point - Customers

  22. Click the Edit Customer Account button:

    Stellar Water Point - Water Meter Editor

  23. In the Account # text, type 4086-938-4783
  24. Click the Find Customer Account button

    Stellar Water Point - Water Meter Editor

  25. Change the values as follows:
    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

    Stellar Water Point - New Water Meter

  26. Click the Update Customer Account button:

    Stellar Water Point - Water Meters

  27. Close the forms and return to your programming environment

Deleting a Customer Account from the Database

.

Practical LearningPractical Learning: Deleting a Customer Account

  1. To create a form, in the Solution Explorer, right-click Customers -> Add -> Form(Windows Forms)...
  2. In the Name text box, replace the string with Delete as the name of the form
  3. Press Enter
  4. Design the form as follows:

    Stellar Water Point - Customer Account Deletion

    Control (Name) Text Other Properties
    Label Label   &Account #:  
    MaskedTextBox Masked Text Box mtbAccountNumber   Masked: 0000-000-0000
    Button Button btnFindCustomerAccount &Find Customer Account  
    Label Label   &Account Name:  
    TextBox Text Box txtAccountName   Enabled: False
    Label Label   Meter &Details:  
    TextBox Text Box txtMeterDetails   Enabled: False
    Label Label   &Account Type:  
    TextBox Combo Box txtAccountsTypes   Enabled: False
    Label Label   &Address:  
    TextBox Text Box txtAddress   Enabled: False
    Label Label   C&ity:  
    TextBox Text Box txtCity   Enabled: False
    Label Label   C&ounty:  
    TextBox Text Box txtCounty   Enabled: False
    Label Label   &State:  
    TextBox Text Box txtState   Enabled: False
    Label Label   &ZIP-Code:  
    TextBox Masked Text Box txtZIPCode   Enabled: False
    Button Button btnDeleteCustomerAccount &Delete Customer Account  
    Button Button btnClose &Close  
  5. On the form, double-click the Find Customer Account button
  6. Change the document as tollows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.Customers
    {
        public partial class Delete : Form
        {
            public Delete()
            {
                InitializeComponent();
            }
    
            private void btnFindCustomerAccount_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(mtbAccountNumber.Text))
                {
                    MessageBox.Show("You must type a valid account number of a customer, " +
                                    "and then click the Find Customer Account button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strCustomers    = string.Empty;
                List<Customer> clients = new List<Customer>();
                string fileCustomers   = @"E:\Stellar Water Point2\Customers.json";
    
                FileInfo fiCustomers   = new FileInfo(fileCustomers);
    
                string? strMeterNumber = string.Empty;
    
                if (fiCustomers.Exists == true)
                {
                    using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
                    {
                        strCustomers  = File.ReadAllText(fiCustomers.FullName);
                        clients       = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
    
                        IEnumerable<Customer> customers = from consumer
                                                          in clients
                                                          where consumer.AccountNumber == mtbAccountNumber.Text
                                                          select consumer;
    
                        foreach (Customer client in customers)
                        {
                            txtAccountName.Text = client.AccountName;
                            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;
                        }
                    }
                }
    
                if (strMeterNumber!.Length > 0)
                {
                    string strWaterMeters = string.Empty;
                    List<WaterMeter> waterMeters = new List<WaterMeter>();
                    string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                    FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                    if (fiWaterMeters.Exists == true)
                    {
                        strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                        waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                        IEnumerable<WaterMeter> meters = from measure
                                                         in waterMeters
                                                         where measure.MeterNumber == strMeterNumber
                                                         select measure;
    
                        foreach (WaterMeter meter in waterMeters)
                        {
                            txtMeterDetails.Text = meter.Make + " " +
                                                   meter.Model +
                                                   " (Meter Size: " + meter.MeterSize + ")";
                        }
                    }
                }
            }
        }
    }
  7. Return to the form and double-click the Delete Customer Account button
  8. Return to the form and double-click the Close button
  9. Implement the event as tollows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.Customers
    {
        public partial class Delete : Form
        {
            public Delete()
            {
                InitializeComponent();
            }
    
            private void btnFindCustomerAccount_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(mtbAccountNumber.Text))
                {
                    MessageBox.Show("You must type a valid account number of a customer, " +
                                    "and then click the Find Customer Account button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strCustomers    = string.Empty;
                List<Customer> clients = new List<Customer>();
                string fileCustomers   = @"E:\Stellar Water Point2\Customers.json";
    
                FileInfo fiCustomers   = new FileInfo(fileCustomers);
    
                string? strMeterNumber = string.Empty;
    
                if (fiCustomers.Exists == true)
                {
                    using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
                    {
                        strCustomers   = File.ReadAllText(fiCustomers.FullName);
                        clients        = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
    
                        IEnumerable<Customer> customers = from consumer
                                                          in clients
                                                          where consumer.AccountNumber == mtbAccountNumber.Text
                                                          select consumer;
    
                        foreach (Customer client in customers)
                        {
                            txtAccountName.Text = client.AccountName;
                            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;
                        }
                    }
                }
    
                if (strMeterNumber!.Length > 0)
                {
                    string strWaterMeters = string.Empty;
                    List<WaterMeter> waterMeters = new List<WaterMeter>();
                    string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                    FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                    if (fiWaterMeters.Exists == true)
                    {
                        strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                        waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                        IEnumerable<WaterMeter> meters = from measure
                                                         in waterMeters
                                                         where measure.MeterNumber == strMeterNumber
                                                         select measure;
    
                        foreach (WaterMeter meter in waterMeters)
                        {
                            txtMeterDetails.Text = meter.Make + " " +
                                                   meter.Model +
                                                   " (Meter Size: " + meter.MeterSize + ")";
                        }
                    }
                }
            }
    
            private void btnDeleteCustomerAccount_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(mtbAccountNumber.Text))
                {
                    MessageBox.Show("You must type a valid account number of a customer, " +
                                    "and then click the Find Customer Account button. " +
                                    "Only then can you delete an customer account.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strCustomers = string.Empty;
                List<Customer> clients = new List<Customer>();
                string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
    
                FileInfo fiCustomers = new FileInfo(fileCustomers);
    
                if (fiCustomers.Exists == true)
                {
                    strCustomers = File.ReadAllText(fiCustomers.FullName);
                    clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
    
                    Customer client = clients.Find(acnt => acnt.AccountNumber == mtbAccountNumber.Text)!;
    
                    if (client is not null)
                    {
                        if (MessageBox.Show("Are you sure you want to delete this customer's account " +
                                            "(you cannot undo the action)?",
                                            "Stellar Water Point",
                                            MessageBoxButtons.YesNo,
                                            MessageBoxIcon.Information) == DialogResult.Yes)
                        {
                            clients.Remove(client);
    
                            JsonSerializerOptions options = new JsonSerializerOptions();
                            options.WriteIndented = true;
    
                            string jsCustomers = JsonSerializer.Serialize(clients, typeof(List<Customer>), options);
                            File.WriteAllText(fiCustomers.FullName, jsCustomers);
    
                            MessageBox.Show("The customer's account has been deleted from our system.",
                                            "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
    
                Close();
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
  10. In the Solution Explorer, below the Customers folder, double-click Central.cs
  11. From the Toolbox, add two buttons to the form below the list view and on the right side of the Update Customer Account button
  12. Change the characteristics of the buttons as follows:

    Stellar Water Point - Water Meters

    Control (Name) Other Properties
    ListView List View lvwCustomers FullRowSelect: True
    GridLines: True
    View: Details
    Anchor: Top, Bottom, Left, Right
    Button Button btnCreateCustomerAccount Create Customer &Account....
    Anchor: Bottom, Right
    Button Button btnCustomerAccountDetails Customer Account &Details...
    Anchor: Bottom, Right
    Button Button btnEditCustomerAccount &Edit Customer Account...
    Anchor: Bottom, Right
    Button Button btnDeleteCustomerAccount &Delete Customer Account...
    Anchor: Bottom, Right
    Button Button btnClose &Close
    Anchor: Bottom, Right
  13. On the form, double-click the Delete Customer Account button
  14. Return to the Central form of the water meters and double-click the Close button
  15. Change the document as follows:
    using StellarWaterPoint4.Models;
    using System.Text.Json;
    
    namespace StellarWaterPoint4.Customers
    {
        public partial class Central : Form
        {
            public Central()
            {
                InitializeComponent();
            }
    
            private void ShowCustomers()
            {
                string strCustomers = string.Empty;
                List<Customer> clients = new List<Customer>();
                string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
    
                FileInfo fiCustomers = new FileInfo(fileCustomers);
    
                if (fiCustomers.Exists == true)
                {
                    using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
                    {
                        strCustomers = File.ReadAllText(fiCustomers.FullName);
                        clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
    
                        int counter = 1;
    
                        lvwCustomers.Items.Clear();
    
                        foreach (Customer client in clients)
                        {
                            ListViewItem lviCustomer = new ListViewItem(counter++.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();
    
                if (create.ShowDialog() == DialogResult.OK)
                {
                    if (string.IsNullOrEmpty(create.mtbAccountNumber.Text))
                    {
                        MessageBox.Show("You must provide an account number for a new customer. " +
                                        "Otherwise, the account cannot be created.",
                                        "Stellar Water Point", MessageBoxButtons.OK);
                        return;
                    }
    
                    if (string.IsNullOrEmpty(create.mtbMeterNumber.Text))
                    {
                        MessageBox.Show("You must type a valid meter number to associate " +
                                        "a water meter to a customer's account. After providing " +
                                        "a meter number, click the Find Water Meter button.",
                                        "Stellar Water Point", MessageBoxButtons.OK);
                        return;
                    }
    
                    string strCustomers = string.Empty;
                    List<Customer> clients = new List<Customer>();
                    string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
    
                    FileInfo fiCustomers = new FileInfo(fileCustomers);
    
                    if (fiCustomers.Exists == true)
                    {
                        strCustomers = File.ReadAllText(fiCustomers.FullName);
                        clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
                    }
    
                    Customer client = new Customer()
                    {
                        AccountNumber = create.mtbAccountNumber.Text,
                        AccountName = create.txtAccountName.Text,
                        MeterNumber = create.mtbMeterNumber.Text,
                        AccountType = create.cbxAccountsTypes.Text,
                        Address = create.txtAddress.Text,
                        City = create.txtCity.Text,
                        County = create.txtCounty.Text,
                        State = create.txtState.Text,
                        ZIPCode = create.mtbZIPCode.Text
                    };
    
                    clients.Add(client);
    
                    JsonSerializerOptions options = new JsonSerializerOptions();
                    options.WriteIndented = true;
    
                    string jsCustomers = JsonSerializer.Serialize(clients, typeof(List<Customer>), options);
                    File.WriteAllText(fiCustomers.FullName, jsCustomers);
                }
    
                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();
            }
        }
    }
  16. To execute the application, on the main menu, click Debug -> Start Without Debugging:

    Stellar Water Point

  17. On the Central form, click the Customers button:

    Stellar Water Point - Customers

  18. On the Central form of the customers, click the Delete Customer Account button:

    Stellar Water Point - Water Meter Deletion

  19. In the Account # text box, type 7080-583-5947
  20. Click the Find Customer Account button:

    Stellar Water Point - Customer Account Deletion

  21. Click the Delete Customer Account button
  22. Read the text on the message box.
    On the message box, click Yes

    Stellar Water Point - Customers

  23. In the same way, delete the other two records
  24. Close the forms and return to your programming environment
  25. In the Colution Eplorer, below Models, double-click Repository.cs to open its class
  26. In the Repository class, add the following method:
    using System.Text.Json;
    
    namespace StellarWaterPoint4.Models
    {
        public static class Repository
        {
            public static void CreateAccountsTypes()
            {
                . . .
            }
    
            public static void CreateWaterMeters()
            {
                . . .
            }
    
            public static void CreateCustomers()
            {
                List<Customer> customers = new List<Customer>();
    
                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" });
                customers.Add(new Customer()  { AccountNumber = "4086-938-4783", AccountName = "Bernotte's Doughnuts",              MeterNumber = "580-742-825", AccountType = "BUS - General Business", Address = "10103 Hexagon Drv", City = "Winterstown", County = "York", State = "PA", ZIPCode = "17402-8818" });
                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" });
                customers.Add(new Customer()  { AccountNumber = "",              AccountName = "First Methodist Congregation",      MeterNumber = "",            AccountType = "",    Address = "7702 Charles Road", City = "", County = "", State = "", ZIPCode = "" });
                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" });
                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" });
                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", County = "", State = "VA", ZIPCode = "22222-6060" });
                customers.Add(new Customer()  { AccountNumber = "7928-131-4850", AccountName = "",                                  MeterNumber = "",            AccountType = "SGO - Social/Government/Non-Profit Organization", Address = "", City = "Washington", County = "", State = "", ZIPCode = "" });
                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" });
                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" });
                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", County = "", State = "VA", ZIPCode = "24012-4824" });
                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" });
                customers.Add(new Customer()  { AccountNumber = "",              AccountName = "Astral Sequence",                   MeterNumber = "",            AccountType = "BUS - General Business", Address = "12715 Eastern Gateway", City = "Catonsville", County = "Baltimore County", State = "", ZIPCode = "" });
                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" });
                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" });
                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" });
                customers.Add(new Customer()  { AccountNumber = "5252-757-9595", AccountName = "",                                  MeterNumber = "379-386-979", AccountType = "",    Address = "4992 Preston Street", City = "", County = "", State = "OH", ZIPCode = "" });
                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" });
                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" });
                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" });
                customers.Add(new Customer()  { AccountNumber = "1827-395-0203", AccountName = "Sathyavanthara Khooni",             MeterNumber = "470-628-850", AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "10331 Chryswell Road", City = "Washington", County = "", State = "DC", ZIPCode = "20008-2426" });
                customers.Add(new Customer()  { AccountNumber = "",              AccountName = "Eastern Cage",                      MeterNumber = "",            AccountType = "",    Address = "", City = "Hammonton", County = "", State = "NJ", ZIPCode = "" });
                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" });
                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" });
                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" });
                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" });
                customers.Add(new Customer()  { AccountNumber = "",              AccountName = "",                                  MeterNumber = "928-317-924", AccountType = "BUS - General Business", Address = "", City = "", County = "", State = "", ZIPCode = "" });
                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" });
                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" });
                customers.Add(new Customer()  { AccountNumber = "9337-947-3664", AccountName = "",                                  MeterNumber = "649-358-184", AccountType = "SGO - Social/Government/Non-Profit Organization", Address = "", City = "Bellefontaine", County = "", State = "OH", ZIPCode = "" });
                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" });
                customers.Add(new Customer()  { AccountNumber = "",              AccountName = "Jeffrey Maney",                     MeterNumber = "",            AccountType = "RES - Residential Household", Address = "", City = "", County = "", State = "", ZIPCode = "" });
                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" });
                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" });
                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", County = "", State = "DC", ZIPCode = "20004-2626" });
                customers.Add(new Customer()  { AccountNumber = "",              AccountName = "Amidou Gomah",                      MeterNumber = "",            AccountType = "RES - Residential Household", Address = "14118 Yellow Burrough Blvd", City = "Philadelphia", County = "", State = "PA", ZIPCode = "" });
                customers.Add(new Customer()  { AccountNumber = "3792-853-6885", AccountName = "Department of Public Affairs",      MeterNumber = "595-753-147", AccountType = "",    Address = "", City = "Upper Marlboro", County = "Prince George County", State = "", ZIPCode = "" });
                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", County = "", State = "MD", ZIPCode = "21613-2288" });
                customers.Add(new Customer()  { AccountNumber = "",              AccountName = "Single Connection",                 MeterNumber = "288-427-585", AccountType = "BUS - General Business", Address = "", City = "Mansfield", County = "", State = "OH", ZIPCode = "44903-3030" });
                customers.Add(new Customer()  { AccountNumber = "2499-636-4444", AccountName = "Bryanna Spencer",                   MeterNumber = "",            AccountType = "RES - Residential Household", Address = "6282 Sheppherd Str", City = "", County = "Anne Arundel", State = "", ZIPCode = "" });
                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", County = "", State = "", ZIPCode = "" });
                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", County = "", State = "MD", ZIPCode = "" });
                customers.Add(new Customer()  { AccountNumber = "",              AccountName = "Miguel Altieri",                    MeterNumber = "",            AccountType = "RES - Residential Household", Address = "10941 Patriot Blvd", City = "Crenshaw", County = "Jefferson", State = "PA", ZIPCode = "15824-6628" });
                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", City = "", County = "", State = "WV", ZIPCode = "" });
                customers.Add(new Customer()  { AccountNumber = "6381-748-2222", AccountName = "Up Eyes",                           MeterNumber = "",            AccountType = "BUS - General Business", Address = "4149 Deerfield Str", City = "", County = "", State = "", ZIPCode = "" });
                customers.Add(new Customer()  { AccountNumber = "",              AccountName = "Annette Wald",                      MeterNumber = "",            AccountType = "RES - Residential Household", Address = "11441  Eastern Friendshi Rd", City = "", County = "", State = "", ZIPCode = "" });
                customers.Add(new Customer()  { AccountNumber = "8384-708-2941", AccountName = "Department of Environment Affairs", MeterNumber = "",            AccountType = "SGO - Social/Government/Non-Profit Organization", Address = "", City = "", County = "", State = "OH", ZIPCode = "" });
                customers.Add(new Customer()  { AccountNumber = "3728-138-2947", AccountName = "Marie Rath",                        MeterNumber = "",            AccountType = "",    Address = "8802 Atlantic Ave", City = "", County = "", State = "PA", ZIPCode = "" });
                customers.Add(new Customer()  { AccountNumber = "1793-857-9413", AccountName = "Body Care",                         MeterNumber = "",            AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "", City = "Ocean City", County = "", State = "NJ", ZIPCode = "" });
                customers.Add(new Customer()  { AccountNumber = "6028-695-2068", AccountName = "Ronald Glassman",                   MeterNumber = "468-359-486", AccountType = "BUS - General Business", Address = "", City = "", County = "", State = "", ZIPCode = "" });
                customers.Add(new Customer()  { AccountNumber = "",              AccountName = "",                                  MeterNumber = "",            AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "8812 Lawrence Ave", City = "", County = "", State = "NW", ZIPCode = "" });
                customers.Add(new Customer()  { AccountNumber = "9616-283-7249", AccountName = "",                                  MeterNumber = "",            AccountType = "SGO - Social/Government/Non-Profit Organization", Address = "13006 Blueberry Ave", City = "", County = "", State = "MD", ZIPCode = "" });
                customers.Add(new Customer()  { AccountNumber = "2829-516-8353", AccountName = "Richard Eghert",                    MeterNumber = "",            AccountType = "RES - Residential Household", Address = "662 Placido Road", City = "", County = "", State = "", ZIPCode = "" });
    
                string strCustomers = string.Empty;
                string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
    
                JsonSerializerOptions options = new JsonSerializerOptions();
                options.WriteIndented = true;
    
                FileInfo fiCustomers = new FileInfo(fileCustomers);
                string jsCustomers = JsonSerializer.Serialize(customers, typeof(List<Customer>), options);
                File.WriteAllText(fiCustomers.FullName, jsCustomers);
            }
        }
    }
  27. In the Solution Explorer, double-click WaterDistribution.cs
  28. Double-click an unoccupied area of the form
  29. Class the function as follows:
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4
    {
        public partial class WaterDistribution : Form
        {
            public WaterDistribution()
            {
                InitializeComponent();
            }
    
            private void WaterDistribution_Load(object sender, EventArgs e)
            {
                Directory.CreateDirectory(@"E:\Stellar Water Point2");
    
                // Repository.CreateAccountsTypes();
                // Repository.CreateWaterMeters();
                Repository.CreateCustomers();
            }
    
            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();
            }
        }
    }

Water Bills

Introduction

.

Practical LearningPractical Learning: Introducing Water Bills

  1. To create a folder, in the Solution Explorer, right-click the StellarWaterPoint3 project -> Add -> New Folder
  2. Type WaterBills as the name of the folder
  3. To create a class, in the Solution Explorer, right-click Models -> Add -> Class...
  4. Type WaterBill as the Name of the file
  5. Click Add
  6. Define the class as follows:
    namespace StellarWaterPoint4.Models
    {
        public class WaterBill
        {
    		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 DateTime LateAmountDue         { get; set; }
        }
    }
  7. To create another class, in the Solution Explorer, right-click Models -> Add -> Class...
  8. Type WaterBillManager as the Name of the file
  9. Click Add
  10. Define the class as follows:
    namespace StellarWaterPoint31.Models
    {
        internal class WaterBillManager
        {
            internal (double a, double b, double c) CalculateTiers(string acnt, double total)
            {
                (double tier1, double tier2, double tier3) results = (0.00, 0.00, 0.00);
    
                if (acnt == "RES - Residential Household")
                {
                    results.tier1 = total * 41.50 / 10000.00;
                    results.tier2 = total * 32.50 / 10000.00;
                    results.tier3 = total * 26.00 / 10000.00;
                }
                else if (acnt == "SGO - Social/Government/Non-Profit Organization")
                {
                    results.tier1 = total * 46.00 / 10000.00;
                    results.tier2 = total * 50.00 / 10000.00;
                    results.tier3 = total * 4.00  / 10000.00;
                }
                else if (acnt == "BUS - General Business")
                {
                    results.tier1 = total * 45.00 / 10000.00;
                    results.tier2 = total * 30.00 / 10000.00;
                    results.tier3 = total * 25.00 / 10000.00;
                }
                else if (acnt == "UUO - Unidentified or Unclassified Type of Organization")
                {
                    results.tier1 = total * 25.00 / 10000.00;
                    results.tier2 = total * 35.00 / 10000.00;
                    results.tier3 = total * 40.00 / 10000.00;
                }
                else if (acnt == "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc")
                {
                    results.tier1 = total * 50.00 / 10000.00;
                    results.tier2 = total * 40.00 / 10000.00;
                    results.tier3 = total * 10.00 / 10000.00;
                }
                else
                {
                    results.tier1 = total * (48.00 / 10000.00);
                    results.tier2 = total * (32.00 / 10000.00);
                    results.tier3 = total * (20.00 / 10000.00);
                }
    
                return results;
            }
    
            internal double CalculateSewerCharges(string acnt, double total)
            {
                double result;
    
                if (acnt == "RES - Residential Household")
                {
                    result = total * 6.826941 / 100.00;
                }
                else if (acnt == "SGO - Social/Government/Non-Profit Organization")
                {
                    result = total * 4.162522 / 100.00;
                }
                else if (acnt == "BUS - General Business")
                {
                    result = total * 8.315136 / 100.00;
                }
                else if (acnt == "UUO - Unidentified or Unclassified Type of Organization")
                {
                    result = total * 10.626147 / 100.00;
                }
                else if (acnt == "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc")
                {
                    result = total * 12.025135 / 100.00;
                }
                else // if (acnt == "OTH - Other")
                {
                    result = total * 9.202615 / 100.00;
                }
    
                return result;
            }
    
            internal double CalculateEnvironmentCharges(string acnt, double total)
            {
                double result;
    
                switch (acnt)
                {
                    case "RES - Residential Household":
                        result = total * 0.022724;
                        break;
                    case "SGO - Social/Government/Non-Profit Organization":
                        result = total * 0.118242;
                        break;
                    case "BUS - General Business":
                        result = total * 0.161369;
                        break;
                    case "UUO - Unidentified or Unclassified Type of Organization":
                        result = total * 0.082477;
                        break;
                    case "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc":
                        result = total * 0.413574;
                        break;
                    default:
                        result = total * 0.221842;
                        break;
                }
    
                return result;
            }
    
            internal double CalculateServiceCharges(string acnt, double total)
            {
                switch (acnt)
                {
                    case "RES - Residential Household":
                        return total * 0.145748;
                    case "SGO - Social/Government/Non-Profit Organization":
                        return total * 0.102246;
                    case "BUS - General Business":
                        return total * 0.242627;
                    case "UUO - Unidentified or Unclassified Type of Organization":
                        return total * 0.186692;
                    case "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc":
                        return total * 0.412628;
                    default:
                        return total * 0.210248;
                }
            }
    
            internal double CalculateLocalTaxes(string acnt, double total) => acnt switch
            {
                "RES - Residential Household" => total * 0.031574,
                "SGO - Social/Government/Non-Profit Organization" => total * 0.035026,
                "BUS - General Business" => total * 0.122517,
                "UUO - Unidentified or Unclassified Type of Organization" => total * 0.105737,
                "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc" => total * 0.153248,
                _     => total * 0.125148
            };
    
            internal double CalculateStateTaxes(string acnt, double total) => acnt switch
            {
                "RES - Residential Household" => total * 0.017242,
                "SGO - Social/Government/Non-Profit Organization" => total * 0.008779,
                "BUS - General Business" => total * 0.042448,
                "UUO - Unidentified or Unclassified Type of Organization" => total * 0.067958,
                "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc" => total * 0.081622,
                _     => total * 0.013746
            };
    
            internal DateTime SetPaymentDueDate(string acnt, DateTime date)
            {
                TimeSpan tsPaymentDueDate = new TimeSpan(1, 0, 0, 0);
    
                if (acnt == "RES - Residential Household")
                {
                    tsPaymentDueDate = new TimeSpan(15, 0, 0, 0);
                }
                else if (acnt == "SGO - Social/Government/Non-Profit Organization")
                {
                    tsPaymentDueDate = new TimeSpan(20, 0, 0, 0);
                }
                else if (acnt == "BUS - General Business")
                {
                    tsPaymentDueDate = new TimeSpan(30, 0, 0, 0);
                }
                else if (acnt == "UUO - Unidentified or Unclassified Type of Organization")
                {
                    tsPaymentDueDate = new TimeSpan(25, 0, 0, 0);
                }
                else if (acnt == "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc")
                {
                    tsPaymentDueDate = new TimeSpan(40, 0, 0, 0);
                }
                else
                {
                    tsPaymentDueDate = new TimeSpan(35, 0, 0, 0);
                }
    
                return date + tsPaymentDueDate;
            }
    
            internal DateTime SetLatePaymentDueDate(string acnt, DateTime date)
            {
                switch (acnt)
                {
                    case "RES - Residential Household":
                        return date + new TimeSpan(30, 0, 0, 0);
                    case "SGO - Social/Government/Non-Profit Organization":
                        return date + new TimeSpan(40, 0, 0, 0);
                    case "BUS - General Business":
                        return date + new TimeSpan(50, 0, 0, 0);
                    case "UUO - Unidentified or Unclassified Type of Organization":
                        return date + new TimeSpan(60, 0, 0, 0);
                    case "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc":
                        return date + new TimeSpan(65, 0, 0, 0);
                    default:
                        return date + new TimeSpan(45, 0, 0, 0);
                }
            }
    
            internal double CalculateLateAmountDue(string acnt, double amt) => acnt switch
            {
                "RES - Residential Household" => amt + 8.95,
                "SGO - Social/Government/Non-Profit Organization" => amt + (amt /  4.575),
                "BUS - General Business" => amt + (amt / 12.315),
                "UUO - Unidentified or Unclassified Type of Organization" => amt + (amt /  7.425),
                "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc" => amt + (amt / 15.225),
                _     => amt + (amt / 6.735)
            };
        }
    }

Showing Water Bills

.

Practical LearningPractical Learning: Showing Water Bills

  1. To create a form, in the Solution Explorer, right-click WaterBills -> Add -> Form (Windows Forms)...
  2. For the Name of the form, type Central
  3. Click Add
  4. In the Toolbox, click the ListView button and click the form
  5. On the form, right-click the list view and click Edit Columns...
  6. Create the columns as follows:
    (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
  7. Click OK
  8. Position and resize the list view on the form as follows:

    Stellar Water Point - Water Bills

    Control (Name) Other Properties
    ListView List View lvwWaterBills FullRowSelect: True
    GridLines: True
    View: Details
    Anchor: Top, Bottom, Left, Right
  9. Double-click an unoccupied area of the form to generate its Load event
  10. Change the document as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.WaterBills
    {
        public partial class Central : Form
        {
            public Central()
            {
                InitializeComponent();
            }
    
            private void ShowWaterBills()
            {
                // We will need the list of customers to display an account summary in the list view
                // Declare a strCustomers variable that will hold the records from the JSON file
                string strCustomers     = string.Empty;
                // Prepare a list to hold the records of customers
                List<Customer> clients  = new List<Customer>();
                // Specify the file that holds the records of customers
                string fileCustomers    = @"E:\Stellar Water Point2\Customers.json";
    
                // Create a FileInfo object for the records of customers
                FileInfo fiCustomers    = new FileInfo(fileCustomers);
    
                // Check if a file that holds the records of customers was created already
                if (fiCustomers.Exists  == true)
                {
                    // If that file exists, create a TextReader object to get those records
                    using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
                    {
                        // Read the records and store them in the strCustomers variable
                        strCustomers = File.ReadAllText(fiCustomers.FullName);
                        // Use JSON deserialization to get the records of customers
                        clients         = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
                    }
                }
    
                // Declare a strWaterBills variable that will hold the records from the JSON file
                string strWaterBills    = string.Empty;
                // Prepare a bills list to hold the records of water bills
                List<WaterBill> bills   = new List<WaterBill>();
                // Specify the file that holds the records of water bills
                string fileWaterBills   = @"E:\Stellar Water Point2\WaterBills.json";
    
                // Create a FileInfo object for the records of water bills
                FileInfo fiWaterBills   = new FileInfo(fileWaterBills);
    
                // Find out whether a file that holds the records of water bills exists already
                if (fiWaterBills.Exists == true)
                {
                    // If that file exists, create a TextReader object to read the file
                    using (TextReader trWaterBills = new StreamReader(fiWaterBills.FullName))
                    {
                        /* Read the contents of the file that holds the water bills.
                         * Store the read text in the strWaterBills variable that was declared. */
                        strWaterBills = File.ReadAllText(fiWaterBills.FullName);
                        // Get the water bills and store them in the bills list variable
                        bills = JsonSerializer.Deserialize<List<WaterBill>>(strWaterBills)!;
    
                        // This variable will be used to keep count of the water bills records
                        int counter = 1;
    
                        /* We are about to display the water bills records in the list view.
                         * Before proceeding, first remove any record in the list view. */
                        lvwWaterBills.Items.Clear();
    
                        // Visit each record of the water bills
                        foreach (WaterBill client in bills)
                        {
                            /* Prepare a ListViewItem object for each record.
                             * Display the record counter in the first column of this object. */
                            ListViewItem lviWaterBill = new ListViewItem(counter++.ToString());
    
                            // Display the water bill number of the current record
                            lviWaterBill.SubItems.Add(client.BillNumber.ToString());
    
                            /* Refer to the list of records of the customers (that list was prepared earlier).
                             * Use LINQ to get the customer whose account number is 
                             * the same as the account number of the current record. */
                            IEnumerable<Customer> customer = clients.Where(cust => cust.AccountNumber == client.AccountNumber);
    
                            // Now that we have located the customer record, display some details about it
                            foreach (Customer cust in customer)
                            {
                                lviWaterBill.SubItems.Add(client.AccountNumber + " - " + 
                                                          cust.AccountName + 
                                                          ", Type: " + cust.AccountType![..3] + 
                                                          ", (Mtr #: " + cust.MeterNumber + ")");
                            }
    
                            /* Continue displaying some other parts of the water bill.
                             * Before there is not enough space for the whole water bill, we display only some values. */
                            lviWaterBill.SubItems.Add(client.MeterReadingStartDate.ToShortDateString());
                            lviWaterBill.SubItems.Add(client.MeterReadingEndDate.ToShortDateString());
                            lviWaterBill.SubItems.Add(client.BillingDays.ToString());
                            lviWaterBill.SubItems.Add(client.CounterReadingStart.ToString());
                            lviWaterBill.SubItems.Add(client.CounterReadingEnd.ToString());
                            lviWaterBill.SubItems.Add(client.TotalHCF.ToString());
                            lviWaterBill.SubItems.Add(client.TotalGallons.ToString());
                            lviWaterBill.SubItems.Add(client.PaymentDueDate.ToShortDateString());
                            lviWaterBill.SubItems.Add(client.AmountDue.ToString());
    
                            lvwWaterBills.Items.Add(lviWaterBill);
                        }
                    }
                }
            }
    
            private void Central_Load(object sender, EventArgs e)
            {
                ShowWaterBills();
            }
        }
    }
  11. In the Solution Explorer, double-click WaterDistribution.cs to display the main form of the application
  12. From the Toolbox, add two buttons to the form
  13. From the Properties window, change the characteristics of the button as follows:

    Stellar Water Point

    Control (Name) Text Font
    Button Button btnWaterBills C&Water Bills... Times New Roman, 24pt, style=Bold
    Button Button btnClose &Close Times New Roman, 24pt, style=Bold
  14. Double-click the &Water Bills button
  15. Return to the form and double-click the Close button
  16. Impliment the events as follows:
    namespace StellarWaterPoint31
    {
        public partial class WaterDistribution : Form
        {
            public WaterDistribution()
            {
                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

.

Practical LearningPractical Learning: Processing a Water Bill

  1. To create a form, in the Solution Explorer, right-click WaterBills -> Add -> Form (Windows Forms)...
  2. Set the name to Create
  3. Click Add
  4. Design the form as follows:

    Stellar Water Point - New Water Bill

    Control Text Name Other Properties
    Label Label &Water Bill #:    
    TextBox Text Box   txtBillNumber  
    GroupBox Group Box Customer Information    
    Label Label &Account #:    
    MaskedTextBox Masked Text Box   mtbAccountNumber Mask: 0000-000-0000
    Button Button Find Customer &Account btnFindCustomerAccount  
    Label Label Account Name:    
    TextBox Text Box   txtAccountName  
    Label Label Account Type:    
    TextBox Text Box   txtAccountType  
    Label Label Address:    
    TextBox Text Box   txtAddress  
    TextBox Text Box   txtCity  
    TextBox Text Box   txtCounty  
    TextBox Text Box   txtState  
    TextBox Text Box   txtZIPCode  
    Label Label _________________________________________________    
    Label Label Meter Details:    
    TextBox Text Box   txtMeterDetails  
    GroupBox Group Box Meter Reading    
    Label Label Meter &Reading Start Date:    
    Date Time Picker Text Box   dtpMeterReadingStartDate  
    Label Label Meter Reading &End Date:    
    Date Time Picker Text Box   dtpMeterReadingEndDate  
    Label Label Coun&ter Reading Start:    
    TextBox Text Box   txtCounterReadingStart  
    Label Label Counter Readi&ng End:    
    TextBox Text Box   txtCounterReadingEnd  
    Button Button &Evaluate Water Bill btnEvaluateWaterBill Times New Roman, 24pt, style=Bold
    GroupBox Group Box Meter Result    
    Label Label Billing Days:    
    TextBox Text Box   txtBillingDays  
    Label Label Total HCF:    
    TextBox Text Box   txtTotalHCF  
    Label Label Total Gallons:    
    TextBox Text Box   txtTotalGallons  
    Label Label First Tier Consumption:    
    TextBox Text Box   txtFirstTierConsumption  
    Label Label Second Tier:    
    TextBox Text Box   txtSecondTierConsumption  
    Label Label Last Tier:    
    TextBox Text Box   txtLastTierConsumption  
    GroupBox Group Box Consumption Charges    
    Label Label Water Charges:    
    TextBox Text Box   txtWaterCharges  
    Label Label Sewer Charges:    
    TextBox Text Box   txtSewerCharges  
    Label Label Environment Charges:    
    TextBox Text Box   txtEnvironmentCharges  
    Label Label Service Charges:    
    TextBox Text Box   txtServiceCharges  
    Label Label Total Charges:    
    TextBox Text Box   txtTotalCharges  
    GroupBox Group Box Taxes    
    Label Label Local Taxes:    
    TextBox Text Box   txtLocalTaxes  
    Label Label State Taxes:    
    TextBox Text Box   txtStateTaxes  
    GroupBox Group Box Water Bill Payment    
    Label Label Payment Due Date:    
    Date Time Picker Text Box   dtpPaymentDueDate  
    Label Label Amount Due:    
    TextBox Text Box   txtAmountDue  
    Label Label Late Payment Due Date:    
    Date Time Picker Text Box   dtpLatePaymentDueDate  
    Label Label &Late Amount Due:    
    TextBox Text Box   txtLateAmountDue  
    Button Button Save Water Bill btnSaveWaterBill  
    Button Button &Close btnClose  

    Form Properties

    Form Property Value
    FormBorderStyle FixedDialog
    Text Stellar Water Point - Water Bill Processing
    StartPosition CenterScreen
    MaximizeBox False
    MinimizeBox False
  5. On the form, double-click the Find Customer Account button
  6. Change the document as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.WaterBills
    {
        public partial class Create : Form
        {
            public Create()
            {
                InitializeComponent();
            }
    
            private void btnFindCustomerAccount_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(mtbAccountNumber.Text))
                {
                    MessageBox.Show("You must type a valid account number of a customer, " +
                                    "and then click the Find Customer Account button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strCustomers    = string.Empty;
                List<Customer> clients = new List<Customer>();
                string fileCustomers   = @"E:\Stellar Water Point2\Customers.json";
    
                FileInfo fiCustomers   = new FileInfo(fileCustomers);
    
                string? strMeterNumber = string.Empty;
    
                if (fiCustomers.Exists == true)
                {
                    using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
                    {
                        strCustomers = File.ReadAllText(fiCustomers.FullName);
                        clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
    
                        IEnumerable<Customer> customers = clients.Where(cust => cust.AccountNumber == mtbAccountNumber.Text);
    
                        foreach (Customer client in customers)
                        {
                            txtAccountName.Text = client.AccountName;
                            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;
                        }
                    }
                }
    
                if (strMeterNumber!.Length > 0)
                {
                    string strWaterMeters        = string.Empty;
                    List<WaterMeter> waterMeters = new List<WaterMeter>();
                    string fileWaterMeters       = @"E:\Stellar Water Point2\WaterMeters.json";
    
                    FileInfo fiWaterMeters       = new FileInfo(fileWaterMeters);
    
                    if (fiWaterMeters.Exists     == true)
                    {
                        strWaterMeters           = File.ReadAllText(fiWaterMeters.FullName);
                        waterMeters              = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                        IEnumerable<WaterMeter> meters = waterMeters.Where(wm => wm.MeterNumber == strMeterNumber);
    
                        foreach (WaterMeter meter in meters)
                        {
                            txtMeterDetails.Text = meter.Make + " " +
                                                   meter.Model +
                                                   " (Meter Size: " + meter.MeterSize + ")";
                        }
                    }
                }
            }
        }
    }
  7. Return to the form and double-click the Meter Reading End Date date time picker to generate its Value Changed event
  8. Implement the event as follows:
    using System.Data;
    using Microsoft.Data.SqlClient;
    
    namespace StellarWaterPoint31.WaterBills
    {
        public partial class Create : Form
        {
            public Create()
            {
                InitializeComponent();
            }
    
            private void btnFindCustomerAccount_Click(object sender, EventArgs e)
            {
                . . .
            }
    
            private void dtpMeterReadingEndDate_ValueChanged(object sender, EventArgs e)
            {
                TimeSpan tsDays = dtpMeterReadingEndDate.Value - dtpMeterReadingStartDate.Value;
    
                txtBillingDays.Text = (tsDays.Days + 1).ToString();
            }
        }
    }
  9. Return to the form and double-click the Evaluate Water Bill button
  10. Implement the event as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.WaterBills
    {
        public partial class Create : Form
        {
            public Create()
            {
                InitializeComponent();
            }
    
            private void btnFindCustomerAccount_Click(object sender, EventArgs e)
            {
                . . .
            }
    
            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)
                {
                    MessageBox.Show("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)
                {
                    MessageBox.Show("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");
            }
        }
    }
  11. Return to the form and double-click the Save Water Bill button
  12. Return to the form and double-click the Close button to generate its Click event
  13. Implement the events as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.WaterBills
    {
        public partial class Create : Form
        {
            public Create()
            {
                InitializeComponent();
            }
    
            private void btnFindCustomerAccount_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(mtbAccountNumber.Text))
                {
                    MessageBox.Show("You must type a valid account number of a customer, " +
                                    "and then click the Find Customer Account button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strCustomers = string.Empty;
                List<Customer> clients = new List<Customer>();
                string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
    
                FileInfo fiCustomers = new FileInfo(fileCustomers);
    
                string? strMeterNumber = string.Empty;
    
                if (fiCustomers.Exists == true)
                {
                    using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
                    {
                        strCustomers = File.ReadAllText(fiCustomers.FullName);
                        clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
    
                        IEnumerable<Customer> customers = clients.Where(cust => cust.AccountNumber == mtbAccountNumber.Text);
    
                        foreach (Customer client in customers)
                        {
                            txtAccountName.Text = client.AccountName;
                            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;
                        }
                    }
                }
    
                if (strMeterNumber!.Length > 0)
                {
                    string strWaterMeters = string.Empty;
                    List<WaterMeter> waterMeters = new List<WaterMeter>();
                    string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                    FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                    if (fiWaterMeters.Exists == true)
                    {
                        strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                        waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                        IEnumerable<WaterMeter> meters = waterMeters.Where(wm => wm.MeterNumber == strMeterNumber);
    
                        foreach (WaterMeter meter in meters)
                        {
                            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)
                {
                    MessageBox.Show("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)
                {
                    MessageBox.Show("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))
                {
                    MessageBox.Show("You must type a (unique) bill number for the " +
                                    "water bill you are processing.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                if (string.IsNullOrEmpty(mtbAccountNumber.Text))
                {
                    MessageBox.Show("You must specify the account number of the customer " +
                                    "whose water bill you are preparing.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strWaterBills      = string.Empty;
                List<WaterBill> bills     = new List<WaterBill>();
                string fileWaterBills     = @"E:\Stellar Water Point2\WaterBills.json";
    
                FileInfo fiWaterBills     = new FileInfo(fileWaterBills);
    
                if (fiWaterBills.Exists   == true)
                {
                    strWaterBills         = File.ReadAllText(fiWaterBills.FullName);
                    bills                 = JsonSerializer.Deserialize<List<WaterBill>>(strWaterBills)!;
                }
    
                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)
                };
    
                bills.Add(bill);
    
                JsonSerializerOptions options = new JsonSerializerOptions();
                options.WriteIndented = true;
    
                string jsWaterBills = JsonSerializer.Serialize(bills, typeof(List<WaterBill>), options);
                File.WriteAllText(fiWaterBills.FullName, jsWaterBills);
    
                Close();
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
  14. In the Solution Explorer, below the WaterBills folder, double-click Central.cs
  15. From the Toolbox, add a button to the form below the list view
  16. Change the characteristics of the button as follows:

    Stellar Water Point - Water Meters

    Control (Name) Other Properties
    ListView List View lvwWaterBills No Change
    Button Button btnProcessWaterBill &Process Water Bill...
  17. On the Central form, double-click the Process Water Bill button
  18. Implement the event as follows:
    using System.Data;
    using Microsoft.Data.SqlClient;
    
    namespace StellarWaterPoint31.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();
            }
        }
    }
  19. To execute the application, on the main menu, click Debug -> Start Without Debugging:

    Stellar Water Point - New Customer Account

  20. On the Water Distribution form, click the Water Bills button:

    Stellar Water Point - Water Bills

  21. On the Water Bills form, click the Create Water Bill button:

    Stellar Water Point - Create Water Bill

  22. Enter the following values in the indicated text boxes or select the date values. Then click Find Customer Account, followed by Evaluate Water Bill, followed by Save Water Bill:

    Stellar Water Point - Create Water Bill

    Stellar Water Point - Create Water Bill

    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

    Stellar Water Point - Customers

  23. Close the forms and return to your programming environment

Water Bill Details

.

Practical LearningPractical Learning: Showing a Water Bill

  1. To create a form, in the Solution Explorer, right-click WaterBills -> Add -> Form (Windows Forms)...
  2. For the Name of the form, type Details
  3. Press Enter
  4. Design the form as follows:

    Stellar Water Point - New Water Bill

    Control Text Name Other Properties
    Label Label &Water Bill #:    
    TextBox Text Box   txtBillNumber  
    Button Button &Find Water Bill btnFindWaterBill  
    GroupBox Group Box Customer Information    
    Label Label &Account #:    
    TextBox Text Box   txtAccountNumber Enabled: False
    Label Label Account Name:    
    TextBox Text Box   txtAccountName Enabled: False
    Label Label Account Type:    
    TextBox Text Box   txtAccountType
    Label Label Address:   Enabled: False
    TextBox Text Box   txtAddress Enabled: False
    TextBox Text Box   txtCity  
    TextBox Text Box   txtCounty   Enabled: False
    TextBox Text Box   txtState Enabled: False
    TextBox Text Box   txtZIPCode Enabled: False
    Label Label _________________________________________________    
    Label Label Meter Details:    
    TextBox Text Box   txtMeterDetails Enabled: False
    GroupBox Group Box Meter Reading    
    Label Label Meter &Reading Start Date:    
    Text Box Text Box   txtMeterReadingStartDate Enabled: False
    Label Label Meter Reading &End Date:    
    Text Box Text Box   txtMeterReadingEndDate Enabled: False
    Label Label Coun&ter Reading Start:    
    TextBox Text Box   txtCounterReadingStart Enabled: False
    Label Label Counter Readi&ng End:    
    TextBox Text Box   txtCounterReadingEnd Enabled: False
    GroupBox Group Box Meter Result    
    Label Label Billing Days:    
    TextBox Text Box   txtBillingDays Enabled: False
    Label Label Total HCF:    
    TextBox Text Box   txtTotalHCF Enabled: False
    Label Label Total Gallons:    
    TextBox Text Box   txtTotalGallons Enabled: False
    Label Label First Tier Consumption:    
    TextBox Text Box   txtFirstTierConsumption Enabled: False
    Label Label Second Tier:    
    TextBox Text Box   txtSecondTierConsumption Enabled: False
    Label Label Last Tier:    
    TextBox Text Box   txtLastTierConsumption Enabled: False
    GroupBox Group Box Consumption Charges    
    Label Label Water Charges:    
    TextBox Text Box   txtWaterCharges Enabled: False
    Label Label Sewer Charges:    
    TextBox Text Box   txtSewerCharges Enabled: False
    Label Label Environment Charges:    
    TextBox Text Box   txtEnvironmentCharges Enabled: False
    Label Label Service Charges:    
    TextBox Text Box   txtServiceCharges Enabled: False
    Label Label Total Charges:    
    TextBox Text Box   txtTotalCharges Enabled: False
    GroupBox Group Box Taxes    
    Label Label Local Taxes:    
    TextBox Text Box   txtLocalTaxes Enabled: False
    Label Label State Taxes:    
    TextBox Text Box   txtStateTaxes Enabled: False
    GroupBox Group Box Water Bill Payment    
    Label Label Payment Due Date:    
    Date Time Picker Text Box   dtpPaymentDueDate Enabled: False
    Label Label Amount Due:    
    TextBox Text Box   txtAmountDue Enabled: False
    Label Label Late Payment Due Date:    
    Text Box Text Box   txtLatePaymentDueDate Enabled: False
    Label Label &Late Amount Due:    
    TextBox Text Box   txtLateAmountDue  
    Button Button &Close btnClose  

    Form Properties

    Form Property Value
    FormBorderStyle FixedDialog
    Text Stellar Water Point - Water Bill Processing
    StartPosition CenterScreen
    MaximizeBox False
    MinimizeBox False
  5. On the form, double-click the Find Water Bill button
  6. Return to the form and double-click the Close button
  7. Change the document as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.WaterBills
    {
        public partial class Details : Form
        {
            public Details()
            {
                InitializeComponent();
            }
    
            private void btnFindWaterBill_Click(object sender, EventArgs e)
            {
                string strWaterBills = string.Empty;
                // Prepare a bills list to hold the records of water bills
                List<WaterBill> bills = new List<WaterBill>();
                // Specify the file that holds the records of water bills
                string fileWaterBills = @"E:\Stellar Water Point2\WaterBills.json";
    
                // Create a FileInfo object for the records of water bills
                FileInfo fiWaterBills = new FileInfo(fileWaterBills);
    
                // Find out whether a file that holds the records of water bills exists already
                if (fiWaterBills.Exists == true)
                {
                    // If that file exists, create a TextReader object to read the file
                    using (TextReader trWaterBills = new StreamReader(fiWaterBills.FullName))
                    {
                        /* Read the contents of the file that holds the water bills.
                         * Store the read text in the strWaterBills variable that was declared. */
                        strWaterBills = File.ReadAllText(fiWaterBills.FullName);
                        // Get the water bills and store them in the bills list variable
                        bills = JsonSerializer.Deserialize<List<WaterBill>>(strWaterBills)!;
    
                        IEnumerable<WaterBill> selected = bills.Where(wb => wb.BillNumber == int.Parse(txtBillNumber.Text));
    
                        foreach (var bill in selected)
                        {
                            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();
                            txtPaymentDueDate.Text = bill.PaymentDueDate.ToLongDateString();
                            txtAmountDue.Text = bill.AmountDue.ToString();
                            txtLatePaymentDueDate.Text = bill.LatePaymentDueDate.ToLongDateString();
                            txtLateAmountDue.Text = bill.LateAmountDue.ToString();
                        }
                    }
                }
    
                string strCustomers = string.Empty;
                List<Customer> clients = new List<Customer>();
                string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
    
                FileInfo fiCustomers = new FileInfo(fileCustomers);
    
                string? strMeterNumber = string.Empty;
    
                if (fiCustomers.Exists == true)
                {
                    using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
                    {
                        strCustomers = File.ReadAllText(fiCustomers.FullName);
                        clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
    
                        IEnumerable<Customer> customers = from consumer
                                                          in clients
                                                          where consumer.AccountNumber == txtAccountNumber.Text
                                                          select consumer;
    
                        foreach (Customer client in customers)
                        {
                            txtAccountName.Text = client.AccountName;
                            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;
                        }
                    }
                }
    
                if (strMeterNumber!.Length > 0)
                {
                    string strWaterMeters = string.Empty;
                    List<WaterMeter> waterMeters = new List<WaterMeter>();
                    string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                    FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                    if (fiWaterMeters.Exists == true)
                    {
                        strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                        waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                        IEnumerable<WaterMeter> meters = from measure
                                                         in waterMeters
                                                         where measure.MeterNumber == strMeterNumber
                                                         select measure;
    
                        foreach (WaterMeter meter in waterMeters)
                        {
                            txtMeterDetails.Text = meter.Make + " " +
                                                   meter.Model +
                                                   " (Meter Size: " + meter.MeterSize + ")";
                        }
                    }
                }
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
  8. In the Solution Explorer, below the WaterBills folder, double-click Central.cs to open its form
  9. From the Toolbox, add a button to the form below the list view and to the right of the New Water Bill button
  10. Change the characteristics of the button as follows:

    Stellar Water Point - Water Meters

    Control (Name) Other Properties
    ListView List View lvwWaterBills No Change
    Button Button No Change No Change
    Button Button btnWaterBillDetails Water Bill &Details...
    Anchor: Bottom, Right
  11. Double-click the Water Bill &Details button
  12. Change the document as follows:
    using System.Data;
    using Microsoft.Data.SqlClient;
    
    namespace StellarWaterPoint31.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();
            }
        }
    }
  13. To execute the application, on the main menu, click Debug -> Start Without Debugging

    Stellar Water Point

  14. On the Water Distribution form, click the Water Bills button:

    Stellar Water Point - Customers

  15. On the Water Bills form, click the View Water Bill button:

    Stellar Water Point - View Water Bill

  16. In the Water Bill # text box, type 917829

    Stellar Water Point - View Water Bill

  17. Click the Find Water Bill button:

    Stellar Water Point - View Water Bill

  18. Close the forms and return to your programming environment

Water Bill Edition

.

Practical LearningPractical Learning: Creating a Water Bill Editor

  1. In the Solution Explorer, right-click the WaterBills folder -> Add -> Form (Windows Forms...)
  2. Set the name of the form to Editor
  3. Click Add
  4. Using the Properties window, change the size of the new form to match the size of the Process Water Bill form
  5. Select everything in the Process Water Bill form and copy it
  6. Paste it in Water Bill Editor form
  7. Change the design of the Water Bill Editor form as follows (you will add only the controls that are not found in the New Water Bill form):

    Stellar Water Point - Water Bill Editor

    Control (Name) Text Additional Properties
    Button Button btnFindWaterBill &Find  
    Button Button btnUpdateWaterBill &Update Water Bill  
  8. On the form, double-click Find Water Bill button
  9. Change the document as follows:
    using StellarWaterPoint4.Models;
    using System.Text.Json;
    
    namespace StellarWaterPoint4.WaterBills
    {
        public partial class Editor : Form
        {
            public Editor()
            {
                InitializeComponent();
            }
    
            private void btnFindWaterBill_Click(object sender, EventArgs e)
            {
                string strWaterBills = string.Empty;
                List<WaterBill> bills = new List<WaterBill>();
                string fileWaterBills = @"E:\Stellar Water Point2\WaterBills.json";
    
                FileInfo fiWaterBills = new FileInfo(fileWaterBills);
    
                if (fiWaterBills.Exists == true)
                {
                    using (TextReader trWaterBills = new StreamReader(fiWaterBills.FullName))
                    {
                        strWaterBills = File.ReadAllText(fiWaterBills.FullName);
                        bills = JsonSerializer.Deserialize<List<WaterBill>>(strWaterBills)!;
    
                        IEnumerable<WaterBill> selected = bills.Where(wb => wb.BillNumber == int.Parse(txtBillNumber.Text));
    
                        foreach (var bill in selected)
                        {
                            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();
                            dtpPaymentDueDate.Value = bill.PaymentDueDate;
                            txtAmountDue.Text = bill.AmountDue.ToString();
                            dtpLatePaymentDueDate.Value = bill.LatePaymentDueDate;
                            txtLateAmountDue.Text = bill.LateAmountDue.ToString();
                        }
                    }
                }
    
                string strCustomers = string.Empty;
                List<Customer> clients = new List<Customer>();
                string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
    
                FileInfo fiCustomers = new FileInfo(fileCustomers);
    
                string? strMeterNumber = string.Empty;
    
                if (fiCustomers.Exists == true)
                {
                    using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
                    {
                        strCustomers = File.ReadAllText(fiCustomers.FullName);
                        clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
    
                        IEnumerable<Customer> customers = from consumer
                                                          in clients
                                                          where consumer.AccountNumber == mtbAccountNumber.Text
                                                          select consumer;
    
                        foreach (Customer client in customers)
                        {
                            txtAccountName.Text = client.AccountName;
                            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;
                        }
                    }
                }
    
                if (strMeterNumber!.Length > 0)
                {
                    string strWaterMeters = string.Empty;
                    List<WaterMeter> waterMeters = new List<WaterMeter>();
                    string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                    FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                    if (fiWaterMeters.Exists == true)
                    {
                        strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                        waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                        IEnumerable<WaterMeter> meters = from measure
                                                         in waterMeters
                                                         where measure.MeterNumber == strMeterNumber
                                                         select measure;
    
                        foreach (WaterMeter meter in waterMeters)
                        {
                            txtMeterDetails.Text = meter.Make + " " +
                                                   meter.Model +
                                                   " (Meter Size: " + meter.MeterSize + ")";
                        }
                    }
                }
            }
        }
    }
  10. Return to the form and double-click the Find Customer Account button
  11. Implement the event as follows:
    using StellarWaterPoint4.Models;
    using System.Text.Json;
    
    namespace StellarWaterPoint4.WaterBills
    {
        public partial class Editor : Form
        {
            public Editor()
            {
                InitializeComponent();
            }
    
            private void btnFindWaterBill_Click(object sender, EventArgs e)
            {
                . . .
            }
    
            private void btnFindCustomerAccount_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(mtbAccountNumber.Text))
                {
                    MessageBox.Show("You must type a valid account number of a customer, " +
                                    "and then click the Find Customer Account button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strCustomers = string.Empty;
                List<Customer> clients = new List<Customer>();
                string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
    
                FileInfo fiCustomers = new FileInfo(fileCustomers);
    
                string? strMeterNumber = string.Empty;
    
                if (fiCustomers.Exists == true)
                {
                    using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
                    {
                        strCustomers = File.ReadAllText(fiCustomers.FullName);
                        clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
    
                        IEnumerable<Customer> customers = clients.Where(cust => cust.AccountNumber == mtbAccountNumber.Text);
    
                        foreach (Customer client in customers)
                        {
                            txtAccountName.Text = client.AccountName;
                            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;
                        }
                    }
                }
    
                if (strMeterNumber!.Length > 0)
                {
                    string strWaterMeters = string.Empty;
                    List<WaterMeter> waterMeters = new List<WaterMeter>();
                    string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                    FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                    if (fiWaterMeters.Exists == true)
                    {
                        strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                        waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                        IEnumerable<WaterMeter> meters = waterMeters.Where(wm => wm.MeterNumber == strMeterNumber);
    
                        foreach (WaterMeter meter in meters)
                        {
                            txtMeterDetails.Text = meter.Make + " " +
                                                   meter.Model +
                                                   " (Meter Size: " + meter.MeterSize + ")";
                        }
                    }
                }
            }
        }
    }
  12. Return to the form and double-click the Meter Reading End Date date time picker control
  13. Implement the event as follows:
    using System.Data;
    using Microsoft.Data.SqlClient;
    
    namespace StellarWaterPoint31.WaterBills
    {
        public partial class Editor : Form
        {
            public Editor()
            {
                InitializeComponent();
            }
    
            private void btnFindWaterBill_Click(object sender, EventArgs e)
            {
                . . .
            }
    
            private void btnFindCustomerAccount_Click(object sender, EventArgs e)
            {
                . . .
            }
    
            private void dtpMeterReadingEndDate_ValueChanged(object sender, EventArgs e)
            {
                TimeSpan tsDays = dtpMeterReadingEndDate.Value - dtpMeterReadingStartDate.Value;
    
                txtBillingDays.Text = (tsDays.Days + 1).ToString();
            }
        }
    }
  14. Return to the form and double-click the Evaluate Water Bill button
  15. Implement the event as follows:
    using StellarWaterPoint4.Models;
    using System.Text.Json;
    
    namespace StellarWaterPoint4.WaterBills
    {
        public partial class Editor : Form
        {
            public Editor()
            {
                InitializeComponent();
            }
    
            private void btnFindWaterBill_Click(object sender, EventArgs e)
            {
                . . .
            }
    
            private void btnFindCustomerAccount_Click(object sender, EventArgs e)
            {
                . . .
            }
    
            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)
                {
                    MessageBox.Show("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)
                {
                    MessageBox.Show("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");
            }
        }
    }
  16. Return to the form and double-click the Update Water Bill button
  17. Return to the form and double-click the Close button
  18. Change the document as follows:
       
                    
    Close();
  19. In the Solution Explorer, below the WaterBills folder, double-click Central.cs
  20. From the Toolbox, add a button to the form below the list view and on the right side of the View Water Bill button
  21. Change the characteristics of the button as follows:

    Stellar Water Point - Water Meters

    Control (Name) Text Other Properties
    ListView List View lvwWaterBills   No Change
    Button Button btnNewWaterBill   No Change
    Button Button btnViewWaterBill   No Change
    Button Button btnEditWaterBill &Edit Water Bill... Anchor: Bottom, Right
  22. Double-click the Edit Water Bill button
  23. Implement the event as follows:
    using System.Data;
    using Microsoft.Data.SqlClient;
    
    namespace StellarWaterPoint31.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)
            {
                
            }
        }
    }
  24. To execute the application, on the main menu, click Debug -> Start Without Debugging

    Stellar Water Point

  25. On the main form of the application, click the Customers button:

    Stellar Water Point - Water Bills

  26. Click the Edit Water Bill button:

    Stellar Water Point - Water Bill Editor

  27. In the Water Bill # text, type 923633
  28. Click the Find Water Bill button

    Stellar Water Point - Water Bill Editor

  29. Change the following values:
    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
  30. Click the Evaluate Water Bill button:

    Stellar Water Point - New Water Bill

  31. Click the Update Water Bill button and click OK on the message box:

    Stellar Water Point - Water Bills

  32. Close the forms and return to your programming environment

Water Bill Deletion

.

Practical LearningPractical Learning: Editing/Updating a Record

  1. To create a form, in the Solution Explorer, right-click the WaterBills folder -> Add -> Form (Windows Forms...)
  2. Change the file Name to Delete
  3. Click Add Resize the form to have the same size as the Water Bill Details form
  4. Select and copy everything in the Water Bill Details form
  5. Paste it in the new Water Bill Delete form
  6. Change the design of the form as follows (you will add only one button):

    Stellar Water Point - Water Bill Deletion

    Control (Name) Text
    Button Button btnDeleteWaterBill &Delete Water Bill
  7. On the form, double-click the Find Water Bill button
  8. Change the document as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.WaterBills
    {
        public partial class Delete : Form
        {
            public Delete()
            {
                InitializeComponent();
            }
    
            private void btnFindWaterBill_Click(object sender, EventArgs e)
            {
                string strWaterBills = string.Empty;
                List<WaterBill> bills = new List<WaterBill>();
                string fileWaterBills = @"E:\Stellar Water Point2\WaterBills.json";
    
                FileInfo fiWaterBills = new FileInfo(fileWaterBills);
    
                if (fiWaterBills.Exists == true)
                {
                    using (TextReader trWaterBills = new StreamReader(fiWaterBills.FullName))
                    {
                        strWaterBills = File.ReadAllText(fiWaterBills.FullName);
                        bills = JsonSerializer.Deserialize<List<WaterBill>>(strWaterBills)!;
    
                        IEnumerable<WaterBill> selected = bills.Where(wb => wb.BillNumber == int.Parse(txtBillNumber.Text));
    
                        foreach (var bill in selected)
                        {
                            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();
                            txtPaymentDueDate.Text = bill.PaymentDueDate.ToLongDateString();
                            txtAmountDue.Text = bill.AmountDue.ToString();
                            txtLatePaymentDueDate.Text = bill.LatePaymentDueDate.ToLongDateString();
                            txtLateAmountDue.Text = bill.LateAmountDue.ToString();
                        }
                    }
                }
    
                string strCustomers = string.Empty;
                List<Customer> clients = new List<Customer>();
                string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
    
                FileInfo fiCustomers = new FileInfo(fileCustomers);
    
                string? strMeterNumber = string.Empty;
    
                if (fiCustomers.Exists == true)
                {
                    using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
                    {
                        strCustomers = File.ReadAllText(fiCustomers.FullName);
                        clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
    
                        IEnumerable<Customer> customers = from consumer
                                                          in clients
                                                          where consumer.AccountNumber == txtAccountNumber.Text
                                                          select consumer;
    
                        foreach (Customer client in customers)
                        {
                            txtAccountName.Text = client.AccountName;
                            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;
                        }
                    }
                }
    
                if (strMeterNumber!.Length > 0)
                {
                    string strWaterMeters = string.Empty;
                    List<WaterMeter> waterMeters = new List<WaterMeter>();
                    string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                    FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                    if (fiWaterMeters.Exists == true)
                    {
                        strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                        waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                        IEnumerable<WaterMeter> meters = from measure
                                                         in waterMeters
                                                         where measure.MeterNumber == strMeterNumber
                                                         select measure;
    
                        foreach (WaterMeter meter in waterMeters)
                        {
                            txtMeterDetails.Text = meter.Make + " " +
                                                   meter.Model +
                                                   " (Meter Size: " + meter.MeterSize + ")";
                        }
                    }
                }
            }
        }
    }
  9. Return to the form and double-click the Delete Water Bill button
  10. Return to the form and double-click the Close button
  11. Change the document as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.WaterBills
    {
        public partial class Delete : Form
        {
            public Delete()
            {
                InitializeComponent();
            }
    
            private void btnFindWaterBill_Click(object sender, EventArgs e)
            {
                string strWaterBills = string.Empty;
                // Prepare a bills list to hold the records of water bills
                List<WaterBill> bills = new List<WaterBill>();
                // Specify the file that holds the records of water bills
                string fileWaterBills = @"E:\Stellar Water Point2\WaterBills.json";
    
                // Create a FileInfo object for the records of water bills
                FileInfo fiWaterBills = new FileInfo(fileWaterBills);
    
                // Find out whether a file that holds the records of water bills exists already
                if (fiWaterBills.Exists == true)
                {
                    // If that file exists, create a TextReader object to read the file
                    using (TextReader trWaterBills = new StreamReader(fiWaterBills.FullName))
                    {
                        /* Read the contents of the file that holds the water bills.
                         * Store the read text in the strWaterBills variable that was declared. */
                        strWaterBills = File.ReadAllText(fiWaterBills.FullName);
                        // Get the water bills and store them in the bills list variable
                        bills = JsonSerializer.Deserialize<List<WaterBill>>(strWaterBills)!;
    
                        IEnumerable<WaterBill> selected = bills.Where(wb => wb.BillNumber == int.Parse(txtBillNumber.Text));
    
                        foreach (var bill in selected)
                        {
                            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();
                            txtPaymentDueDate.Text = bill.PaymentDueDate.ToLongDateString();
                            txtAmountDue.Text = bill.AmountDue.ToString();
                            txtLatePaymentDueDate.Text = bill.LatePaymentDueDate.ToLongDateString();
                            txtLateAmountDue.Text = bill.LateAmountDue.ToString();
                        }
                    }
                }
    
                string strCustomers = string.Empty;
                List<Customer> clients = new List<Customer>();
                string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
    
                FileInfo fiCustomers = new FileInfo(fileCustomers);
    
                string? strMeterNumber = string.Empty;
    
                if (fiCustomers.Exists == true)
                {
                    using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
                    {
                        strCustomers = File.ReadAllText(fiCustomers.FullName);
                        clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
    
                        IEnumerable<Customer> customers = from consumer
                                                          in clients
                                                          where consumer.AccountNumber == txtAccountNumber.Text
                                                          select consumer;
    
                        foreach (Customer client in customers)
                        {
                            txtAccountName.Text = client.AccountName;
                            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;
                        }
                    }
                }
    
                if (strMeterNumber!.Length > 0)
                {
                    string strWaterMeters = string.Empty;
                    List<WaterMeter> waterMeters = new List<WaterMeter>();
                    string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
    
                    FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
    
                    if (fiWaterMeters.Exists == true)
                    {
                        strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
                        waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
    
                        IEnumerable<WaterMeter> meters = from measure
                                                         in waterMeters
                                                         where measure.MeterNumber == strMeterNumber
                                                         select measure;
    
                        foreach (WaterMeter meter in waterMeters)
                        {
                            txtMeterDetails.Text = meter.Make + " " +
                                                   meter.Model +
                                                   " (Meter Size: " + meter.MeterSize + ")";
                        }
                    }
                }
            }
    
            private void btnDeleteWaterBill_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(txtBillNumber.Text))
                {
                    MessageBox.Show("Please provide the bill number of the water bill you want to delete, " +
                                    "and then click the Find Water Bill button.",
                                    "Stellar Water Point", MessageBoxButtons.OK);
                    return;
                }
    
                string strWaterBills = string.Empty;
                List<WaterBill> waterBills = new List<WaterBill>();
                string fileWaterBills = @"E:\Stellar Water Point2\WaterBills.json";
    
                FileInfo fiWaterBills = new FileInfo(fileWaterBills);
    
                if (fiWaterBills.Exists == true)
                {
                    strWaterBills = File.ReadAllText(fiWaterBills.FullName);
                    waterBills = JsonSerializer.Deserialize<List<WaterBill>>(strWaterBills)!;
    
                    WaterBill bill = waterBills.Find(bl => bl.BillNumber == int.Parse(txtBillNumber.Text))!;
    
                    if (bill is not null)
                    {
                        if (MessageBox.Show("Are you sure you want to delete (or remove) or cancel " +
                                          "this water bill from the system (you cannot undo the action)?",
                                          "Stellar Water Point",
                                          MessageBoxButtons.YesNo,
                                          MessageBoxIcon.Information) == DialogResult.Yes)
                        {
                            waterBills.Remove(bill);
    
                            JsonSerializerOptions options = new JsonSerializerOptions();
                            options.WriteIndented = true;
    
                            string jsWaterBills = JsonSerializer.Serialize(waterBills, typeof(List<WaterBill>), options);
                            File.WriteAllText(fiWaterBills.FullName, jsWaterBills);
    
                            MessageBox.Show("The water bill has been deleted (or removed, or cancelled) from our system.",
                                          "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
    
                Close();
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
  12. In the Solution Explorer, below the WaterBills folder, double-click Central.cs to open its form
  13. From the Toolbox, add two buttons to the form below the list view and to the right of the Edit Water Bill button
  14. Change the form design as follows:

    Stellar Water Point - Water Bills

    Control (Name) Text Other Properties
    ListView List View lvwWaterBills   Anchor: Bottom, Top, Bottom, Left, Right
    Button Button btnProcessWaterBill &Process Water Bill... Anchor: Bottom, Right
    Button Button btnViewWaterBill &View Water Bill... Anchor: Bottom, Right
    Button Button btnEditWaterBill &Edit Water Bill... Anchor: Bottom, Right
    Button Button btnDeleteWaterBill &Delete Water Bill... Anchor: Bottom, Right
    Button Button btnClose &Close Anchor: Bottom, Right
  15. On the form, double-click the Delete Water Bill button
  16. Return to the Water Bills - Central form and double-click the Close button
  17. Implement the events as follows:
    using System.Text.Json;
    using StellarWaterPoint4.Models;
    
    namespace StellarWaterPoint4.WaterBills
    {
        public partial class Central : Form
        {
            public Central()
            {
                InitializeComponent();
            }
    
            private void ShowWaterBills()
            {
                // We will need the list of customers to display an account summary in the list view
                // Declare a strCustomers variable that will hold the records from the JSON file
                string strCustomers = string.Empty;
                // Prepare a list to hold the records of customers
                List<Customer> clients = new List<Customer>();
                // Specify the file that holds the records of customers
                string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
    
                // Create a FileInfo object for the records of customers
                FileInfo fiCustomers = new FileInfo(fileCustomers);
    
                // Check if a file that holds the records of customers was created already
                if (fiCustomers.Exists == true)
                {
                    // If that file exists, create a TextReader object to get those records
                    using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
                    {
                        // Read the records and store them in the strCustomers variable
                        strCustomers = File.ReadAllText(fiCustomers.FullName);
                        // Use JSON deserialization to get the records of customers
                        clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
                    }
                }
    
                // Declare a strWaterBills variable that will hold the records from the JSON file
                string strWaterBills = string.Empty;
                // Prepare a bills list to hold the records of water bills
                List<WaterBill> bills = new List<WaterBill>();
                // Specify the file that holds the records of water bills
                string fileWaterBills = @"E:\Stellar Water Point2\WaterBills.json";
    
                // Create a FileInfo object for the records of water bills
                FileInfo fiWaterBills = new FileInfo(fileWaterBills);
    
                // Find out whether a file that holds the records of water bills exists already
                if (fiWaterBills.Exists == true)
                {
                    // If that file exists, create a TextReader object to read the file
                    using (TextReader trWaterBills = new StreamReader(fiWaterBills.FullName))
                    {
                        /* Read the contents of the file that holds the water bills.
                         * Store the read text in the strWaterBills variable that was declared. */
                        strWaterBills = File.ReadAllText(fiWaterBills.FullName);
                        // Get the water bills and store them in the bills list variable
                        bills = JsonSerializer.Deserialize<List<WaterBill>>(strWaterBills)!;
    
                        // This variable will be used to keep count of the water bills records
                        int counter = 1;
    
                        /* We are about to display the water bills records in the list view.
                         * Before proceeding, first remove any record in the list view. */
                        lvwWaterBills.Items.Clear();
    
                        // Visit each record of the water bills
                        foreach (WaterBill client in bills)
                        {
                            /* Prepare a ListViewItem object for each record.
                             * Display the record counter in the first column of this object. */
                            ListViewItem lviWaterBill = new ListViewItem(counter++.ToString());
    
                            // Display the water bill number of the current record
                            lviWaterBill.SubItems.Add(client.BillNumber.ToString());
    
                            /* Refer to the list of records of the customers (that list was prepared earlier).
                             * Use LINQ to get the customer whose account number is 
                             * the same as the account number of the current record. */
                            IEnumerable<Customer> customer = clients.Where(cust => cust.AccountNumber == client.AccountNumber);
    
                            // Now that we have located the customer record, display some details about it
                            foreach (Customer cust in customer)
                            {
                                lviWaterBill.SubItems.Add(client.AccountNumber + " - " +
                                                          cust.AccountName +
                                                          ", Type: " + cust.AccountType![..3] +
                                                          ", (Mtr #: " + cust.MeterNumber + ")");
                            }
    
                            /* Continue displaying some other parts of the water bill.
                             * Before there is not enough space for the whole water bill, we display only some values. */
                            lviWaterBill.SubItems.Add(client.MeterReadingStartDate.ToShortDateString());
                            lviWaterBill.SubItems.Add(client.MeterReadingEndDate.ToShortDateString());
                            lviWaterBill.SubItems.Add(client.BillingDays.ToString());
                            lviWaterBill.SubItems.Add(client.CounterReadingStart.ToString());
                            lviWaterBill.SubItems.Add(client.CounterReadingEnd.ToString());
                            lviWaterBill.SubItems.Add(client.TotalHCF.ToString());
                            lviWaterBill.SubItems.Add(client.TotalGallons.ToString());
                            lviWaterBill.SubItems.Add(client.PaymentDueDate.ToShortDateString());
                            lviWaterBill.SubItems.Add(client.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.ShowDialog();
    
                ShowWaterBills();
            }
    
            private void btnViewWaterBill_Click(object sender, EventArgs e)
            {
                Details details = new();
    
                details.Show();
            }
    
            private void btnEditWaterBill_Click(object sender, EventArgs e)
            {
                Editor editor = new();
    
                editor.ShowDialog();
    
                ShowWaterBills();
            }
    
            private void btnDeleteWaterBill_Click(object sender, EventArgs e)
            {
                Delete delete = new();
    
                delete.ShowDialog();
    
                ShowWaterBills();
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
  18. To execute, on the main menu, click Debug -> Start Without Debugging:

  19. On the Stellar Water Point form, click the Water Bills button

    Stellar Water Point - Water Bills

  20. On the Water Bills form, click the Delete Water Bill button

    Stellar Water Point - Water Bill Deletion

  21. In the Water Bill # text box, type 917829

    Stellar Water Point - Water Bill Deletion

  22. Click Find Water Bill

    Stellar Water Point - Water Bill Deletion

  23. Click Delete Water Bill
  24. Read the message in the message box and click Yes:

    Stellar Water Point - Water Bills

  25. Close the forms and return to your programming environment
  26. Close Microsoft Visual Studio

Home Copyright © 2010-2024, FunctionX, Inc. Sunday 11 June 2023 Home