Starting the Project

Introduction

A water distribution company delivers water to homes and commercial buildings. In this exercise, we will create an application for a fictitious company that distributes and sells water. This version of the application creates and stores records in XML.

Practical LearningPractical Learning: Introducing XML Node Maintenance

  1. Start Microsoft Visual Studio
  2. On the Visual Studio 2022 dialog box, click Create a New Project:

    Visual Studio 2022 - Create a New Project

  3. In the Create a New Project dialog box, in the languages combo box, select C#:

    Visual Studio 2022 - Create a New Project

  4. In the list of projects templates, click ASP.NET Web Application (.NET Framework):

    Visual Studio 2022 - Create a New Project

  5. Click Next
  6. Change the Project Name to StellarWaterPoint1.
    Set the location to anything you want.
    In the Framework combo box, select th highest version (.NET Framework 4.8)

    Visual Studio 2022 - Configure Your New Project

  7. Click Create
  8. In the Create a New ASP.NET Web Application wizard page, click the MVC icon

    Visual Studio 2022 - Create A New ASP.NET Web Application - MVC

  9. Click Create
  10. In the Solution Explorer, right-click StellarWaterPoint1 -> Add -> New Folder
  11. Type Images
  12. Add the following photos to the Images folder:
    Stellar Water Point - Water Bills Stellar Water Point - Customers Accounts
    Stellar Water Point - Water Meters Stellar Water Point - Community Services
    Stellar Water Point - Legal Affairs Stellar Water Point - Employees Portal
  13. In the Solution Explorer, expand Content
  14. In the Content folder, double-click Site.css to open it
  15. Change the document as follows:
  16. In the Solution Explorer, right-click WaterDistribution2 -> Add -> New Folder
  17. Type WaterDistribution

Setting Up the Site

.

Practical LearningPractical Learning: Setting Up the Ŝite

  1. In the Solution Explorer, expand Views
  2. Under Views, expand Shared
  3. Double-click _Layout.cshtml and change the document as follows:
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>@ViewBag.Title - Stellar Water Point</title>
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
    </head>
    <body>
        <nav class="common-font navbar navbar-expand-sm navbar-toggleable-sm navbar-dark bg-dark">
            <div class="container">
                @Html.ActionLink("Stellar Water Point", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
                <button type="button" class="navbar-toggler" data-bs-toggle="collapse"
                        data-bs-target=".navbar-collapse" title="Toggle navigation" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse d-sm-inline-flex justify-content-between">
                    <ul class="navbar-nav flex-grow-1">
                        <li>@Html.ActionLink("Water Bills", "Index", "WaterBills", new { area = "" }, new { @class = "nav-link" })</li>
                        <li>@Html.ActionLink("Customers", "Index", "Customers", new { area = "" }, new { @class = "nav-link" })</li>
                        <li>@Html.ActionLink("Water Meters", "Index", "WaterMeters", new { area = "" }, new { @class = "nav-link" })</li>
                        <li>@Html.ActionLink("About", "About", "Home", new { area = "" }, new { @class = "nav-link" })</li>
                        <li>@Html.ActionLink("Contact", "Contact", "Home", new { area = "" }, new { @class = "nav-link" })</li>
                    </ul>
                </div>
            </div>
        </nav>
        <div class="container body-content">
            @RenderBody()
            <hr />
            <footer>
                <p class="common-font text-center">&copy; 2011-@DateTime.Now.Year - Stellar Water Point</p>
            </footer>
        </div>
    
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/bootstrap")
        @RenderSection("scripts", required: false)
    </body>
    </html>
  4. In the Solution Explorer, expand Controllers
  5. Under Controllers, double-click HomeController
  6. In the Solution Explorer, under Views, expand Home
  7. In the Index() method, write a line to create a directory where the records of the application will be stored:
    using System.IO;
    using System.Web.Mvc;
    
    namespace StellarWaterPoint1.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                Directory.CreateDirectory(@"C:\Stellar Water Point");
    
                return View();
            }
    
            public ActionResult About()
            {
                ViewBag.Message = "Your application description page.";
    
                return View();
            }
    
            public ActionResult Contact()
            {
                ViewBag.Message = "Your contact page.";
    
                return View();
            }
        }
    }
  8. In the Solution Explorer, expand Views, and expand Home
  9. Below Home, double-click Index.cshtml
  10. Change the document as follows:
    @{
        ViewBag.Title = "Home Page";
    }
    
    <main class="common-font">
        <section class="row text-center" aria-labelledby="aspnetTitle">
            <h1 id="title" class="display-4 fw-bold">Stellar Water Point</h1>
            <p class="lead">Stellar Water Point is a community-based company that commercially distributes water to customers in need. Our water process is socially responsible and community oriented.</p>
            </p>
        </section>
    
        <div class="row">
            <section class="col-md-4" aria-labelledby="gettingStartedTitle">
                <div class="card mb-4 box-shadow">
                    <img class="bordered card-img-top" src="../Images/WaterBills.png" alt="Water Bills">
                    <div class="card-body">
                        <h5><a class="nav-link text-dark fw-bold" href="/WaterBills/Index">Water Bills</a></h5>
                        <hr />
                        <p class="card-text">Water bills are processed in a timely and responsible manner, applying only the strictest, regular, and lawful rules.</p>
                    </div>
                </div>
            </section>
            <section class="col-md-4" aria-labelledby="librariesTitle">
                <div class="card mb-4 box-shadow">
                    <img class="bordered card-img-top" src="../Images/Customers.png" alt="Customers Accounts">
                    <div class="card-body">
                        <h5><a class="nav-link text-dark fw-bold" href="/Customers/Index">Customers Accounts</a></h5>
                        <hr />
                        <p class="card-text">Water bills are sent in a trimester-base to our customers who holds an account with us, all for good service.</p>
                    </div>
                </div>
            </section>
            <section class="col-md-4" aria-labelledby="hostingTitle">
                <div class="card mb-4 box-shadow">
                    <img class="bordered card-img-top" src="../Images/WaterMeter.png" alt="Water Meters">
                    <div class="card-body">
                        <h5><a class="nav-link text-dark fw-bold" href="/WaterMeters/Index">Water Meters</a></h5>
                        <hr />
                        <p class="card-text">We use industry standard water meters that are regularly government inspected for their accuracy and precision.</p>
                    </div>
                </div>
            </section>
        </div>
        <div class="row">
            <section class="col-md-4" aria-labelledby="gettingStartedTitle">
                <div class="card mb-4">
                    <img class="card-img-top bordered" src="../Images/Community.png" alt="Community Services">
                    <div class="card-body">
                        <h5><a class="nav-link text-dark fw-bold" href="/StellarWaterPoint">Community Services</a></h5>
                        <hr />
                        <p class="card-text">Stellar Water Point is a community-oriented company that works withn various local activities and authorities.</p>
                    </div>
                </div>
            </section>
            <section class="col-md-4" aria-labelledby="librariesTitle">
                <div class="card mb-4">
                    <img class="card-img-top bordered" src="../Images/LegalAffairs.png" alt="Legal Affairs">
                    <div class="card-body">
                        <h5><a class="nav-link text-dark fw-bold" href="/StellarWaterPoint">Legal Affairs</a></h5>
                        <hr />
                        <p class="card-text">Issues of regulations and government affairs are addressed here. This is available for employees, contractors, etc.</p>
                    </div>
                </div>
            </section>
            <section class="col-md-4" aria-labelledby="hostingTitle">
                <div class="card mb-4">
                    <img class="card-img-top bordered" src="../Images/Employees.png" alt="Employees Portal">
                    <div class="card-body">
                        <h5><a class="nav-link text-dark fw-bold" href="/StellarWaterPoint">Employees Portal</a></h5>
                        <hr />
                        <p class="card-text">This is a central area form employees to access the company resources such as time sheets, payroll, benefits, etc.</p>
                    </div>
                </div>
            </section>
        </div>
    </main>

Water Meters

Introduction

Utility companies use a device that measure what their customers consume. For example a water utility company installs water meters in residences and commercial buildings to find out how much water has been used. In this section, we will create a view that allows an employee to keep track of water consumption.

Practical LearningPractical Learning: Introducing Water Meters

  1. In the Solution Explorer, right-click Controllers -> Add -> Controller...
  2. In the middle frame of the Add Scaffold dialog box, click MVC 5 Controller With Read/Write Actions:

    Add New Scaffolded Item

  3. Click Add
  4. In the name, replace Default with WaterMeters to get WaterMetersController
  5. Click Add

Setting Up a Water Meter

.

Practical LearningPractical Learning: Setting Up a Water Meter

  1. In the WaterMetersController class, change the second Create() method as follows:
    using System.IO;
    using System.Xml;
    using System.Web.Mvc;
    
    namespace StellarWaterPoint1.Controllers
    {
        public class WaterMetersController : Controller
        {
            // GET: WaterMeters
            public ActionResult Index()
            {
                return View();
            }
    
            // GET: WaterMeters/Details/5
            public ActionResult Details(int id)
            {
                return View();
            }
    
            // GET: WaterMeters/Create
            public ActionResult Create()
            {
                return View();
            }
    
            // POST: WaterMeters/Create
            [HttpPost]
            public ActionResult Create(FormCollection collection)
            {
                try
                {
                    // TODO: Add insert logic here
                    XmlDocument xdWaterMeters = new XmlDocument();
                    string strFileWaterMeters = Server.MapPath("/WaterDistribution/WaterMeters.xml");
    
                    if (!string.IsNullOrEmpty(collection["txtMeterNumber"]))
                    {
                        if (System.IO.File.Exists(strFileWaterMeters))
                        {
                            using(FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.OpenOrCreate,
                                                                            FileAccess.ReadWrite, FileShare.ReadWrite))
                            {
    
                                xdWaterMeters.Load(fsWaterMeters);
                            }
                        }
                        else
                        {
                            xdWaterMeters.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                                  "<water-meters></water-meters>");
                        }
                    }
    
                    XmlElement xeWaterMeter = xdWaterMeters.CreateElement("water-meter");
    
                    string strWaterMeter = "<water-meter-id>" + collection["txtWaterMeterId"] + "</water-meter-id>" + 
                                           "<meter-number>"   + collection["txtMeterNumber"]  + "</meter-number>"   +
                                           "<make>"           + collection["txtMake"]         + "</make>"           +
                                           "<model>"          + collection["txtModel"]        + "</model>"          +
                                           "<meter-size>"     + collection["txtMeterSize"]    + "</meter-size>";
    
                    xeWaterMeter.InnerXml = strWaterMeter;
                    xdWaterMeters.DocumentElement.AppendChild(xeWaterMeter);
    
                    using(FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Create,
                                                                    FileAccess.Write, FileShare.Write))
                    {
                        xdWaterMeters.Save(fsWaterMeters);
                    }
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
    
            // GET: WaterMeters/Edit/5
            public ActionResult Edit(int id)
            {
                return View();
            }
    
            // POST: WaterMeters/Edit/5
            [HttpPost]
            public ActionResult Edit(int id, FormCollection collection)
            {
                try
                {
                    // TODO: Add update logic here
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
    
            // GET: WaterMeters/Delete/5
            public ActionResult Delete(int id)
            {
                return View();
            }
    
            // POST: WaterMeters/Delete/5
            [HttpPost]
            public ActionResult Delete(int id, FormCollection collection)
            {
                try
                {
                    // TODO: Add delete logic here
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
        }
    }
  2. In the WaterMetersController class, right-click any section of one of the Create() methods and click Add View...
  3. In the Add New Scaffolded Item dialog box, make sure MVC 5 View is selected.
    Click Add
  4. In the Add View dialog box, make sure the View Name text box is displaying Create.
    Click Add
  5. Create a form as follows:
    @{
        ViewBag.Title = "Create Water Meter";
    }
    
    <h2 class="common-font fw-bold text-center">Create Water Meter</h2>
    
    <hr />
    
    @using (Html.BeginForm())
    {
        <div class="common-font encloser">
            <div class="row mb-2">
                @Html.Label("txtWaterMeterId", "Meter Meter Id:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
                <div class="col-md-8">
                    @Html.TextBox("txtWaterMeterId", null, htmlAttributes: new { @class = "form-control" })
                </div>
            </div>
            <div class="row mb-2">
                @Html.Label("txtMeterNumber", "Meter #:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
                <div class="col-md-8">
                    @Html.TextBox("txtMeterNumber", null, htmlAttributes: new { @class = "form-control" })
                </div>
            </div>
    
            <div class="row mb-2">
                @Html.Label("txtMake", "Make:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
                <div class="col-md-8">
                    @Html.TextBox("txtMake", null, htmlAttributes: new { @class = "form-control" })
                </div>
            </div>
    
            <div class="row mb-2">
                @Html.Label("txtModel", "Model:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
                <div class="col-md-8">
                    @Html.TextBox("txtModel", null, htmlAttributes: new { @class = "form-control" })
                </div>
            </div>
    
            <div class="row mb-2">
                @Html.Label("txtMeterSize", "Meter Size:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
                <div class="col-md-8">
                    @Html.TextBox("txtMeterSize", null, htmlAttributes: new { @class = "form-control" })
                </div>
            </div>
    
            <hr />
    
            <div class="row mb-2">
                <div class="col-md-5 col-form-label">
                    @Html.ActionLink("Water Meters", "Index", new { area = "" }, new { @class = "stellar" })
                </div>
                <div class="col-md-7">
                    <input type="submit" name="btnSaveWaterMeter" value="Save Water Meter" class="btn btn-stellar" />
                </div>
            </div>
        </div>
    }
    
    <hr />
    
    <p class="text-center">
        @Html.ActionLink("View Water Meter",   "Details", new { area = "" }, new { @class = "stellar" }) ::
        @Html.ActionLink("Edit Water Meter",   "Edit",    new { area = "" }, new { @class = "stellar" }) ::
        @Html.ActionLink("Delete Water Meter", "Delete",  new { area = "" }, new { @class = "stellar" })
    </p>

Getting a List of Water Meters

.

Practical LearningPractical Learning: Getting a List of Water Meters

  1. Click the WaterMetersController.cs tab to access the controller
  2. In the WaterMetersController class, change the Index() method as follows:
    using System.IO;
    using System.Xml;
    using System.Web.Mvc;
    
    namespace StellarWaterPoint1.Controllers
    {
        public class WaterMetersController : Controller
        {
            // GET: WaterMeters
            public ActionResult Index()
            {
                XmlDocument xdWaterMeters = new XmlDocument();
                string strFileWaterMeters = @"C:\Stellar Water Point4\WaterMeters.xml";
    
                if (System.IO.File.Exists(strFileWaterMeters))
                {
                    using (FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Open,
                                                                     FileAccess.Read, FileShare.Read))
                    {
                        xdWaterMeters.Load(fsWaterMeters);
                    }
    
                    if (xdWaterMeters.DocumentElement.ChildNodes.Count > 0)
                    {
                        ViewData["WaterMeters"] = xdWaterMeters.DocumentElement.ChildNodes;
                    }
                    else
                    {
                        ViewData["WaterMeters"] = null;
                    }
                }
    
                return View();
            }
    
            . . .
        }
    }
  3. In the WaterMetersController class, right-click anything from the Index() line to the closing curly bracket of the Index() method and click Add View...
  4. In the Add New Scaffolded Item dialog box, make sure MVC 5 View is selected.
    Click Add
  5. In the Add View dialog box, make sure the View Name text box is displaying Index.
    Click Add
  6. Chreate a form as follows:
    @{
        ViewBag.Title = "Water Meters";
    }
    
    <h2 class="fw-bold common-font text-center">Water Meters</h2>
    
    <hr />
    
    <table class="table table-striped common-font">
        <tr>
            <th class="fw-bold text-center">Water Meter Id</th>
            <th class="fw-bold">Meter #</th>
            <th class="fw-bold">Make</th>
            <th class="fw-bold">Model</th>
            <th class="fw-bold">Meter Size</th>
            <th>@Html.ActionLink("New Water Meter", "Create", new { area = "" }, new { @class = "stellar" })</th>
        </tr>
    
        @if (ViewBag.waterMeters != null)
        {
            foreach (System.Xml.XmlNode meter in ViewBag.WaterMeters as System.Xml.XmlNodeList)
            {
                <tr>
                    <td class="text-center">@meter.FirstChild.InnerText</td>
                    <td>@meter.FirstChild.NextSibling.InnerText</td>
                    <td>@meter.FirstChild.NextSibling.NextSibling.InnerText</td>
                    <td>@meter.FirstChild.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@meter.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>
                        @Html.ActionLink("Update", "Edit",    new { id = @meter.FirstChild.InnerText }, new { @class = "stellar" }) ::
                        @Html.ActionLink("Review", "Details", new { id = @meter.FirstChild.InnerText }, new { @class = "stellar" }) ::
                        @Html.ActionLink("Remove", "Delete",  new { id = @meter.FirstChild.InnerText }, new { @class = "stellar" })
                    </td>
                </tr>
            }
        }
    </table>
  7. Click the WaterMetersController.cs tab to access the controller
  8. To execute the application, on the main menu, click Debug -> Start Without Debugging

    Stellar Water Point

  9. Click the Water Meters link

    Stellar Water Point

  10. Click the New Water Meter link

    Stellar Water Point

  11. Type each of the following rows of records and click Save Water Meter for each:
     
    Water Meter Id Meter # Make Model Meter Size
    1 392-44-572 Constant Tech TG-4822 5/8 Inches
    2 938-75-869 Stan Wood 266G Half Inch
    3 588-279-663 Estellano NCF-226 1 1/2 Inches

    Stellar Water Point

  12. Return to your programming environment

Getting a Details of a Water Meter

.

Practical LearningPractical Learning: Getting a Details of a Water Meter

  1. In the WaterMetersController class, change the Details() method as follows:
    using System.IO;
    using System.Xml;
    using System.Web.Mvc;
    
    namespace StellarWaterPoint1.Controllers
    {
        public class WaterMetersController : Controller
        {
            . . .
    
            // GET: WaterMeters/Details/5
            public ActionResult Details(int id)
            {
                XmlDocument xdWaterMeters = new XmlDocument();
                string strFileWaterMeters = Server.MapPath("/WaterDistribution/WaterMeters.xml");
    
                if (System.IO.File.Exists(strFileWaterMeters))
                {
                    using (FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Open,
                                                                     FileAccess.Read, FileShare.Read))
                    {
                        xdWaterMeters.Load(fsWaterMeters);
    
                        XmlNodeList xnlWaterMeters = xdWaterMeters.GetElementsByTagName("water-meter-id");
    
                        foreach (XmlNode xnWaterMeter in xnlWaterMeters)
                        {
                            if (xnWaterMeter.InnerText == id.ToString())
                            {
                                ViewData["txtWaterMeterId"] = xnWaterMeter.InnerText;
                                ViewData["txtMeterNumber"]  = xnWaterMeter.NextSibling.InnerText;
                                ViewData["txtMake"]         = xnWaterMeter.NextSibling.NextSibling.InnerText;
                                ViewData["txtModel"]        = xnWaterMeter.NextSibling.NextSibling.NextSibling.InnerText;
                                ViewData["txtMeterSize"]    = xnWaterMeter.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                            }
                        }
                    }
                }
    
                return View();
            }
            
            . . .
        }
    }
  2. In the WaterMetersController class, right-click any section of the Details() method and click Add View...
  3. In the Add New Scaffolded Item dialog box, make sure MVC 5 View is selected.
    Click Add
  4. In the Add View dialog box, make sure the View Name text box is displaying Details.
    Click Add
  5. Change the code as follows:
    @{
        ViewBag.Title = "Water Meter Details";
    }
    
    <h2 class="text-center fw-bold common-font">Water Meter Details</h2>
    
    <hr />
    
    <div class="encloser common-font">
        <div class="row mb-2">
            @Html.Label("txtWaterMeterId", "Meter Meter Id:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
            <div class="col-md-8">
                @Html.TextBox("txtWaterMeterId", ViewBag.WaterMeterId as string,
                              htmlAttributes: new { @class = "form-control", disabled = "disabled" })
            </div>
        </div>
        <div class="row mb-2">
            @Html.Label("txtMeterNumber", "Meter #:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
            <div class="col-md-8">
                @Html.TextBox("txtMeterNumber", ViewBag.MeterNumber as string,
                              htmlAttributes: new { @class = "form-control", disabled = "disabled" })
            </div>
        </div>
        <div class="row mb-2">
            @Html.Label("txtMake", "Make:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
            <div class="col-md-8">
                @Html.TextBox("txtMake", ViewBag.Make as string,
                              htmlAttributes: new { @class = "form-control", disabled = "disabled" })
            </div>
        </div>
        <div class="row mb-2">
            @Html.Label("txtModel", "Model:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
            <div class="col-md-8">
                @Html.TextBox("txtModel", ViewBag.Model as string,
                              htmlAttributes: new { @class = "form-control", disabled = "disabled" })
            </div>
        </div>
        <div class="row mb-2">
            @Html.Label("txtMeterSize", "Meter Size:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
            <div class="col-md-8">
                @Html.TextBox("txtMeterSize", ViewBag.MeterSize as string,
                              htmlAttributes: new { @class = "form-control", disabled = "disabled" })
            </div>
        </div>
    </div>
    
    <hr />
    
    <p class="text-center">
        @Html.ActionLink("Create Water Meter", "Create", new { area = "" }, new { @class = "stellar" }) ::
        @Html.ActionLink("Edit Water Meter",   "Edit",   new { area = "" }, new { @class = "stellar" }) ::
        @Html.ActionLink("Delete Water Meter", "Delete", new { area = "" }, new { @class = "stellar" })
    </p>
  6. Click the WaterMetersController.cs tab to access the controller
  7. To execute the application, on the main menu, click Debug -> Start Without Debugging
  8. Click the Water Meters link

    Stellar Water Point

  9. In the list of water meters, click the Review link that corresponds to the second record (Water Meter Id = 2):

    Stellar Water Point

  10. Return to your programming environment

Updating a Water Meter

.

Practical LearningPractical Learning: Updating a Water Meter

  1. In the WaterMetersController class, change the Edit() methods as follows:
    using System.IO;
    using System.Xml;
    using System.Web.Mvc;
    
    namespace StellarWaterPoint1.Controllers
    {
        public class WaterMetersController : Controller
        {
            . . .
    
            // GET: WaterMeters/Edit/5
            public ActionResult Edit(int id)
            {
                XmlDocument xdWaterMeters = new XmlDocument();
                string strFileWaterMeters = @&quot;C:\Stellar Water Point4\WaterMeters.xml&quot;;
    
                if (System.IO.File.Exists(strFileWaterMeters))
                {
                    using(FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Open,
                                                                    FileAccess.Read, FileShare.Read))
                    {
                        xdWaterMeters.Load(fsWaterMeters);
    
                        XmlNodeList xnlWaterMeters = xdWaterMeters.GetElementsByTagName(&quot;water-meter-id&quot;);
                        
                        foreach (XmlNode xnWaterMeter in xnlWaterMeters)
                        {  
                            if (xnWaterMeter.InnerText == id.ToString())
                            {    
                                ViewData[&quot;txtWaterMeterId&quot;] = xnWaterMeter.InnerText;
                                ViewData[&quot;txtMeterNumber&quot;]  = xnWaterMeter.NextSibling.InnerText;
                                ViewData[&quot;txtMake&quot;]         = xnWaterMeter.NextSibling.NextSibling.InnerText;
                                ViewData[&quot;txtModel&quot;]        = xnWaterMeter.NextSibling.NextSibling.NextSibling.InnerText;
                                ViewData[&quot;txtMeterSize&quot;]    = xnWaterMeter.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                            }
                        }
                    }
                }
    
                return View();
            }
    
            // POST: WaterMeters/Edit/5
            [HttpPost]
            public ActionResult Edit(int id, FormCollection collection)
            {
                try
                {
                    // TODO: Add update logic here
                    XmlDocument xdWaterMeters = new XmlDocument();
                    string strFileWaterMeters = @&quot;C:\Stellar Water Point4\WaterMeters.xml&quot;;
    
                    if (System.IO.File.Exists(strFileWaterMeters))
                    {
                        using (FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Open,
                                                                         FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            xdWaterMeters.Load(fsWaterMeters);
                        }
    
                        XmlNodeList xnlWaterMeters = xdWaterMeters.GetElementsByTagName(&quot;water-meter-id&quot;);
    
                        using (FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Truncate,
                                                                         FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            foreach (XmlNode xnWaterMeter in xnlWaterMeters)
                            {
                                if (xnWaterMeter.InnerText == id.ToString())
                                {
                                    xnWaterMeter.ParentNode.InnerXml = &quot;&lt;water-meter-id&gt;&quot; + id + &quot;&lt;/water-meter-id&gt;&quot;     +
                                                                       &quot;&lt;meter-number&gt;&quot;   + collection[&quot;txtMeterNumber&quot;] + &quot;&lt;/meter-number&gt;&quot; +
                                                                       &quot;&lt;make&gt;&quot;           + collection[&quot;txtMake&quot;]        + &quot;&lt;/make&gt;&quot;         +
                                                                       &quot;&lt;model&gt;&quot;          + collection[&quot;txtModel&quot;]       + &quot;&lt;/model&gt;&quot;        +
                                                                       &quot;&lt;meter-size&gt;&quot;     + collection[&quot;txtMeterSize&quot;]   + &quot;&lt;/meter-size&gt;&quot;;
                                    xdWaterMeters.Save(fsWaterMeters);
                                    break;
                                }
                            }
                        }
                    }
    
                    return RedirectToAction(&quot;Index&quot;);
                }
                catch
                {
                    return View();
                }
            }
    
            . . .
        }
    }
  2. In the WaterMetersController class, right-click one of the Edit() methods and click Add View...
  3. In the Add New Scaffolded Item dialog box, make sure MVC 5 View is selected.
    Click Add
  4. In the Add View dialog box, make sure the View Name text box is displaying Edit.
    Click Add
  5. Change the document as follows:
    @{
        ViewBag.Title = "Edit Water Meter";
    }
    
    <h2 class="fw-bold common-font text-center">Edit/Update Water Meter</h2>
    
    <hr />
    
    @using (Html.BeginForm())
    {
        <div class="common-font encloser">
            <div class="row mb-2">
                @Html.Label("txtWaterMeterId", "Meter Meter Id:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
                <div class="col-md-8">
                    @Html.TextBox("txtWaterMeterId", null, htmlAttributes: new { @class = "form-control", id = "wtrMdlId" })
                </div>
            </div>
            <div class="row mb-2">
                @Html.Label("txtMeterNumber", "Meter #:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
                <div class="col-md-8">
                    @Html.TextBox("txtMeterNumber", ViewBag.MeterNumber as string,
                                  htmlAttributes: new { @class = "form-control", id = "mtrNbr" })
                </div>
            </div>
    
            <div class="row mb-2">
                @Html.Label("txtMake", "Make:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
                <div class="col-md-8">
                    @Html.TextBox("txtMake", ViewBag.Make as string,
                                  htmlAttributes: new { @class = "form-control", id = "make" })
                </div>
            </div>
    
            <div class="row mb-2">
                @Html.Label("txtModel", "Model:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
                <div class="col-md-8">
                    @Html.TextBox("txtModel", ViewBag.Model as string, htmlAttributes: new { @class = "form-control", id = "model" })
                </div>
            </div>
    
            <div class="row mb-2">
                @Html.Label("txtMeterSize", "Meter Size:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
                <div class="col-md-8">
                    @Html.TextBox("txtMeterSize", ViewBag.MeterSize as string, htmlAttributes: new { @class = "form-control", id = "mtrSize" })
                </div>
            </div>
    
            <div class="row mb-2">
                <label class="control-label col-md-5">
                    @Html.ActionLink("Water Meters", "Index", null, htmlAttributes: new { @class = "stellar" })
                </label>
                <div class="col-md-7">
                    <input type="submit" value="Update Water Meter" class="btn btn-stellar" />
                </div>
            </div>
        </div>
    }
  6. Click the WaterMetersController.cs tab
  7. To execute the application, on the main menu, click Debug -> Start Without Debugging
  8. Click the Water Meters link

    Stellar Water Point

  9. In the list of water meters, click the Edit link that corresponds to the first record (Water Meter Id = 1):

    Stellar Water Point

  10. Change the following values:
    Meter #: 392-494-572
    Make:    Constance Technologies
  11. Click Update Water Meter
  12. In the list of water meters, click the Edit link that corresponds to the second record (Water Meter Id = 2):

    Stellar Water Point

  13. Change the following values:
    Meter #: 938-725-869
    Make:   Stanford Trend
    Meter Size: 1 1/2 Inches
  14. Return to your programming environment

Deleting a Water Meter

.

Practical LearningPractical Learning: Removing a Water Meter

  1. In the WaterMetersController class, change the Delete() methods as follows:
    using System.IO;
    using System.Xml;
    using System.Web.Mvc;
    w
    namespace StellarWaterPoint1.Controllers
    {
        public class WaterMetersController : Controller
        {
            // GET: WaterMeters
            public ActionResult Index()
            {
                // Create a reference to the XML's DOM object
                XmlDocument xdWaterMeters = new XmlDocument();
                // Indicate the file that holds a list of water meters
                string strFileWaterMeters = Server.MapPath("/WaterDistribution/WaterMeters.xml");
    
                // Check whether a file that holds a list of water meters exists
                if (System.IO.File.Exists(strFileWaterMeters))
                {
                    // If that file exists, open it
                    using (FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Open,
                                                                     FileAccess.Read, FileShare.Read))
                    {
                        /* Get the list of water meters from the file and store
                         * the records in the previously created DOM object. */
                        xdWaterMeters.Load(fsWaterMeters);
                    }
    
                    // Check if there is at least one record in the file of water meters
                    if (xdWaterMeters.DocumentElement.ChildNodes.Count > 0)
                    {
                        /* If there is at least one record for the water meters, 
                         * create an XmlNodeList list and transmit it to 
                         * a ViewData collection that wille be used in the view page.*/
                        ViewData["WaterMeters"] = xdWaterMeters.DocumentElement.ChildNodes;
                    }
                    else
                    {
                        /* If there is no file or record for water meters,
                         * transmit a null list to the ViewData object.*/
                        ViewData["WaterMeters"] = null;
                    }
                }
    
                return View();
            }
    
            // GET: WaterMeters/Details/5
            public ActionResult Details(int id)
            {
                // Create a reference to the XML's DOM object
                XmlDocument xdWaterMeters = new XmlDocument();
                // Indicate the file that holds a list of water meters
                string strFileWaterMeters = Server.MapPath("/WaterDistribution/WaterMeters.xml");
    
                // If there a file that holds a list of water meters
                if (System.IO.File.Exists(strFileWaterMeters))
                {
                    // If there is such a file, open it to get its records
                    using (FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Open,
                                                                     FileAccess.Read, FileShare.Read))
                    {
                        // Get the records and load them in the created XML's DOM object
                        xdWaterMeters.Load(fsWaterMeters);
    
                        /* Using the water-meter-id element from the list of water meters, 
                         * create a collection of nodes and store it in an XmlNodeList variable. */
                        XmlNodeList xnlWaterMeters = xdWaterMeters.GetElementsByTagName("water-meter-id");
    
                        // Check each record in the above XmlNodeList collection
                        foreach (XmlNode xnWaterMeter in xnlWaterMeters)
                        {
                            /* Find out if there is a record whose water-meter-id is the 
                             * same as the number (id) that came from the form. */
                            if (xnWaterMeter.InnerText == id.ToString())
                            {
                                /* If you find such a record, get the value of each element 
                                 * and transmit it to a corresponding ViewData object. */
                                ViewData["txtWaterMeterId"] = xnWaterMeter.InnerText;
                                ViewData["txtMeterNumber"]  = xnWaterMeter.NextSibling.InnerText;
                                ViewData["txtMake"]         = xnWaterMeter.NextSibling.NextSibling.InnerText;
                                ViewData["txtModel"]        = xnWaterMeter.NextSibling.NextSibling.NextSibling.InnerText;
                                ViewData["txtMeterSize"]    = xnWaterMeter.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                            }
                        }
                    }
                }
    
                return View();
            }
    
            // GET: WaterMeters/Create
            public ActionResult Create()
            {
                return View();
            }
    
            // POST: WaterMeters/Create
            [HttpPost]
            public ActionResult Create(FormCollection collection)
            {
                try
                {
                    // TODO: Add insert logic here
                    // Create a reference to the XML's DOM
                    XmlDocument xdWaterMeters = new XmlDocument();
                    // Specify the file that would contain a list of water meters
                    string strFileWaterMeters = Server.MapPath("/WaterDistribution/WaterMeters.xml");
    
                    /* When the user clicks the Submit button on the Create webpage, check the
                     * content of the Meter Number text box.
                     * If the user typed a value in the Meter Number text box, ... */
                    if (!string.IsNullOrEmpty(collection["txtMeterNumber"]))
                    {
                        // If an XML file for water meters was created already, ...
                        if (System.IO.File.Exists(strFileWaterMeters))
                        {
                            // ... open it ...
                            using(FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.OpenOrCreate,
                                                                            FileAccess.ReadWrite, FileShare.ReadWrite))
                            {
                                // ... and put the records in the DOM
                                xdWaterMeters.Load(fsWaterMeters);
                            }
                        }
                        else
                        {
                            // If there is no XML file yet, create skeleton code for an XML document, ...
                            xdWaterMeters.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                                  "<water-meters></water-meters>");
                        }
                    }
    
                    // Get ready to create an XML element named water-meter
                    XmlElement xeWaterMeter = xdWaterMeters.CreateElement("water-meter");
    
                    // Create the markup of the XML water meter
                    string strWaterMeter = "<water-meter-id>" + collection["txtWaterMeterId"] + "</water-meter-id>" + 
                                           "<meter-number>"   + collection["txtMeterNumber"]  + "</meter-number>"   +
                                           "<make>"           + collection["txtMake"]         + "</make>"           +
                                           "<model>"          + collection["txtModel"]        + "</model>"          +
                                           "<meter-size>"     + collection["txtMeterSize"]    + "</meter-size>";
    
                    // Specify the markup of the new element
                    xeWaterMeter.InnerXml = strWaterMeter;
                    // Add the new node to the root
                    xdWaterMeters.DocumentElement.AppendChild(xeWaterMeter);
    
                    // Save the (new version of the) XML file
                    using(FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Create,
                                                                    FileAccess.Write, FileShare.Write))
                    {
                        xdWaterMeters.Save(fsWaterMeters);
                    }
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
    
            // GET: WaterMeters/Edit/5
            public ActionResult Edit(int id)
            {
                // Create an XML DOM object
                XmlDocument xdWaterMeters = new XmlDocument();
                // Declare a string variable for the file that holds the records of water meters
                string strFileWaterMeters = Server.MapPath("/WaterDistribution/WaterMeters.xml");
    
                // Check the existence of the file that holds the records of water meters
                if (System.IO.File.Exists(strFileWaterMeters))
                {
                    // If that file exists, open it and store it in a FileStream object
                    using(FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Open,
                                                                    FileAccess.Read, FileShare.Read))
                    {
                        // Put the water meters records in the previously created DOM object
                        xdWaterMeters.Load(fsWaterMeters);
    
                        /* Declare an XmlNodeList.
                         * Create a list of water meter records, using the water-meter-id element as reference. */
                        XmlNodeList xnlWaterMeters = xdWaterMeters.GetElementsByTagName("water-meter-id");
                        
                        // Once a list of water meters has been created, check each of its records
                        foreach (XmlNode xnWaterMeter in xnlWaterMeters)
                        {
                            // Look for a record whose water-meter-id value is the same as the number that came from the Web form   
                            if (xnWaterMeter.InnerText == id.ToString())
                            {
                                /* If you find such an element, get the value of each of its siblings 
                                 * and pass it to a text box that corresponds to the XML node.*/    
                                ViewData["txtWaterMeterId"] = xnWaterMeter.InnerText;
                                ViewData["txtMeterNumber"]  = xnWaterMeter.NextSibling.InnerText;
                                ViewData["txtMake"]         = xnWaterMeter.NextSibling.NextSibling.InnerText;
                                ViewData["txtModel"]        = xnWaterMeter.NextSibling.NextSibling.NextSibling.InnerText;
                                ViewData["txtMeterSize"]    = xnWaterMeter.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                            }
                        }
                    }
                }
    
                return View();
            }
    
            // POST: WaterMeters/Edit/5
            [HttpPost]
            public ActionResult Edit(int id, FormCollection collection)
            {
                try
                {
                    // TODO: Add update logic here
                    // Create an XML DOM object
                    XmlDocument xdWaterMeters = new XmlDocument();
                    // Get the file that holds the records of water meters and store its path in a string variable
                    string strFileWaterMeters = Server.MapPath("/WaterDistribution/WaterMeters.xml");
    
                    // Check the file that holds the records of water meters
                    if (System.IO.File.Exists(strFileWaterMeters))
                    {
                        // If that file exists, open it and store it in a FileStream object
                        using (FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Open,
                                                                         FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            // Store the records of water meters in the existing XML DOM object
                            xdWaterMeters.Load(fsWaterMeters);
                        }
    
                        // Create an XmlNodeList that will hold a list of water meters (from the file)
                        XmlNodeList xnlWaterMeters = xdWaterMeters.GetElementsByTagName("water-meter-id");
    
                        // Create a FileStream from the previously mentioned file of water meters
                        using (FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Truncate,
                                                                         FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            // Check each water meter record
                            foreach (XmlNode xnWaterMeter in xnlWaterMeters)
                            {
                                /* If you find a record whose water-meter-id has the same value 
                                 * as the value from the Water Meter Id from the Web form, ... */
                                if (xnWaterMeter.InnerText == id.ToString())
                                {
                                    // Change each value of the elements of the current node
                                    xnWaterMeter.ParentNode.InnerXml = "<water-meter-id>" + id + "</water-meter-id>"     +
                                                                       "<meter-number>"   + collection["txtMeterNumber"] + "</meter-number>" +
                                                                       "<make>"           + collection["txtMake"]        + "</make>"         +
                                                                       "<model>"          + collection["txtModel"]       + "</model>"        +
                                                                       "<meter-size>"     + collection["txtMeterSize"]   + "</meter-size>";
                                    // Save the updated file
                                    xdWaterMeters.Save(fsWaterMeters);
                                    // Stop the operation (because it has been completed).
                                    break;
                                }
                            }
                        }
                    }
    
                    // After this operation, go to the home page of the water meters
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
    
            // GET: WaterMeters/Delete/5
            public ActionResult Delete(int id)
            {
                // Create an XML DOM
                XmlDocument xdWaterMeters = new XmlDocument();
                // Get the path to the file that has the records of water meters
                string strFileWaterMeters = Server.MapPath("/WaterDistribution/WaterMeters.xml");
    
                // Check if that file exists
                if (System.IO.File.Exists(strFileWaterMeters))
                {
                    // If that file exists, open it and pass it to a FileStream object
                    using (FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Open,
                                                                     FileAccess.Read, FileShare.Read))
                    {
                        // Get the records from the DOM object and store them into the DOM object
                        xdWaterMeters.Load(fsWaterMeters);
    
                        // Get the collection of water meter objects
                        XmlNodeList xnlWaterMeters = xdWaterMeters.GetElementsByTagName("water-meter-id");
    
                        // Scan the collection of water meters
                        foreach (XmlNode xnWaterMeter in xnlWaterMeters)
                        {
                            /* Try to match a record whose Id is the same as the Id 
                             * of the record the user had clicked on the form*/
                            if (xnWaterMeter.InnerText == id.ToString())
                            {
                                /* If you find such a record, get the values of that record 
                                 * and pass them to the text boxes on the Web form.*/
                                ViewData["txtWaterMeterId"] = xnWaterMeter.InnerText;
                                ViewData["txtMeterNumber"] = xnWaterMeter.NextSibling.InnerText;
                                ViewData["txtMake"] = xnWaterMeter.NextSibling.NextSibling.InnerText;
                                ViewData["txtModel"] = xnWaterMeter.NextSibling.NextSibling.NextSibling.InnerText;
                                ViewData["txtMeterSize"] = xnWaterMeter.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                            }
                        }
                    }
                }
    
                return View();
            }
    
            // POST: WaterMeters/Delete/5
            [HttpPost]
            public ActionResult Delete(int id, FormCollection collection)
            {
                try
                {
                    // TODO: Add delete logic here
                    XmlDocument xdWaterMeters = new XmlDocument();
                    string strFileWaterMeters = Server.MapPath("/WaterDistribution/WaterMeters.xml");
    
                    // Make sure an XML file for the water meters was previously created
                    if (System.IO.File.Exists(strFileWaterMeters))
                    {
                        // If such a file exists, open it and store it in a FileStream object
                        using (FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Open,
                                                                         FileAccess.Read, FileShare.Read))
                        {
                            // Get the records and store them in an XML DOM object
                            xdWaterMeters.Load(fsWaterMeters);
                        }
    
                        // Get ready to change something on the file
                        using (FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Truncate,
                                                                         FileAccess.Write, FileShare.Write))
                        {
                            // Get a collection of water meter nodes
                            XmlNodeList xnlWaterMeters = xdWaterMeters.GetElementsByTagName("water-meter-id");
    
                            // Check each node
                            foreach (XmlNode xnWaterMeter in xnlWaterMeters)
                            {
                                /* If you find a water meter record whose water-meter-id is 
                                 * the same as the id of the record the user clicked, ... */
                                if (xnWaterMeter.InnerText == id.ToString())
                                {
                                    // ... ask its parent to delete that record
                                    xdWaterMeters.DocumentElement.RemoveChild(xnWaterMeter.ParentNode);
                                    // Now that the record has been deleted, save the XML file
                                    xdWaterMeters.Save(fsWaterMeters);
                                    // And stop checking the records
                                    break;
                                }
                            }
                        }
                    }
    
                    // Return to the home page of the water meters
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
        }
    }
  2. In the WaterMetersController.cs class, right-click one of the Delete() methods and click Add View...
  3. In the Add New Scaffolded Item dialog box, make sure MVC 5 View is selected.
    Click Add
  4. In the Add View dialog box, make sure the View Name is displaying Delete.
    Click Add
  5. Change the document as follows:
    @{
        ViewBag.Title = "Delete Water Meter";
    }
    
    <h2 class="fw-bold common-font text-center">Delete Water Meter</h2>
    
    <hr />
    
    @using (Html.BeginForm())
    {
        <div class="encloser common-font">
            <div class="row mb-2">
                @Html.Label("txtWaterMeterId", "Meter Meter Id:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
                <div class="col-md-8">
                    @Html.TextBox("txtWaterMeterId", ViewBag.WaterMeterId as string,
                                  htmlAttributes: new { @class = "form-control", disabled = "disabled" })
                </div>
            </div>
            <div class="row mb-2">
                @Html.Label("txtMeterNumber", "Meter #:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
                <div class="col-md-8">
                    @Html.TextBox("txtMeterNumber", ViewBag.MeterNumber as string,
                                  htmlAttributes: new { @class = "form-control", disabled = "disabled" })
                </div>
            </div>
            <div class="row mb-2">
                @Html.Label("txtMake", "Make:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
                <div class="col-md-8">
                    @Html.TextBox("txtMake", ViewBag.Make as string,
                                  htmlAttributes: new { @class = "form-control", disabled = "disabled" })
                </div>
            </div>
            <div class="row mb-2">
                @Html.Label("txtModel", "Model:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
                <div class="col-md-8">
                    @Html.TextBox("txtModel", ViewBag.Model as string,
                                  htmlAttributes: new { @class = "form-control", disabled = "disabled" })
                </div>
            </div>
            <div class="row mb-2">
                @Html.Label("txtMeterSize", "Meter Size:", htmlAttributes: new { @class = "col-form-label col-md-4 fw-bold" })
                <div class="col-md-8">
                    @Html.TextBox("txtMeterSize", ViewBag.MeterSize as string,
                                  htmlAttributes: new { @class = "form-control", disabled = "disabled" })
                </div>
            </div>
        </div>
    
        <hr />
    
        <div class="encloser">
            <h3 class="common-font fw-bold text-center">Are you sure you want to remove this water meter from the system?</h3>
            <div class="row mb-2">
                <div class="col-md-4">
                    @Html.ActionLink("Water Meters", "Index", null, new { @class = "stellar" }) ::
                </div>
                <div class="col-md-8">
                    <input type="submit" value="Delete this Water Meter" class="btn btn-stellar" />
                </div>
            </div>
        </div>
    }
    
    <hr />
    
    <p class="text-center">
        @Html.ActionLink("Create Water Meter", "Create", new { area = "" }, new { @class = "stellar" }) ::
        @Html.ActionLink("Edit Water Meter",   "Edit",   new { area = "" }, new { @class = "stellar" }) ::
        @Html.ActionLink("Delete Water Meter", "Delete", new { area = "" }, new { @class = "stellar" })
    </p>
  6. Click the WaterMetersController.cs tab
  7. To execute the application, on the main menu, click Debug -> Start Without Debugging
  8. Click the Water Meters link

    Stellar Water Point

  9. In the list of water meters, click the Delete link that corresponds to the third record (Water Meter Id = 3):

    Stellar Water Point

  10. Click the Delete this Water Meter button
  11. From the Windows Explorer, open the WaterMeters.xml file
  12. Replace the content of the file with the provided file
  13. Save the file
  14. Return to your programming environment

Customers

Introduction

Customers are people and companies that consume a resource such as water. In this section, we will create object that can assist the employees in creating and managing customers accounts.

Practical LearningPractical Learning: Introducing Customers

  1. In the Solution Explorer, right-click Controllers -> Add -> New Scaffolded Item...
  2. In the left list of the Add New Scaffolded Item dialog box, click MVC and, in the middle list, click MVC 5 Controller With Read/Write Actions

    Add New Scaffolded Item

  3. Click Add
  4. In the Add Controller dialog box, in the Controller Name text box, replace Default with Customers to get CustomersController
  5. Click Add

Creating a Customer Account

.

Practical LearningPractical Learning: Creating a Customer Account

  1. In the CustomersController class, change the Create() methods as follows:
    using System.Xml;
    using System.Web.Mvc;
    
    namespace StellarWaterPoint1.Controllers
    {
        public class CustomersController : Controller
        {        // GET: Customers/Create
            public ActionResult Create()
            {
                return View();
            }
    
            // POST: Customers/Create
            [HttpPost]
            public ActionResult Create(FormCollection collection)
            {
                try
                {
                    // TODO: Add insert logic here
                    int account_id = -1;
                    bool meterNumberIsValid = false;
                    XmlDocument xdWaterMeters = new XmlDocument();
                    XmlDocument xdCustomersAccounts = new XmlDocument();
                    string strFileCustomers = Server.MapPath("/WaterDistribution/Customers.xml");
                    string strFileWaterMeters = Server.MapPath("/WaterDistribution/WaterMeters.xml");
    
                    // Make sure the user provides an account number, ...
                    if (!string.IsNullOrEmpty(collection["AccountNumber"]))
                    {
                        // If the user provided an account number, to start, find out if a file for water meters was created already.
                        if (System.IO.File.Exists(strFileWaterMeters))
                        {
                            // If a file for water meters exists, open it
                            using (FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Open,
                                                                                                 FileAccess.Read,
                                                                                                 FileShare.Read))
                            {
                                // Store the list of water meters in an XML document
                                xdWaterMeters.Load(fsWaterMeters);
    
                                // Create a list of child nodes of the root node
                                XmlNodeList xnlWaterMeters = xdWaterMeters.DocumentElement.ChildNodes;
    
                                // Visit each node of the list of elements
                                foreach (XmlNode xnWaterMeter in xnlWaterMeters)
                                {
                                    // When you get to a list of (child) nodes of a water-meter node, visit each child node
                                    foreach (XmlNode xnMeterNumber in xnWaterMeter.ChildNodes)
                                    {
                                        // If you find a meter number that is the same as the meter number from the form, ...
                                        if (xnMeterNumber.InnerText == collection["MeterNumber"])
                                        {
                                            // ... make a note
                                            meterNumberIsValid = true;
                                        }
                                    }
                                }
                            }
                        }
    
                        // If either the user didn't provide a meter number or provided a meter number that doesn't exist, ...
                        if (meterNumberIsValid == false)
                        {
                            // ... create a message that will display to the user
                            ViewBag.ErrorMessage = "You must provide a valid meter number";
                        }
                        else
                        {
                            // It appears that the user provided both an account number and a valid meter number.
    
                            // If an XML file for customers accounts was previously created, ...
                            if (System.IO.File.Exists(strFileCustomers))
                            {
                                // ... open it ...
                                using (FileStream fsCustomers = new FileStream(strFileCustomers, FileMode.OpenOrCreate,
                                                                                                 FileAccess.ReadWrite,
                                                                                                 FileShare.ReadWrite))
                                {
                                    // ... and put the records in the DOM
                                    xdCustomersAccounts.Load(fsCustomers);
    
                                    XmlNodeList xnlCustomers = xdCustomersAccounts.GetElementsByTagName("account-id");
    
                                    foreach (XmlNode xnCustomer in xnlCustomers)
                                    {
                                        account_id = int.Parse(xnCustomer.InnerText);
                                    }
                                }
                            }
                            else
                            {
                                // If there is no XML file yet for the customers, create skeleton code for an XML document
                                xdCustomersAccounts.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                                            "<customers></customers>");
                                account_id = 0;
                            }
    
                            // Get ready to create an XML element named customer
                            XmlElement xeCustomer = xdCustomersAccounts.CreateElement("customer");
    
                            account_id++;
    
                            // Create the markup of the XML customer
                            string strCustomer = "<account-id>" + account_id + "</account-id>" +
                                                 "<account-number>" + collection["AccountNumber"] + "</account-number>" +
                                                 "<meter-number>" + collection["MeterNumber"] + "</meter-number>" +
                                                 "<first-name>" + collection["FirstName"] + "</first-name>" +
                                                 "<last-name>" + collection["LastName"] + "</last-name>" +
                                                 "<address>" + collection["Address"] + "</address>" +
                                                 "<city>" + collection["City"] + "</city>" +
                                                 "<county>" + collection["County"] + "</county>" +
                                                 "<state>" + collection["State"] + "</state>" +
                                                 "<zip-code>" + collection["ZIPCode"] + "</zip-code>";
    
                            // Specify the markup of the new element
                            xeCustomer.InnerXml = strCustomer;
    
                            // Add the new node to the root
                            xdCustomersAccounts.DocumentElement.AppendChild(xeCustomer);
    
                            // Save the (new version of the) XML file
                            using (FileStream fsCustomers = new FileStream(strFileCustomers, FileMode.Create, FileAccess.Write, FileShare.Write))
                            {
                                xdCustomersAccounts.Save(fsCustomers);
                            }
                        }
                    }
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
  2. Click the CustomersController.cs tab
  3. In the CustomersController class, right-click one of the Create() methods and click Add View...
  4. In the Add New Scaffolded Item dialog box, make sure MVC5 View is selected.
    Click Add
  5. In the Add View dialog box, make sure the View Name combo box is displaying Create.
    Click Add
  6. Change the document as follows
    @{
        ViewBag.Title = "New Customer Account";
    }
    
    <div class="push-down">
        <h2>New Customer Account</h2>
    </div>
    
    <hr />
    
    @using (Html.BeginForm())
    {
        <div class="form-horizontal">
            <div class="form-group">
                <label for="acntNbr" class="control-label col-md-4 caption">Account #:</label>
                <div class="col-md-8">
                    @Html.TextBox("AccountNumber", null, htmlAttributes: new { @class = "form-control", id = "acntNbr" })
                </div>
            </div>
    
            <div class="form-group">
                <label for="mtrNbr" class="control-label col-md-4 caption">Water Meter:</label>
    
                <div class="col-md-8">
                    @Html.TextBox("MeterNumber", null, htmlAttributes: new { @class = "form-control", id = "mtrNbr" })
                </div>
            </div>
    
            <div class="form-group">
                <label class="control-label col-md-4 caption">&nbsp;</label>
                <div class="col-md-8"><input type="text" id="meterDetails" class="form-control" disabled /></div>
            </div>
    
            <div class="form-group">
                <label for="fName" class="control-label col-md-4 caption">First Name:</label>
                <div class="col-md-8">
                    @Html.TextBox("FirstName", null, htmlAttributes: new { @class = "form-control", id = "fName" })
                </div>
            </div>
    
            <div class="form-group">
                <label for="lName" class="control-label col-md-4 caption">Last Name:</label>
                <div class="col-md-8">
                    @Html.TextBox("LastName", null, htmlAttributes: new { @class = "form-control", id = "lName" })
                </div>
            </div>
    
            <div class="form-group">
                <label for="adrs" class="control-label col-md-4 caption">Address:</label>
                <div class="col-md-8">
                    @Html.TextBox("Address", null, htmlAttributes: new { @class = "form-control", id = "adrs" })
                </div>
            </div>
    
            <div class="form-group">
                <label for="ct" class="control-label col-md-4 caption">City:</label>
                <div class="col-md-8">
                    @Html.TextBox("City", null, htmlAttributes: new { @class = "form-control", id = "ct" })
                </div>
            </div>
    
            <div class="form-group">
                <label for="county" class="control-label col-md-4 caption">County:</label>
                <div class="col-md-8">
                    @Html.TextBox("County", null, htmlAttributes: new { @class = "form-control", id = "county" })
                </div>
            </div>
    
            <div class="form-group">
                <label for="state" class="control-label col-md-4 caption">State:</label>
                <div class="col-md-8">
                    @Html.TextBox("State", null, htmlAttributes: new { @class = "form-control", id = "state" })
                </div>
            </div>
    
            <div class="form-group">
                <label for="zip" class="control-label col-md-4 caption">ZIP Code:</label>
    
                <div class="col-md-8">
                    @Html.TextBox("ZIPCode", null, htmlAttributes: new { @class = "form-control", id = "zip" })
                </div>
            </div>
    
            <div class="form-group">
                <label class="control-label col-md-5">
                    @Html.ActionLink("Customers", "Index", null, htmlAttributes: new { @class = "water-nav" })
                </label>
                <div class="col-md-7">
                    <input type="submit" value="Create Customer Account" class="btn btn-primary" />
                </div>
            </div>
        </div>
    }
    
    @Scripts.Render("~/bundles/jquery")
    
    <script type="text/javascript">
        $(document).ready(function () {
            $("#mtrNbr").blur(function (event) {
                var connection = {
                    url: "/WaterDistribution/WaterMeters.xml",
                    method: "GET",
                    dataType: "xml"
                };
                $.ajax(connection).
                    done(function (data) {
                        var waterMeters = $(data).find("water-meter");
                        waterMeters.each(function () {
                            if ($(this).find("meter-number").text() == $("#mtrNbr").val())
                                $("#meterDetails").val($(this).find("make").text() + " " + $(this).find("model").text() + " (" + $(this).find("meter-size").text() + ")");
                        });
                    });
            }); // Lost Focus Event
        }); // Document.Ready
    </script>
  7. Click the CustomersController.cs tab

Getting a List of Customers Records

.

Practical LearningPractical Learning: Getting a List of Customers Records

  1. In the CustomersController class, change the Index() method as follows:
            // GET: Customers
            public ActionResult Index()
            {
                // Get a reference to the XML DOM
                XmlDocument xdCustomers = new XmlDocument();
                // This is the name and path of the XML file that contains the customers records
                string strFileCustomers = Server.MapPath("/WaterDistribution/Customers.xml");
    
                // If a file that contains the customers records was previously created, ...
                if (System.IO.File.Exists(strFileCustomers))
                {
                    // ... open it
                    using (FileStream fsCustomers = new FileStream(strFileCustomers, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        // and store the records in the DOM
                        xdCustomers.Load(fsCustomers);
                    }
    
                    /* If the Customers records exist, send them to the view.
                     * If there is no file for the customers, indicate that the DOM is null. */
                    ViewBag.Customers = xdCustomers.DocumentElement.ChildNodes.Count > 0 ? xdCustomers.DocumentElement.ChildNodes : null;
                }
    
                return View();
            }
  2. In the CustomersController class, right-click any area of the Index() method and click Add View...
  3. In the Add New Scaffolded Item dialog box, make sure MVC5 View is selected.
    Click Add
  4. In the Add View dialog box, make sure the View Name combo box is displaying Index.
    Click Add
  5. Change the document as follows:
    @{
        ViewBag.Title = "Customers Accounts";
    }
    
    <div class="push-down">
        <h2>Customers Accounts</h2>
    </div>
    
    <hr />
    
    <table class="table table-striped common-font">
        <tr>
            <th class="fw-bold">Account ID</th>
            <th class="fw-bold">Account #</th>
            <th class="fw-bold">Meter #</th>
            <th class="fw-bold">First Name</th>
            <th class="fw-bold">Last Name</th>
            <th class="fw-bold">Address</th>
            <th class="fw-bold">City</th>
            <th class="fw-bold">County</th>
            <th class="fw-bold">State</th>
            <th class="fw-bold">ZIP Code</th>
            <th>@Html.ActionLink("New Customer Account", "Create", null, htmlAttributes: new { @class = "water-nav" })</th>
        </tr>
    
        @if (ViewBag.customers != null)
        {
            foreach (System.Xml.XmlNode client in ViewBag.Customers as System.Xml.XmlNodeList)
            {
                <tr>
                    <td class="text-center">@client.FirstChild.InnerText</td>
                    <td>@client.FirstChild.NextSibling.InnerText</td>
                    <td>@client.FirstChild.NextSibling.NextSibling.InnerText</td>
                    <td>@client.FirstChild.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@client.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@client.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@client.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@client.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@client.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@client.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>
                        @Html.ActionLink("Update", "Edit", new { id = @client.FirstChild.InnerText }) |
                        @Html.ActionLink("Review", "Details", new { id = @client.FirstChild.InnerText }) |
                        @Html.ActionLink("Remove", "Delete", new { id = @client.FirstChild.InnerText })
                    </td>
                </tr>
            }
        }
    </table>
  6. To execute the application, on the main menu, click Debug -> Start Without Debugging

    Switching a String

  7. Click the Customers link

    Water Distribution Company - Customers Accounts

  8. Click the New Customer Account link

    Water Distribution Company - New Customer Account

  9. In the text boxes, type the following values:
    Customer Id Account # Account Name Meter # Account Type Address City County State ZIP Code
    1 9279-777-8394 Thommy Melone 799-528-461 RES 10252 Broward Ave #D4 Frederick Frederick MD 21000-2020

    Water Distribution Company - Customers

  10. Click the Save Customer Account button
  11. Click the New Customer Account link
  12. In the text boxes, type the following values:
    Customer Id Account # Account Name Meter # Account Type Address City County State ZIP Code
    2 8613-494-5820 Wasson Books 682-537-380 RES 6248 Walling Str Hamonstown Atlentis NewJ 98037

    Water Distribution Company - Customers

  13. Click the Save Customer Account button

Getting a Details of a Customer Account

.

Practical LearningPractical Learning: Getting a Details of a Customer Account

  1. In the CustomersController class, change the Details() method as follows:
    
                        
  2. In the CustomersController class, right-click any section of the Details() method and click Add View...
  3. In the Add New Scaffolded Item dialog box, make sure MVC 5 View is selected.
    Click Add
  4. In the Add View dialog box, make sure the View Name text box is displaying Details.
    Click Add
  5. Change the document as follows:
  6. Click the CustomersController.cs tab to access the controller
  7. To execute the application, on the main menu, click Debug -> Start Without Debugging
  8. Click the Customers link
  9. In the list of customers, click the Review link that corresponds to the first record (Customer Id = 1):

    Stellar Water Point

  10. Click the Customers link
  11. In the list of customers, click the Review link that corresponds to the second record (Customer Id = 2):

    Stellar Water Point

  12. Return to your programming environment

Updating a Customer Account

.

Practical LearningPractical Learning: Updating a Customer Account

  1. In the CustomersController class, change the Edit() methods as follows:
  2. In the CustomersController class, right-click one of the Edit() methods and click Add View...
  3. In the Add New Scaffolded Item dialog box, make sure MVC 5 View is selected.
    Click Add
  4. In the Add View dialog box, make sure the View Name text box is displaying Edit.
    Click Add
  5. Change the document as follows:
  6. Click the CustomersController.cs tab
  7. Click the CustomersController.cs tab
  8. To execute the application, on the main menu, click Debug -> Start Without Debugging
  9. Click the Customers link
  10. In the list of customers, click the Edit link that corresponds to the first record (Customer Id = 1):

    Stellar Water Point

  11. Change the following values:
    Customer Id Account # Account Name Meter # Account Type Address City County State ZIP Code
    1 9279-570-8394 Thomas Stones 799-528-461 RES 10252 Broward Avenue, Suite #D4 Frederick Frederick MD 21703-4422

    Water Distribution Company - Customers

  12. Click Update Customer Account
  13. Click the Customers link
  14. In the list of customers, click the Edit link that corresponds to the first record (Customer Id = 1):

    Stellar Water Point

  15. Change the following values:
    Customer Id Account # Account Name Meter # Account Type Address City County State ZIP Code
    1 1386-949-2058 Watson Country Buffet 296-837-495 WAT 4862 Wellington Street Hammonton Atlantic NJ 08037-2828

    Water Distribution Company - Customers

  16. Click Update Customer Account
  17. Return to your programming environment

Deleting a Customer Account

.

Practical LearningPractical Learning: Deleting a Customer Account

  1. In the CustomersController class, change the Delete() methods as follows:
  2. In the CustomersController class, right-click any part of the Delete() methods and click Add View...
  3. In the Add New Scaffolded Item dialog box, make sure MVC 5 View is selected.
    Click Add
  4. In the Add View dialog box, make sure the View Name text box is displaying Delete.
    Click Add
  5. Change the document as follows:
  6. Click the CustomersController.cs tab
  7. To execute the application, on the main menu, click Debug -> Start Without Debugging
  8. Click the Customers link
  9. In the list of customers, click the Delete link that corresponds to the second record (Customer Id = 2):

    Stellar Water Point

  10. Click the Delete this Customer Account button
  11. From the Windows Explorer, open the WaterMeters.xml file
  12. Replace the content of the file with the provided file
  13. Save the file
  14. Return to your programming environment

Water Bills

Introduction

A water bill is a document that provides various pieces of information about a costumer consuming water and the amount to pay for it. In thissection, we will create objects that can assist the company in creating water bills.

Practical LearningPractical Learning: Introducing Water Bills

  1. In the Solution Explorer, right-click Controllers -> Add -> Controller...
  2. In the middle frame of the Add New Scaffolded Item dialog box, make sure MVC 5 Controller With Read/Write Actions is selected.
    Click Add
  3. In the Add Contoller dialog box, replace Default with WaterBills to get WaterBillsControllers
  4. Press Enter

Creating a Water Bill

.

Practical LearningPractical Learning: Creating a Water Bill

  1. In the WaterBillsControllers class, change the Create() methods as follows:
    // GET: WaterBills/Create
            public ActionResult Create()
            {
                int water_bill_id = 0;
                XmlDocument xdWaterBills = new XmlDocument();
                string strFileWaterBills = Server.MapPath("/WaterDistribution/WaterBills.xml");
    
                if (System.IO.File.Exists(strFileWaterBills))
                {
                    using (FileStream fsWaterBills = new FileStream(strFileWaterBills, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        xdWaterBills.Load(fsWaterBills);
    
                        XmlNodeList xnlWaterBills = xdWaterBills.GetElementsByTagName("water-bill-id");
    
                        foreach (XmlNode xnWaterBill in xnlWaterBills)
                        {
                            water_bill_id = int.Parse(xnWaterBill.InnerText);
                        }
                    }
                }
    
                ViewData["WaterBillID"] = (water_bill_id + 1);
    
                Random rndNumber = new Random();
                ViewData["InvoiceNumber"] = rndNumber.Next(100001, 999999).ToString();
    
                return View();
            }
    
            // POST: WaterBills/Create
            [HttpPost]
            public ActionResult Create(FormCollection collection)
            {
                try
                {
                    // TODO: Add insert logic here
                    int bill_id = -1;
                    bool customerIsValid = false;
                    string meterNumber = string.Empty;
                    XmlDocument xdCustomers = new XmlDocument();
                    XmlDocument xdWaterBills = new XmlDocument();
                    string strFileCustomers = Server.MapPath("/WaterDistribution/Customers.xml");
                    string strFileWaterBills = Server.MapPath("/WaterDistribution/WaterBills.xml");
    
                    // Make sure the user provided an account number for the customer, ...
                    if (!string.IsNullOrEmpty(collection["AccountNumber"]))
                    {
                        // If the user provided an account number, find out if an XML file for customers was already created.
                        if (System.IO.File.Exists(strFileCustomers))
                        {
                            // If a file for customers exists, open it
                            using (FileStream fsCustomers = new FileStream(strFileCustomers, FileMode.Open,
                                                                                             FileAccess.Read,
                                                                                             FileShare.Read))
                            {
                                // Store the list of customers in an XML document
                                xdCustomers.Load(fsCustomers);
    
                                // Create a list of customers nodes that use the account number provided by the user
                                XmlNodeList xnlCustomers = xdCustomers.GetElementsByTagName("account-number");
    
                                // Visit each node of the list of elements
                                foreach (XmlNode xnCustomer in xnlCustomers)
                                {
                                    // If you find a customer that is the same as the account number from the form, ...
                                    if (xnCustomer.InnerText == collection["AccountNumber"])
                                    {
                                        // ... make a note
                                        customerIsValid = true;
                                        // and get the meter number used by that customer
                                        meterNumber = xnCustomer.NextSibling.InnerText;
                                    }
                                }
                            }
                        }
                    }
    
                    if (customerIsValid == true)
                    {
                        // It appears that the user provided a valid customer account number.
                        // If an XML file for water bills was previously created, ...
                        if (System.IO.File.Exists(strFileWaterBills))
                        {
                            // ... open it ...
                            using (FileStream fsWaterBills = new FileStream(strFileWaterBills, FileMode.OpenOrCreate,
                                                                                               FileAccess.ReadWrite,
                                                                                               FileShare.ReadWrite))
                            {
                                // ... and put the records in the DOM
                                xdWaterBills.Load(fsWaterBills);
    
                                XmlNodeList xnlWaterBills = xdWaterBills.GetElementsByTagName("water-bill-id");
    
                                foreach (XmlNode xnWaterBill in xnlWaterBills)
                                {
                                    bill_id = int.Parse(xnWaterBill.InnerText);
                                }
                            }
                        }
                        else
                        {
                            // If there is no XML file yet for the customers, create skeleton code for an XML document
                            xdWaterBills.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                                 "<water-bills></water-bills>");
                            bill_id = 0;
                        }
    
                        // Get ready to create an XML element named water-bill
                        XmlElement xeWaterBill = xdWaterBills.CreateElement("invoice");
    
                        bill_id++;
    
                        // Create the markup of the XML water bill
                        xeWaterBill.InnerXml = "<water-bill-id>"            + bill_id + "</water-bill-id>"        +
                                               "<invoice-number>"           + collection["InvoiceNumber"]         + "</invoice-number>"           +
                                               "<account-number>"           + collection["AccountNumber"]         + "</account-number>"           +
                                               "<meter-reading-start-date>" + collection["MeterReadingStartDate"] + "</meter-reading-start-date>" +
                                               "<meter-reading-end-date>"   + collection["MeterReadingEndDate"]   + "</meter-reading-end-date>"   +
                                               "<billing-days>"             + collection["BillingDays"]           + "</billing-days>"             +
                                               "<counter-reading-start>"    + collection["CounterReadingStart"]   + "</counter-reading-start>"    +
                                               "<counter-reading-end>"      + collection["CounterReadingEnd"]     + "</counter-reading-end>"      +
                                               "<total-hcf>"                + collection["TotalHCF"]              + "</total-hcf>"                +
                                               "<total-gallons>"            + collection["TotalGallons"]          + "</total-gallons>"            +
                                               "<first-15-hcf>"             + collection["First15HCF"]            + "</first-15-hcf>"             +
                                               "<next-10-hcf>"              + collection["Next10HCF"]             + "</next-10-hcf>"              +
                                               "<remaining-hcf>"            + collection["RemainingHCF"]          + "</remaining-hcf>"            +
                                               "<sewer-charges>"            + collection["SewerCharges"]          + "</sewer-charges>"            +
                                               "<storm-charges>"            + collection["StormCharges"]          + "</storm-charges>"            + 
                                               "<water-usage-charges>"      + collection["WaterUsageCharges"]     + "</water-usage-charges>"      +
                                               "<total-charges>"            + collection["TotalCharges"]          + "</total-charges>"            +
                                               "<local-taxes>"              + collection["LocalTaxes"]            + "</local-taxes>"              +
                                               "<state-taxes>"              + collection["StateTaxes"]            + "</state-taxes>"              +
                                               "<payment-due-date>"         + collection["PaymentDueDate"]        + "</payment-due-date>"         +
                                               "<amount-due>"               + collection["AmountDue"]             + "</amount-due>"               +
                                               "<late-payment-due-date>"    + collection["LatePaymentDueDate"]    + "</late-payment-due-date>"    +
                                               "<late-amount-due>"          + collection["LateAmountDue"]         + "</late-amount-due>";
    
                        // Add the new node to the root
                        xdWaterBills.DocumentElement.AppendChild(xeWaterBill);
    
                        // Save the (new version of the) XML file
                        using (FileStream fsWaterBills = new FileStream(strFileWaterBills, FileMode.Create, FileAccess.Write, FileShare.Write))
                        {
                            xdWaterBills.Save(fsWaterBills);
                        }
    
                        // We also want to update the counter value on the water meter with the new Counter Reading End value
                        XmlDocument xdWaterMeters = new XmlDocument();
                        string strFileWaterMeters = Server.MapPath("/WaterDistribution/WaterMeters.xml");
    
                        if (System.IO.File.Exists(strFileWaterMeters))
                        {
                            using (FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                            {
                                xdWaterMeters.Load(fsWaterMeters);
                            }
    
                            XmlNodeList xnlWaterMeters = xdWaterMeters.GetElementsByTagName("meter-id");
    
                            using (FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite))
                            {
                                foreach (XmlNode xnWaterMeter in xnlWaterMeters)
                                {
                                    if (xnWaterMeter.NextSibling.InnerText == meterNumber)
                                    {
                                        xnWaterMeter.ParentNode.InnerXml = "<meter-id>"         + xnWaterMeter.InnerText + "</meter-id>" +
                                                                           "<meter-number>"     + meterNumber + "</meter-number>" +
                                                                           "<make>"             + xnWaterMeter.NextSibling.NextSibling.InnerText + "</make>" +
                                                                           "<model>"            + xnWaterMeter.NextSibling.NextSibling.NextSibling.InnerText + "</model>" +
                                                                           "<meter-size>"       + xnWaterMeter.NextSibling.NextSibling.NextSibling.NextSibling.InnerText + "</meter-size>" +
                                                                           "<date-last-update>" + xnWaterMeter.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText + "</date-last-update>" +
                                                                           "<counter-value>"    + collection["CounterReadingEnd"] + "</counter-value>";
                                        xdWaterMeters.Save(fsWaterMeters);
                                        break;
                                    }
                                }
                            }
                        }
                    }
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
  2. In the WaterBillsController class, right-click one of the Create() methods and click Add View...
  3. In the Add New Scaffolded Item dialog box, make sure MVC 5 View is selected.
    Click Add
  4. In the Add View dialog box, make sure the View Name text box is displaying Create.
    Click Add
  5. Change the document as follows:
    @{
        ViewBag.Title = "Prepare Customer Water Bill";
    }
    
    <div class="push-down">
        <h2>Prepare Customer Water Bill</h2>
    </div>
    
    <hr />
    
    @using (Html.BeginForm())
    {
        <div class="form-horizontal">
            <div class="form-group">
                <label for="billNbr" class="control-label col-md-4 caption">Water Bill ID:</label>
                <div class="col-md-8">
                    @Html.TextBox("WaterBillID", null,
                                  htmlAttributes: new { @class = "form-control", id = "billNbr", disabled = "disabled" })
                </div>
            </div>
            <div class="form-group">
                <label for="invoiceNbr" class="control-label col-md-4 caption">Invoice #:</label>
                <div class="col-md-8">
                    @Html.TextBox("InvoiceNumber", null,
                                  htmlAttributes: new { @class = "form-control", id = "invoiceNbr" })
                </div>
            </div>
            <div class="form-group">
                <label for="acntNbr" class="control-label col-md-4 caption">Customer Account #:</label>
                <div class="col-md-8">
                    @Html.TextBox("AccountNumber", null,
                                  htmlAttributes: new { @class = "form-control", id = "acntNbr" })
                </div>
            </div>
    
            <div class="form-group">
                <label class="control-label col-md-4 caption">Customer Name:</label>
                <div class="col-md-8">
                    @Html.TextBox("CustomerName", null,
                                  new { @class = "form-control", id = "custName", disabled = "disabled" })
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-4 caption">Customer Address:</label>
                <div class="col-md-8">
                    @Html.TextBox("CustomerAddress", null,
                                  new { @class = "form-control", id = "adrs", disabled = "disabled" })
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-4">&nbsp;</label>
                <div class="col-md-125">
                    @Html.TextBox("CustomerCity", null,
                                  new { @class = "form-control", id = "city", disabled = "disabled" })
                </div>
                <div class="col-md-125">
                    @Html.TextBox("CustomerCounty", null,
                                  new { @class = "form-control", id = "county", disabled = "disabled" })
                </div>
                <div class="col-md-125">
                    @Html.TextBox("CustomerState", null,
                                  new { @class = "form-control", id = "state", disabled = "disabled" })
                </div>
                <div class="col-md-125">
                    @Html.TextBox("CustomerZIPCode", null,
                                  new { @class = "form-control", id = "zip", disabled = "disabled" })
                </div>
            </div>
    
            <div class="form-group">
                <label class="control-label col-md-4 caption">Meter Details:</label>
                <div class="col-md-125">
                    @Html.TextBox("MeterNumber", null,
                                  new { @class = "form-control", id = "mtrNbr", disabled = "disabled" })
                </div>
                <div class="col-md-6">
                    @Html.TextBox("MeterDetails", null,
                                  htmlAttributes: new { @class = "form-control", id = "meterDetails", disabled = "disabled" })
                </div>
            </div>
            <hr />
            <div class="form-group">
                <label for="mrsd" class="control-label col-md-4 caption">Meter Reading Start Date:</label>
                <div class="col-md-2">
                    @Html.TextBox("MeterReadingStartDate", null,
                                  htmlAttributes: new { @class = "form-control", id = "mrsd" })
                </div>
                <label for="mred" class="control-label col-md-125 caption">Meter Reading End Date:</label>
                <div class="col-md-125">
                    @Html.TextBox("MeterReadingEndDate", null,
                                  htmlAttributes: new { @class = "form-control", type = "date", id = "mred" })
                </div>
                <label for="days" class="control-label col-md-2 caption">Billing Days:</label>
                <div class="col-md-1">
                    @Html.TextBox("BillingDays", null, htmlAttributes: new { @class = "form-control", id = "days" })
                </div>
            </div>
    
            <div class="form-group">
                <label for="crs" class="control-label col-md-4 caption">Counter Reading Start:</label>
                <div class="col-md-2">
                    @Html.TextBox("CounterReadingStart", null, htmlAttributes: new { @class = "form-control", id = "crs" })
                </div>
                <label for="cre" class="control-label col-md-125 caption">Current Meter Reading:</label>
                <div class="col-md-125">
                    @Html.TextBox("CounterReadingEnd", null, htmlAttributes: new { @class = "form-control", id = "cre" })
                </div>
                <label for="thcf" class="control-label col-md-2 caption">Total HCF:</label>
                <div class="col-md-1">
                    @Html.TextBox("TotalHCF", null, htmlAttributes: new { @class = "form-control", id = "thcf" })
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-4">&nbsp;</label>
                <div class="col-md-2">&nbsp;</div>
                <label class="control-label col-md-125">&nbsp;</label>
                <div class="col-md-125">&nbsp;</div>
                <label for="gallons" class="control-label col-md-2 caption">Total Gallons:</label>
                <div class="col-md-1">
                    @Html.TextBox("TotalGallons", null, htmlAttributes: new { @class = "form-control", id = "gallons" })
                </div>
            </div>
    
            <div class="form-group">
                <label for="f15HCF" class="control-label col-md-4 caption">1st 15 HCF at $3.6121:</label>
                <div class="col-md-2">
                    @Html.TextBox("First15HCF", null,
                                  htmlAttributes: new { @class = "form-control", id = "f15HCF" })
                </div>
                <label for="next10HCF" class="control-label col-md-125 caption">Next 10 HCF at $3.9180:</label>
                <div class="col-md-125">
                    @Html.TextBox("Next10HCF", null,
                                  htmlAttributes: new { @class = "form-control", id = "next10HCF" })
                </div>
                <label for="remHCF" class="control-label col-md-2 caption">Remaining HCF at $4.2763:</label>
                <div class="col-md-1">
                    @Html.TextBox("RemainingHCF", null, htmlAttributes: new { @class = "form-control", id = "remHCF" })
                </div>
            </div>
            <div class="form-group">
                <label for="sewerCharges" class="control-label col-md-4 caption">Sewer Charges:</label>
                <div class="col-md-2">
                    @Html.TextBox("SewerCharges", null,
                                  htmlAttributes: new { @class = "form-control", id = "sewerCharges" })
                </div>
                <label for="stormCharges" class="control-label col-md-125 caption">Storm Charges:</label>
                <div class="col-md-125">
                    @Html.TextBox("StormCharges", null,
                                  htmlAttributes: new { @class = "form-control", id = "stormCharges" })
                </div>
                <label for="wuc" class="control-label col-md-2 caption">Water Usage Charges:</label>
                <div class="col-md-1">
                    @Html.TextBox("WaterUsageCharges", null, htmlAttributes: new { @class = "form-control", id = "wuc" })
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-4">&nbsp;</label>
                <div class="col-md-2">&nbsp;</div>
                <label class="control-label col-md-125">&nbsp;</label>
                <div class="col-md-125">&nbsp;</div>
                <label for="totalCharges" class="control-label col-md-2 caption">Total Charges:</label>
                <div class="col-md-1">
                    @Html.TextBox("TotalCharges", null, htmlAttributes: new { @class = "form-control", id = "totalCharges" })
                </div>
            </div>
            <hr />
            <div class="form-group">
                <label class="control-label col-md-4">&nbsp;</label>
                <div class="col-md-125">&nbsp;</div>
                <label for="localTaxes" class="control-label col-md-125 caption">Local Taxes:</label>
                <div class="col-md-125">
                    @Html.TextBox("LocalTaxes", null,
                                  htmlAttributes: new { @class = "form-control", id = "localTaxes" })
                </div>
                <label for="stateTaxes" class="control-label col-md-125 caption">State Taxes:</label>
                <div class="col-md-125">
                    @Html.TextBox("StateTaxes", null, htmlAttributes: new { @class = "form-control", id = "stateTaxes" })
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-4">&nbsp;</label>
                <div class="col-md-125">&nbsp;</div>
                <label for="pdd" class="control-label col-md-125 caption">Payment Due Date:</label>
                <div class="col-md-125">
                    @Html.TextBox("PaymentDueDate", null,
                                  htmlAttributes: new { @class = "form-control", type = "date", id = "pdd" })
                </div>
                <label for="amtDue" class="control-label col-md-125 caption">Amount Due:</label>
                <div class="col-md-125">
                    @Html.TextBox("AmountDue", null,
                                  htmlAttributes: new { @class = "form-control", id = "amtDue" })
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-4 caption">&nbsp;</label>
                <div class="col-md-125">&nbsp;</div>
                <label for="lpdd" class="control-label col-md-125 caption">Late Payment Due Date:</label>
                <div class="col-md-125">
                    @Html.TextBox("LatePaymentDueDate", null,
                                  htmlAttributes: new { @class = "form-control", id = "lpdd" })
                </div>
                <label for="lateAmtDue" class="control-label col-md-125 caption">Late Amount Due:</label>
                <div class="col-md-125">
                    @Html.TextBox("LateAmountDue", null, htmlAttributes: new { @class = "form-control", id = "lateAmtDue" })
                </div>
            </div>
    
            <div class="form-group text-center">
                <label class="control-label col-md-5">
                    @Html.ActionLink("Water Bills", "Index", null, htmlAttributes: new { @class = "water-nav" })
                </label>
                <div class="col-md-7">
                    <input type="submit" value="Save Water Bill" class="btn btn-primary" />
                </div>
            </div>
        </div>
    }
    
    @Scripts.Render("~/bundles/jquery")
    
    <script type="text/javascript">
        $(document).ready(function () {
            $("#acntNbr").blur(function (event) {
                var connection = {
                    method: "GET",
                    dataType: "xml",
                    url: "/WaterDistribution/Customers.xml"
                };
                $.ajax(connection).
                    done(function (data) {
                        var clients = $(data).find("customer");
                        clients.each(function () {
                            if ($(this).find("account-number").text() === $("#acntNbr").val()) {
                                $("#mtrNbr").val($(this).find("meter-number").text());
                                $("#custName").val($(this).find("first-name").text() + " " + $(this).find("last-name").text());
                                $("#adrs").val($(this).find("address").text());
                                $("#city").val($(this).find("city").text());
                                $("#county").val($(this).find("county").text());
                                $("#state").val($(this).find("state").text());
                                $("#zip").val($(this).find("zip-code").text());
                            }
                        });
                    });
    
                connection = {
                    method: "GET",
                    dataType: "xml",
                    url: "/WaterDistribution/WaterMeters.xml"
                };
                $.ajax(connection).
                    done(function (data) {
                        var waterMeters = $(data).find("water-meter");
                        waterMeters.each(function () {
                            if ($(this).find("meter-number").text() === $("#mtrNbr").val()) {
                                $("#meterDetails").val($(this).find("make").text() + " " + $(this).find("model").text() + " (" + $(this).find("meter-size").text() + ")");
                                $("#mrsd").val($(this).find("date-last-update").text());
                                $("#crs").val($(this).find("counter-value").text());
                            }
                        });
                    });
    
                connection = {
                    method: "GET",
                    dataType: "xml",
                    url: "/WaterDistribution/WaterBills.xml"
                };
                $.ajax(connection).
                    done(function (data) {
                        var invoices = $(data).find("invoice");
                        invoices.each(function () {
                            if ($(this).find("account-number").text() === $("#acntNbr").val()) {
                                $("#mrsd").val($(this).find("meter-reading-end-date").text());
                                $("#crs").val($(this).find("counter-reading-end").text());
                            }
                        });
                    });
            }); // Account # Lost Focus
    
            $("#mred").change(function (event) {
                var meterReadingStartDate = Date.parse($("#mrsd").val());
                var meterReadingEndDate = Date.parse($("#mred").val());
    
                var days = (meterReadingEndDate - meterReadingStartDate) / 86400000;
    
                $("#days").val(days.toFixed());
            }); // Meter Reading End Date Lost Focus
    
            $("#cre").focusout(function (event) {
                var counterReadingStart = parseInt($("#crs").val());
                var counterReadingEnd = parseInt($("#cre").val());
                var totalHCF = counterReadingEnd - counterReadingStart;
                var gallons = totalHCF * 748; // 748.05
    
                var first15HCF = totalHCF * 3.612;
                var next10HCF = 0, remainingHCF = 0;
    
                if (totalHCF <= 15) {
                    first15HCF = totalHCF * 3.612;
                    next10HCF = 0;
                    remainingHCF = 0;
                }
                else if (totalHCF <= 25) {
                    first15HCF = 15 * 3.612;
                    next10HCF = (totalHCF - 15) * 3.918;
                    remainingHCF = 0;
                }
                else {
                    first15HCF = 15 * 3.612;
                    next10HCF = 10 * 3.918;
                    remainingHCF = (totalHCF - 25) * 2.2763;
                }
    
                var waterUsageCharges = first15HCF + next10HCF + remainingHCF;
                var sewerCharges      = waterUsageCharges * 0.252;
                var stormCharges      = waterUsageCharges * 0.0025;
                var totalCharges      = waterUsageCharges + sewerCharges + stormCharges;
                var localTaxes        = totalCharges * 0.0152;
                var stateTaxes        = totalCharges * 0.005;
                var amountDue         = totalCharges + localTaxes + stateTaxes;
    
                $("#thcf").val(totalHCF.toFixed());
                $("#gallons").val(gallons.toFixed());
                $("#amtDue").val(amountDue.toFixed(2));
                $("#f15HCF").val(first15HCF.toFixed(2));
                $("#next10HCF").val(next10HCF.toFixed(2));
                $("#remHCF").val(remainingHCF.toFixed(2));
                $("#wuc").val(waterUsageCharges.toFixed(2));
                $("#stateTaxes").val(stateTaxes.toFixed(2));
                $("#localTaxes").val(localTaxes.toFixed(2));
                $("#sewerCharges").val(sewerCharges.toFixed(2));
                $("#stormCharges").val(stormCharges.toFixed(2));
                $("#totalCharges").val(totalCharges.toFixed(2));
                $("#lateAmtDue").val((amountDue + 8.95).toFixed(2));
            }); // Counter Reading End Lost Focus
        }); // Document.Ready
    </script>
  6. Click the WaterBillsController.cs tab to return to the controller
  7. Click the CustomersController.cs tab
  8. In the CustomersController class, right-click one of the Create() methods and click Add View...
  9. In the Add New Scaffolded Item dialog box, make sure MVC5 View is selected.
    Click Add
  10. In the Add View dialog box, make sure the View Name combo box is displaying Create.
    Click Add

Reviewing a Water Bill

.

Practical LearningPractical Learning: Reviewing a Water Bill

  1. Click the WaterBillsController.cs tab
  2. 
    using System.Xml;
    using System.Web.Mvc;
    using System.Collections.Generic;
    
    namespace StellarWaterPoint1.Controllers
    {
        public class WaterBillsController : Controller
        {
            // GET: WaterBills
            public ActionResult Index()
            {
                XmlDocument xdWaterBills = new XmlDocument();
                string strFileWaterBills = Server.MapPath("/WaterDistribution/WaterBills.xml");
    
                if (System.IO.File.Exists(strFileWaterBills))
                {
                    using (FileStream fsWaterBills = new FileStream(strFileWaterBills, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        xdWaterBills.Load(fsWaterBills);
                    }
    
                    if (xdWaterBills.DocumentElement.ChildNodes.Count > 0)
                    {
                        ViewData["Invoices"] = xdWaterBills.DocumentElement.ChildNodes;
                    }
                    else
                    {
                         ViewData["Invoices"] = null;
                    }
                }
    
                return View();
            }
    
            // GET: WaterBills/Details/5
            public ActionResult Details(int id)
            {
                XmlDocument xdWaterBills = new XmlDocument();
                string strFileWaterBills = Server.MapPath("/WaterDistribution/WaterBills.xml");
    
                if (System.IO.File.Exists(strFileWaterBills))
                {
                    using (FileStream fsWaterBills = new FileStream(strFileWaterBills, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        xdWaterBills.Load(fsWaterBills);
    
                        XmlNodeList xnlWaterBills = xdWaterBills.GetElementsByTagName("water-bill-id");
    
                        foreach (XmlNode xnWaterBill in xnlWaterBills)
                        {
                            if (xnWaterBill.InnerText == id.ToString())
                            {
                                 ViewData["WaterBillID"]           = xnWaterBill.InnerText;
                                 ViewData["InvoiceNumber"]         = xnWaterBill.NextSibling.InnerText;
                                 ViewData["AccountNumber"]         = xnWaterBill.NextSibling.NextSibling.InnerText;
                                 ViewData["MeterReadingStartDate"] = xnWaterBill.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["MeterReadingEndDate"]   = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["BillingDays"]           = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["CounterReadingStart"]   = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["CounterReadingEnd"]     = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["TotalHCF"]              = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["TotalGallons"]          = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["First15HCF"]            = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["Next10HCF"]             = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["RemainingHCF"]          = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["SewerCharges"]          = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["StormCharges"]          = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["WaterUsageCharges"]     = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["TotalCharges"]          = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["LocalTaxes"]            = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["StateTaxes"]            = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["PaymentDueDate"]        = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["AmountDue"]             = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["LatePaymentDueDate"]    = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["LateAmountDue"]         = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                            }
                        }
                    }
                }
    
                return View();
            }
    
            
    
            // GET: WaterBills/Edit/5
            public ActionResult Edit(int id)
            {
                XmlDocument xdWaterBills = new XmlDocument();
                string strAccountNumber = string.Empty, strMeterNumber = string.Empty;
                string strFileWaterBills = Server.MapPath("/WaterDistribution/WaterBills.xml");
    
                if (System.IO.File.Exists(strFileWaterBills))
                {
                    using (FileStream fsWaterBills = new FileStream(strFileWaterBills, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        xdWaterBills.Load(fsWaterBills);
    
                        XmlNodeList xnlWaterBills = xdWaterBills.GetElementsByTagName("water-bill-id");
    
                        foreach (XmlNode xnWaterBill in xnlWaterBills)
                        {
                            if (xnWaterBill.InnerText == id.ToString())
                            {
                                 ViewData["WaterBillID"]           = xnWaterBill.InnerText;
                                 ViewData["InvoiceNumber"]         = xnWaterBill.NextSibling.InnerText;
                                 ViewData["AccountNumber"]         = xnWaterBill.NextSibling.NextSibling.InnerText;
                                 ViewData["MeterReadingStartDate"] = xnWaterBill.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["MeterReadingEndDate"]   = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["BillingDays"]           = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["CounterReadingStart"]   = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["CounterReadingEnd"]     = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["TotalHCF"]              = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["TotalGallons"]          = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["First15HCF"]            = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["Next10HCF"]             = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["RemainingHCF"]          = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["SewerCharges"]          = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["StormCharges"]          = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["WaterUsageCharges"]     = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["TotalCharges"]          = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["LocalTaxes"]            = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["StateTaxes"]            = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["PaymentDueDate"]        = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["AmountDue"]             = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["LatePaymentDueDate"]    = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["LateAmountDue"]         = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
    
                                strAccountNumber = xnWaterBill.NextSibling.NextSibling.InnerText;
                            }
                        }
                    }
    
                    XmlDocument xdCustomers = new XmlDocument();
                    string strFileCustomers = Server.MapPath("/WaterDistribution/Customers.xml");
    
                    if (System.IO.File.Exists(strFileCustomers))
                    {
                        using (FileStream fsWaterMeters = new FileStream(strFileCustomers, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            xdCustomers.Load(fsWaterMeters);
    
                            XmlNodeList xnlCustomers = xdCustomers.GetElementsByTagName("account-number");
    
                            foreach (XmlNode xnCustomer in xnlCustomers)
                            {
                                if (xnCustomer.InnerText == strAccountNumber)
                                {
                                     ViewData["AccountNumber"]   = xnCustomer.InnerText;
                                     ViewData["MeterNumber"]     = xnCustomer.NextSibling.InnerText;
                                     ViewData["CustomerName"]    = xnCustomer.NextSibling.NextSibling.InnerText + " " +
                                                                   xnCustomer.NextSibling.NextSibling.NextSibling.InnerText;
                                     ViewData["CustomerAddress"] = xnCustomer.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                     ViewData["CustomerCity"]    = xnCustomer.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                     ViewData["CustomerCounty"]  = xnCustomer.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                     ViewData["CustomerState"]   = xnCustomer.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                     ViewData["CustomerZIPCode"] = xnCustomer.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
    
                                    strMeterNumber = xnCustomer.NextSibling.InnerText;
                                }
                            }
                        }
                    }
    
                    XmlDocument xdWaterMeters = new XmlDocument();
                    string strFileWaterMeters = Server.MapPath("/WaterDistribution/WaterMeters.xml");
    
                    if (System.IO.File.Exists(strFileWaterMeters))
                    {
                        using (FileStream fsWaterMeters = new FileStream(strFileWaterMeters, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            xdWaterMeters.Load(fsWaterMeters);
    
                            XmlNodeList xnlWaterMeters = xdWaterMeters.GetElementsByTagName("meter-number");
    
                            foreach (XmlNode xnWaterMeter in xnlWaterMeters)
                            {
                                if (xnWaterMeter.InnerText == strMeterNumber)
                                {
                                     ViewData["MeterDetails"] = xnWaterMeter.InnerText + " " +
                                                           xnWaterMeter.NextSibling.InnerText + " (" +
                                                           xnWaterMeter.NextSibling.NextSibling.NextSibling.InnerText + ")";
                                }
                            }
                        }
                    }
                }
    
                return View();
            }
    
            // POST: WaterBills/Edit/5
            [HttpPost]
            public ActionResult Edit(int id, FormCollection collection)
            {
                try
                {
                    // TODO: Add update logic here
                    XmlDocument xdWaterBills = new XmlDocument();
                    string strFileWaterBills = Server.MapPath("/WaterDistribution/WaterBills.xml");
    
                    if (System.IO.File.Exists(strFileWaterBills))
                    {
                        using (FileStream fsWaterMeters = new FileStream(strFileWaterBills, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            xdWaterBills.Load(fsWaterMeters);
                        }
    
                        XmlNodeList xnlWaterBills = xdWaterBills.GetElementsByTagName("water-bill-id");
    
                        using (FileStream fsWaterBills = new FileStream(strFileWaterBills, FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            foreach (XmlNode xnWaterBill in xnlWaterBills)
                            {
                                if (xnWaterBill.InnerText == id.ToString())
                                {
                                    xnWaterBill.ParentNode.InnerXml = "<water-bill-id>" + id + "</water-bill-id>" +
                                                                      "<invoice-number>" + collection["InvoiceNumber"] + "</invoice-number>" +
                                                                      "<account-number>" + collection["AccountNumber"] + "</account-number>" +
                                                                      "<meter-reading-start-date>" + collection["MeterReadingStartDate"] + "</meter-reading-start-date>" +
                                                                      "<meter-reading-end-date>" + collection["MeterReadingEndDate"] + "</meter-reading-end-date>" +
                                                                      "<billing-days>" + collection["BillingDays"] + "</billing-days>" +
                                                                      "<counter-reading-start>" + collection["CounterReadingStart"] + "</counter-reading-start>" +
                                                                      "<counter-reading-end>" + collection["CounterReadingEnd"] + "</counter-reading-end>" +
                                                                      "<total-hcf>" + collection["TotalHCF"] + "</total-hcf>" +
                                                                      "<total-gallons>" + collection["TotalGallons"] + "</total-gallons>" +
                                                                      "<first-15-hcf>" + collection["First15HCF"] + "</first-15-hcf>" +
                                                                      "<next-10-hcf>" + collection["Next10HCF"] + "</next-10-hcf>" +
                                                                      "<remaining-hcf>" + collection["RemainingHCF"] + "</remaining-hcf>" +
                                                                      "<sewer-charges>" + collection["SewerCharges"] + "</sewer-charges>" +
                                                                      "<storm-charges>" + collection["StormCharges"] + "</storm-charges>" +
                                                                      "<water-usage-charges>" + collection["WaterUsageCharges"] + "</water-usage-charges>" +
                                                                      "<total-charges>" + collection["TotalCharges"] + "</total-charges>" +
                                                                      "<local-taxes>" + collection["LocalTaxes"] + "</local-taxes>" +
                                                                      "<state-taxes>" + collection["StateTaxes"] + "</state-taxes>" +
                                                                      "<payment-due-date>" + collection["PaymentDueDate"] + "</payment-due-date>" +
                                                                      "<amount-due>" + collection["AmountDue"] + "</amount-due>" +
                                                                      "<late-payment-due-date>" + collection["LatePaymentDueDate"] + "</late-payment-due-date>" +
                                                                      "<late-amount-due>" + collection["LateAmountDue"] + "</late-amount-due>";
    
                                    xdWaterBills.Save(fsWaterBills);
                                    break;
                                }
                            }
                        }
                    }
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
    
            // GET: WaterBills/Delete/5
            public ActionResult Delete(int id)
            {
                XmlDocument xdWaterBills = new XmlDocument();
                string strAccountNumber = string.Empty, strMeterNumber = string.Empty;
                string strFileWaterBills = Server.MapPath("/WaterDistribution/WaterBills.xml");
    
                if (System.IO.File.Exists(strFileWaterBills))
                {
                    using (FileStream fsWaterBills = new FileStream(strFileWaterBills, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        xdWaterBills.Load(fsWaterBills);
    
                        XmlNodeList xnlWaterBills = xdWaterBills.GetElementsByTagName("water-bill-id");
    
                        foreach (XmlNode xnWaterBill in xnlWaterBills)
                        {
                            if (xnWaterBill.InnerText == id.ToString())
                            {
                                 ViewData["WaterBillID"]           = xnWaterBill.InnerText;
                                 ViewData["InvoiceNumber"]         = xnWaterBill.NextSibling.InnerText;
                                 ViewData["AccountNumber"]         = xnWaterBill.NextSibling.NextSibling.InnerText;
                                 ViewData["MeterReadingStartDate"] = xnWaterBill.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["MeterReadingEndDate"]   = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["BillingDays"]           = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["CounterReadingStart"]   = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["CounterReadingEnd"]     = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["TotalHCF"]              = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["TotalGallons"]          = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["First15HCF"]            = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["Next10HCF"]             = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["RemainingHCF"]          = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["SewerCharges"]          = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["StormCharges"]          = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["WaterUsageCharges"]     = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["TotalCharges"]          = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["LocalTaxes"]            = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["StateTaxes"]            = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["PaymentDueDate"]        = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["AmountDue"]             = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["LatePaymentDueDate"]    = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["LateAmountDue"]         = xnWaterBill.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
    
                                strAccountNumber = xnWaterBill.NextSibling.NextSibling.InnerText;
                            }
                        }
                    }
                }
    
                return View();
            }
    
            // POST: WaterBills/Delete/5
            [HttpPost]
            public ActionResult Delete(int id, FormCollection collection)
            {
                try
                {
                    // TODO: Add delete logic here
                    XmlDocument xdWaterBills = new XmlDocument();
                    string strFileWaterBills = Server.MapPath("/WaterDistribution/WaterBills.xml");
    
                    if (System.IO.File.Exists(strFileWaterBills))
                    {
                        using (FileStream fsWaterBills = new FileStream(strFileWaterBills, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            xdWaterBills.Load(fsWaterBills);
                        }
    
                        using (FileStream fsWaterBills = new FileStream(strFileWaterBills, FileMode.Truncate, FileAccess.Write, FileShare.Write))
                        {
                            XmlNodeList xnlWaterBills = xdWaterBills.GetElementsByTagName("invoice");
    
                            foreach (XmlNode xnWaterBill in xnlWaterBills)
                            {
                                if (xnWaterBill.FirstChild.InnerText == id.ToString())
                                {
                                    xnWaterBill.ParentNode.RemoveChild(xnWaterBill);
                                    xdWaterBills.Save(fsWaterBills);
                                    break;
                                }
                            }
                        }
                    }
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
        }
    }
  3. In the document, right-click Index() and click Add View...
  4. In the Add View dialog box, make sure the View Name text is displaying Index. Click Add
  5. Change the document as follows:
    @{
        ViewBag.Title = "Customers Water Bills";
    }
    
    <div class="push-down">
        <h2 class="common-font bold text-center">Customers Water Bills</h2>
    </div>
    
    <hr />
    
    <table class="table table-striped common-font">
        <tr>
            <th class="fw-bold text-center" colspan="2">Identification</th>
            <th class="fw-bold text-center" colspan="4">Meter Reading</th>
            <th class="fw-bold text-center" colspan="2">Counter Reading</th>
            <th class="fw-bold text-center" colspan="5">HCF Summary</th>
            <th class="fw-bold text-center" colspan="4">Charges</th>
            <th class="fw-bold text-center" colspan="2">Taxes</th>
            <th class="fw-bold text-center" colspan="4">Payment</th>
            <th>&nbsp;</th>
        </tr>
        <tr>
            <th class="fw-bold">Bill ID</th>
            <th class="fw-bold">Invoice #</th>
            <th class="fw-bold">Account #</th>
            <th class="fw-bold">Start Date</th>
            <th class="fw-bold">End Date</th>
            <th class="fw-bold">Days</th>
            <th class="fw-bold">Start</th>
            <th class="fw-bold">End</th>
            <th class="fw-bold">Total</th>
            <th class="fw-bold">Gallons</th>
            <th class="fw-bold">First 15</th>
            <th class="fw-bold">Next 10</th>
            <th class="fw-bold">Remaining</th>
            <th class="fw-bold">Sewer</th>
            <th class="fw-bold">Storm</th>
            <th class="fw-bold">Water Usage</th>
            <th class="fw-bold">Total</th>
            <th class="fw-bold">Local</th>
            <th class="fw-bold">State</th>
            <th class="fw-bold">Due Date</th>
            <th class="fw-bold">Amount</th>
            <th class="fw-bold">Late Pmt</th>
            <th class="fw-bold">Late Amt</th>
            <th>&nbsp;</th>
        </tr>
    
        @if (ViewBag.Invoices != null)
        {
            foreach (System.Xml.XmlNode bill in ViewData["Invoices"] as System.Xml.XmlNodeList)
            {
                <tr>
                    <td class="text-center">@bill.FirstChild.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@bill.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>
                        @Html.ActionLink("Update", "Edit", new { id = @bill.FirstChild.InnerText }) <span style="color: aquamarine">::</span>
                        @Html.ActionLink("Review", "Details", new { id = @bill.FirstChild.InnerText }) <span style="color: aquamarine">::</span>
                        @Html.ActionLink("Remove", "Delete", new { id = @bill.FirstChild.InnerText })
                    </td>
                </tr>
            }
        }
    </table>
    
    <p class="text-center">@Html.ActionLink("New Water Bill", "Create", null, htmlAttributes: new { @class = "water-nav" })</p>
  6. Click the WaterBillsController.cs tab to access its class
  7. In the class, right-click Details() and click Add View...
  8. Makre sure the View Name text box is displaying Details. Click Add
  9. Change the document as follows:
    @{
        ViewBag.Title = "Water Bill Details";
    }
    
    <div class="push-down">
        <h2>Water Bill Details</h2>
    </div>
    
    <hr />
    
    <div class="containment">
        <dl class="dl-horizontal common-font caption">
            <dt class="fw-bold">Water Bill ID:</dt>
            <dd>@ViewData["WaterBillID"]</dd>
            <dt class="fw-bold">Invoice #:</dt>
            <dd>@ViewData["InvoiceNumber"]</dd>
            <dt class="fw-bold">Account #:</dt>
            <dd>@ViewData["AccountNumber"]</dd>
            <dt class="fw-bold">Meter Reading Start Date:</dt>
            <dd>@ViewData["MeterReadingStartDate"]</dd>
            <dt class="fw-bold">Meter Reading End Date:</dt>
            <dd>@ViewData["MeterReadingEndDate"]</dd>
            <dt class="fw-bold">Billing Days:</dt>
            <dd>@ViewData["BillingDays"]</dd>
            <dt class="fw-bold">Counter Reading Start:</dt>
            <dd>@ViewData["CounterReadingStart"]</dd>
            <dt class="fw-bold">Counter Reading End:</dt>
            <dd>@ViewData["CounterReadingEnd"]</dd>
            <dt class="fw-bold">Total HCF:</dt>
            <dd>@ViewData["TotalHCF"]</dd>
            <dt class="fw-bold">Total Gallons:</dt>
            <dd>@ViewData["TotalGallons"]</dd>
            <dt class="fw-bold">First 15 HCF:</dt>
            <dd>@ViewData["First15HCF"]</dd>
            <dt class="fw-bold">Next 10 HCF:</dt>
            <dd>@ViewData["Next10HCF"]</dd>
            <dt class="fw-bold">Remaining HCF:</dt>
            <dd>@ViewData["RemainingHCF"]</dd>
            <dt class="fw-bold">Sewer Charges:</dt>
            <dd>@ViewData["SewerCharges"]</dd>
            <dt class="fw-bold">Storm Charges:</dt>
            <dd>@ViewData["StormCharges"]</dd>
            <dt class="fw-bold">Water Usage Charges:</dt>
            <dd>@ViewData["WaterUsageCharges"]</dd>
            <dt class="fw-bold">Total Charges:</dt>
            <dd>@ViewData["TotalCharges"]</dd>
            <dt class="fw-bold">Local Taxes:</dt>
            <dd>@ViewData["LocalTaxes"]</dd>
            <dt class="fw-bold">State Taxes:</dt>
            <dd>@ViewData["StateTaxes"]</dd>
            <dt class="fw-bold">Payment Due Date:</dt>
            <dd>@ViewData["PaymentDueDate"]</dd>
            <dt class="fw-bold">Amount Due:</dt>
            <dd>@ViewData["AmountDue"]</dd>
            <dt class="fw-bold">Late Payment Due Date:</dt>
            <dd>@ViewData["LatePaymentDueDate"]</dd>
            <dt class="fw-bold">Late Amount Due:</dt>
            <dd>@ViewData["LateAmountDue"]</dd>
        </dl>
    </div>
    
    <p class="text-center">
        @Html.ActionLink("Edit/Update Customer Water Bill", "Edit",
                         new { id = ViewData["WaterBillID"] }, htmlAttributes: new { @class = "water -nav" }) :: 
        @Html.ActionLink("Customers Water Bills", "Index",
                         null, new { @class = "water-nav" })
    </p>
  10. In the class, right-click one of the Edit() methods and click Add View...
  11. In the Add View dialog box, make sure the View Name text box is displaying Edit. Click Add
  12. Create a form as follows:
    @{
        ViewBag.Title = "Edit/Update Water Bill";
    }
    
    <div class="push-down">
        <h2>Edit/Update Water Bill</h2>
    </div>
    
    <hr />
    
    @using (Html.BeginForm())
    {
        <div class="form-horizontal">
            <div class="form-group">
                <label for="billNbr" class="control-label col-md-4 caption">Water Bill ID:</label>
                <div class="col-md-8">
                    @Html.TextBox("WaterBillID", null,
                                  htmlAttributes: new { @class = "form-control", id = "billNbr", disabled = "disabled" })
                </div>
            </div>
            <div class="form-group">
                <label for="invoiceNbr" class="control-label col-md-4 caption">Invoice #:</label>
                <div class="col-md-8">
                    @Html.TextBox("InvoiceNumber", null,
                                  htmlAttributes: new { @class = "form-control", id = "invoiceNbr" })
                </div>
            </div>
            <div class="form-group">
                <label for="acntNbr" class="control-label col-md-4 caption">Customer Account #:</label>
                <div class="col-md-8">
                    @Html.TextBox("AccountNumber", null,
                                  htmlAttributes: new { @class = "form-control", id = "acntNbr" })
                </div>
            </div>
    
            <div class="form-group">
                <label class="control-label col-md-4 caption">Customer Name:</label>
                <div class="col-md-8">
                    @Html.TextBox("CustomerName", null,
                                  new { @class = "form-control", id = "custName", disabled = "disabled" })
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-4 caption">Customer Address:</label>
                <div class="col-md-8">
                    @Html.TextBox("CustomerAddress", null,
                                  new { @class = "form-control", id = "adrs", disabled = "disabled" })
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-4">&nbsp;</label>
                <div class="col-md-125">
                    @Html.TextBox("CustomerCity", null,
                                  new { @class = "form-control", id = "city", disabled = "disabled" })
                </div>
                <div class="col-md-125">
                    @Html.TextBox("CustomerCounty", null,
                                  new { @class = "form-control", id = "county", disabled = "disabled" })
                </div>
                <div class="col-md-125">
                    @Html.TextBox("CustomerState", null,
                                  new { @class = "form-control", id = "state", disabled = "disabled" })
                </div>
                <div class="col-md-125">
                    @Html.TextBox("CustomerZIPCode", null,
                                  new { @class = "form-control", id = "zip", disabled = "disabled" })
                </div>
            </div>
    
            <div class="form-group">
                <label class="control-label col-md-4 caption">Meter Details:</label>
                <div class="col-md-125">
                    @Html.TextBox("MeterNumber", null,
                                  new { @class = "form-control", id = "mtrNbr", disabled = "disabled" })
                </div>
                <div class="col-md-6">
                    @Html.TextBox("MeterDetails", null,
                                  new { @class = "form-control", id = "meterDetails", disabled = "disabled" })
                </div>
            </div>
            <hr />
            <div class="form-group">
                <label for="mrsd" class="control-label col-md-4 caption">Meter Reading Start Date:</label>
                <div class="col-md-2">
                    @Html.TextBox("MeterReadingStartDate", null,
                                  htmlAttributes: new { @class = "form-control", id = "mrsd" })
                </div>
                <label for="mred" class="control-label col-md-125 caption">Meter Reading End Date:</label>
                <div class="col-md-125">
                    @Html.TextBox("MeterReadingEndDate", null,
                                  htmlAttributes: new { @class = "form-control", type = "date", id = "mred" })
                </div>
                <label for="days" class="control-label col-md-2 caption">Billing Days:</label>
                <div class="col-md-1">
                    @Html.TextBox("BillingDays", null, htmlAttributes: new { @class = "form-control", id = "days" })
                </div>
            </div>
    
            <div class="form-group">
                <label for="crs" class="control-label col-md-4 caption">Counter Reading Start:</label>
                <div class="col-md-2">
                    @Html.TextBox("CounterReadingStart", null, htmlAttributes: new { @class = "form-control", id = "crs" })
                </div>
                <label for="cre" class="control-label col-md-125 caption">Current Meter Reading:</label>
                <div class="col-md-125">
                    @Html.TextBox("CounterReadingEnd", null, htmlAttributes: new { @class = "form-control", id = "cre" })
                </div>
                <label for="thcf" class="control-label col-md-2 caption">Total HCF:</label>
                <div class="col-md-1">
                    @Html.TextBox("TotalHCF", null, htmlAttributes: new { @class = "form-control", id = "thcf" })
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-4">&nbsp;</label>
                <div class="col-md-2">&nbsp;</div>
                <label class="control-label col-md-125">&nbsp;</label>
                <div class="col-md-125">&nbsp;</div>
                <label for="gallons" class="control-label col-md-2 caption">Total Gallons:</label>
                <div class="col-md-1">
                    @Html.TextBox("TotalGallons", null, htmlAttributes: new { @class = "form-control", id = "gallons" })
                </div>
            </div>
    
            <div class="form-group">
                <label for="f15HCF" class="control-label col-md-4 caption">1st 15 HCF at $3.6121:</label>
                <div class="col-md-2">
                    @Html.TextBox("First15HCF", null,
                                  htmlAttributes: new { @class = "form-control", id = "f15HCF" })
                </div>
                <label for="next10HCF" class="control-label col-md-125 caption">Next 10 HCF at $3.9180:</label>
                <div class="col-md-125">
                    @Html.TextBox("Next10HCF", null,
                                  htmlAttributes: new { @class = "form-control", id = "next10HCF" })
                </div>
                <label for="remHCF" class="control-label col-md-2 caption">Remaining HCF at $4.2763:</label>
                <div class="col-md-1">
                    @Html.TextBox("RemainingHCF", null, htmlAttributes: new { @class = "form-control", id = "remHCF" })
                </div>
            </div>
            <div class="form-group">
                <label for="sewerCharges" class="control-label col-md-4 caption">Sewer Charges:</label>
                <div class="col-md-2">
                    @Html.TextBox("SewerCharges", null,
                                  htmlAttributes: new { @class = "form-control", id = "sewerCharges" })
                </div>
                <label for="stormCharges" class="control-label col-md-125 caption">Storm Charges:</label>
                <div class="col-md-125">
                    @Html.TextBox("StormCharges", null,
                                  htmlAttributes: new { @class = "form-control", id = "stormCharges" })
                </div>
                <label for="wuc" class="control-label col-md-2 caption">Water Usage Charges:</label>
                <div class="col-md-1">
                    @Html.TextBox("WaterUsageCharges", null, htmlAttributes: new { @class = "form-control", id = "wuc" })
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-4">&nbsp;</label>
                <div class="col-md-2">&nbsp;</div>
                <label class="control-label col-md-125">&nbsp;</label>
                <div class="col-md-125">&nbsp;</div>
                <label for="totalCharges" class="control-label col-md-2 caption">Total Charges:</label>
                <div class="col-md-1">
                    @Html.TextBox("TotalCharges", null, htmlAttributes: new { @class = "form-control", id = "totalCharges" })
                </div>
            </div>
            <hr />
            <div class="form-group">
                <label class="control-label col-md-4">&nbsp;</label>
                <div class="col-md-125">&nbsp;</div>
                <label for="localTaxes" class="control-label col-md-125 caption">Local Taxes:</label>
                <div class="col-md-125">
                    @Html.TextBox("LocalTaxes", null,
                                  htmlAttributes: new { @class = "form-control", id = "localTaxes" })
                </div>
                <label for="stateTaxes" class="control-label col-md-125 caption">State Taxes:</label>
                <div class="col-md-125">
                    @Html.TextBox("StateTaxes", null, htmlAttributes: new { @class = "form-control", id = "stateTaxes" })
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-4">&nbsp;</label>
                <div class="col-md-125">&nbsp;</div>
                <label for="pdd" class="control-label col-md-125 caption">Payment Due Date:</label>
                <div class="col-md-125">
                    @Html.TextBox("PaymentDueDate", null,
                                  htmlAttributes: new { @class = "form-control", type = "date", id = "pdd" })
                </div>
                <label for="amtDue" class="control-label col-md-125 caption">Amount Due:</label>
                <div class="col-md-125">
                    @Html.TextBox("AmountDue", null,
                                  htmlAttributes: new { @class = "form-control", id = "amtDue" })
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-4 caption">&nbsp;</label>
                <div class="col-md-125">&nbsp;</div>
                <label for="lpdd" class="control-label col-md-125 caption">Late Payment Due Date:</label>
                <div class="col-md-125">
                    @Html.TextBox("LatePaymentDueDate", null,
                                  htmlAttributes: new { @class = "form-control", id = "lpdd" })
                </div>
                <label for="lateAmtDue" class="control-label col-md-125 caption">Late Amount Due:</label>
                <div class="col-md-125">
                    @Html.TextBox("LateAmountDue", null, htmlAttributes: new { @class = "form-control", id = "lateAmtDue" })
                </div>
            </div>
    
            <div class="form-group text-center">
                <label class="control-label col-md-5">
                    @Html.ActionLink("Water Bills", "Index", null, htmlAttributes: new { @class = "water-nav" })
                </label>
                <div class="col-md-7">
                    <input type="submit" value="Update Water Bill" class="btn btn-primary" />
                </div>
            </div>
        </div>
    }
  13. Click the WaterBillsController.cs tab
  14. In the document, right-click one of the Delete() methods and click Add View...
  15. In the Add View dialog box, make sure the View Name text box is displayinig Delete. Click Add
  16. Create the webpage as follows:
    @{
        ViewBag.Title = "Delete Water Bill";
    }
    
    <div class="push-down">
        <h2>Cancel Water Bill</h2>
    </div>
    
    <hr />
    
    <div class="containment">
        <dl class="dl-horizontal common-font caption">
            <dt class="fw-bold">Water Bill ID:</dt>
            <dd>@ViewData["WaterBillID"]</dd>
            <dt class="fw-bold">Invoice #:</dt>
            <dd>@ViewData["InvoiceNumber"]</dd>
            <dt class="fw-bold">Account #:</dt>
            <dd>@ViewData["AccountNumber"]</dd>
            <dt class="fw-bold">Meter Reading Start Date:</dt>
            <dd>@ViewData["MeterReadingStartDate"]</dd>
            <dt class="fw-bold">Meter Reading End Date:</dt>
            <dd>@ViewData["MeterReadingEndDate"]</dd>
            <dt class="fw-bold">Billing Days:</dt>
            <dd>@ViewData["BillingDays"]</dd>
            <dt class="fw-bold">Counter Reading Start:</dt>
            <dd>@ViewData["CounterReadingStart"]</dd>
            <dt class="fw-bold">Counter Reading End:</dt>
            <dd>@ViewData["CounterReadingEnd"]</dd>
            <dt class="fw-bold">Total HCF:</dt>
            <dd>@ViewData["TotalHCF"]</dd>
            <dt class="fw-bold">Total Gallons:</dt>
            <dd>@ViewData["TotalGallons"]</dd>
            <dt class="fw-bold">First 15 HCF:</dt>
            <dd>@ViewData["First15HCF"]</dd>
            <dt class="fw-bold">Next 10 HCF:</dt>
            <dd>@ViewData["Next10HCF"]</dd>
            <dt class="fw-bold">Remaining HCF:</dt>
            <dd>@ViewData["RemainingHCF"]</dd>
            <dt class="fw-bold">Sewer Charges:</dt>
            <dd>@ViewData["SewerCharges"]</dd>
            <dt class="fw-bold">Storm Charges:</dt>
            <dd>@ViewData["StormCharges"]</dd>
            <dt class="fw-bold">Water Usage Charges:</dt>
            <dd>@ViewData["WaterUsageCharges"]</dd>
            <dt class="fw-bold">Total Charges:</dt>
            <dd>@ViewData["TotalCharges"]</dd>
            <dt class="fw-bold">Local Taxes:</dt>
            <dd>@ViewData["LocalTaxes"]</dd>
            <dt class="fw-bold">State Taxes:</dt>
            <dd>@ViewData["StateTaxes"]</dd>
            <dt class="fw-bold">Payment Due Date:</dt>
            <dd>@ViewData["PaymentDueDate"]</dd>
            <dt class="fw-bold">Amount Due:</dt>
            <dd>@ViewData["AmountDue"]</dd>
            <dt class="fw-bold">Late Payment Due Date:</dt>
            <dd>@ViewData["LatePaymentDueDate"]</dd>
            <dt class="fw-bold">Late Amount Due:</dt>
            <dd>@ViewData["LateAmountDue"]</dd>
        </dl>
    
        <h3 class="common-font caption">Are you sure you want to cancel this water bill (if you do, the bill will disappear from the system)?</h3>
    
        @using (Html.BeginForm())
        {
            <div class="form-actions no-color">
                <input type="submit" value="Cancel this Water Bill" class="btn btn-primary" /> |
                @Html.ActionLink("Customers Water Bills", "Index", null, new { @class = "water-nav" })
            </div>
        }
    </div>

Payments

Bills payments are the means by which a company make smoney. Now, we will create the webpages necessary to create and manage payments.

Practical LearningPractical Learning: Making Payments

  1. In the Solution Explorer, right-click Controllers -> Add -> New Scaffolded Item...
  2. In the middle frame of the Add Scaffold dialog box, make sure MVC 5 Controller With Read/Write Actions is selected.
    Click Add
  3. Type Payments to get PaymentsControllers
  4. Press Enter
  5. Change the class as follows:
    
    using System.Xml;
    u
    using System.Web.Mvc;
    
    namespace StellarWaterPoint1.Controllers
    {
        public class PaymentsController : Controller
        {
            // GET: Payments
            public ActionResult Index()
            {
                XmlDocument xdPayments = new XmlDocument();
                string strFilePayments = Server.MapPath("/WaterDistribution/Payments.xml");
    
                if (System.IO.File.Exists(strFilePayments))
                {
                    using (FileStream fsPayments = new FileStream(strFilePayments, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        xdPayments.Load(fsPayments);
                    }
    
                    if (xdPayments.DocumentElement.ChildNodes.Count > 0)
                    {
                         ViewData["Payments"] = xdPayments.DocumentElement.ChildNodes;
                    }
                    else
                    {
                         ViewData["Payments"] = null;
                    }
                }
    
                return View();
            }
    
            // GET: Payments/Details/5
            public ActionResult Details(int id)
            {
                XmlDocument xdPayments = new XmlDocument();
                string strFilePayments = Server.MapPath("/WaterDistribution/Payments.xml");
    
                if (System.IO.File.Exists(strFilePayments))
                {
                    using (FileStream fsPayments = new FileStream(strFilePayments, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        xdPayments.Load(fsPayments);
    
                        XmlNodeList xnlPayments = xdPayments.GetElementsByTagName("payment-id");
    
                        foreach (XmlNode xnPayment in xnlPayments)
                        {
                            if (xnPayment.InnerText == id.ToString())
                            {
                                 ViewData["PaymentID"]     = xnPayment.InnerText;
                                 ViewData["ReceiptNumber"] = xnPayment.NextSibling.InnerText;
                                 ViewData["WaterBillID"]   = xnPayment.NextSibling.NextSibling.InnerText;
                                 ViewData["PaymentDate"]   = xnPayment.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["PaymentAmount"] = xnPayment.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                            }
                        }
                    }
                }
    
                return View();
            }
    
            // GET: Payments/Create
            public ActionResult Create()
            {
                int payment_id = 0;
                XmlDocument xdPayments = new XmlDocument();
                string strFilePayments = Server.MapPath("/WaterDistribution/Payments.xml");
    
                if (System.IO.File.Exists(strFilePayments))
                {
                    using (FileStream fsPayments = new FileStream(strFilePayments, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        xdPayments.Load(fsPayments);
    
                        XmlNodeList xnlPayments = xdPayments.GetElementsByTagName("payment-id");
    
                        foreach (XmlNode xnPayment in xnlPayments)
                        {
                            payment_id = int.Parse(xnPayment.FirstChild.InnerText);
                        }
                    }
                }
    
                ViewData["PaymentID"] = (payment_id + 1);
    
                Random rndNumber = new Random();
                ViewData["ReceiptNumber"] = rndNumber.Next(100001, 999999).ToString();
    
                return View();
            }
    
            // POST: Payments/Create
            [HttpPost]
            public ActionResult Create(FormCollection collection)
            {
                try
                {
                    // TODO: Add insert logic here
                    // int pmtId = 0;
                    XmlDocument xdPayments = new XmlDocument();
                    string strFilePayments = Server.MapPath("/WaterDistribution/Payments.xml");
    
                    if (System.IO.File.Exists(strFilePayments))
                    {
                        using (FileStream fsPayments = new FileStream(strFilePayments, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            xdPayments.Load(fsPayments);
    
                            /* XmlNodeList xnlPayments = xdPayments.GetElementsByTagName("payment-id");
    
                            foreach (XmlNode xnPayment in xnlPayments)
                            {
                                pmtId = int.Parse(xnPayment.InnerText);
                            } */
                        }
                    }
                    else
                    {
                        xdPayments.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                           "<payments></payments>");
                        // pmtId = 0;
                    }
    
                    // pmtId++;
    
                    XmlElement xePayment = xdPayments.CreateElement("payment");
    
                    xePayment.InnerXml = "<payment-id>"     + collection["PaymentID"]     + "</payment-id>"     +
                                         "<receipt-number>" + collection["ReceiptNumber"] + "</receipt-number>" +
                                         "<water-bill-id>"  + collection["WaterBillID"]   + "</water-bill-id>"  +
                                         "<payment-date>"   + collection["PaymentDate"]   + "</payment-date>"   +
                                         "<payment-amount>" + collection["PaymentAmount"] + "</payment-amount>";
    
                    xdPayments.DocumentElement.AppendChild(xePayment);
    
                    using (FileStream fsPayments = new FileStream(strFilePayments, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        xdPayments.Save(fsPayments);
                    }
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
    
            // GET: Payments/Edit/5
            public ActionResult Edit(int id)
            {
                XmlDocument xdPayments = new XmlDocument();
                string strFilePayments = Server.MapPath("/WaterDistribution/Payments.xml");
    
                if (System.IO.File.Exists(strFilePayments))
                {
                    using (FileStream fsPayments = new FileStream(strFilePayments, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        xdPayments.Load(fsPayments);
    
                        XmlNodeList xnlPayments = xdPayments.GetElementsByTagName("payment-id");
    
                        foreach (XmlNode xnPayment in xnlPayments)
                        {
                            if (xnPayment.InnerText == id.ToString())
                            {
                                 ViewData["PaymentID"]     = xnPayment.InnerText;
                                 ViewData["ReceiptNumber"] = xnPayment.NextSibling.InnerText;
                                 ViewData["WaterBillID"]   = xnPayment.NextSibling.NextSibling.InnerText;
                                 ViewData["PaymentDate"]   = xnPayment.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["PaymentAmount"] = xnPayment.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                            }
                        }
                    }
                }
    
                return View();
            }
    
            // POST: Payments/Edit/5
            [HttpPost]
            public ActionResult Edit(int id, FormCollection collection)
            {
                try
                {
                    // TODO: Add update logic here
                    XmlDocument xdPayments = new XmlDocument();
                    string strFilePayments = Server.MapPath("/WaterDistribution/Payments.xml");
    
                    if (System.IO.File.Exists(strFilePayments))
                    {
                        using (FileStream fsPayments = new FileStream(strFilePayments, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            xdPayments.Load(fsPayments);
                        }
    
                        XmlNodeList xnlPayments = xdPayments.GetElementsByTagName("payment-id");
    
                        using (FileStream fsPayments = new FileStream(strFilePayments, FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            foreach (XmlNode xnPayment in xnlPayments)
                            {
                                if (xnPayment.InnerText == id.ToString())
                                {
                                    xnPayment.ParentNode.InnerXml = "<payment-id>"     + id                          + "</payment-id>"     +
                                                                    "<receipt-number>" + collection["ReceiptNumber"] + "</receipt-number>" +
                                                                    "<water-bill-id>"  + collection["WaterBillID"]   + "</water-bill-id>"  +
                                                                    "<payment-date>"   + collection["PaymentDate"]   + "</payment-date>"   +
                                                                    "<payment-amount>" + collection["PaymentAmount"] + "</payment-amount>";
                                    xdPayments.Save(fsPayments);
                                    break;
                                }
                            }
                        }
                    }
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
    
            // GET: Payments/Delete/5
            public ActionResult Delete(int id)
            {
                XmlDocument xdPayments = new XmlDocument();
                string strFilePayments = Server.MapPath("/WaterDistribution/Payments.xml");
    
                if (System.IO.File.Exists(strFilePayments))
                {
                    using (FileStream fsPayments = new FileStream(strFilePayments, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        xdPayments.Load(fsPayments);
    
                        XmlNodeList xnlPayments = xdPayments.GetElementsByTagName("payment-id");
    
                        foreach (XmlNode xnPayment in xnlPayments)
                        {
                            if (xnPayment.InnerText == id.ToString())
                            {
                                 ViewData["PaymentID"]     = xnPayment.InnerText;
                                 ViewData["ReceiptNumber"] = xnPayment.NextSibling.InnerText;
                                 ViewData["WaterBillID"]   = xnPayment.NextSibling.NextSibling.InnerText;
                                 ViewData["PaymentDate"]   = xnPayment.NextSibling.NextSibling.NextSibling.InnerText;
                                 ViewData["PaymentAmount"] = xnPayment.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                            }
                        }
                    }
                }
    
                return View();
            }
    
            // POST: Payments/Delete/5
            [HttpPost]
            public ActionResult Delete(int id, FormCollection collection)
            {
                try
                {
                    // TODO: Add delete logic here
                    XmlDocument xdPayments = new XmlDocument();
                    string strFilePayments = Server.MapPath("/WaterDistribution/Payments.xml");
    
                    if (System.IO.File.Exists(strFilePayments))
                    {
                        using (FileStream fsPayments = new FileStream(strFilePayments, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            xdPayments.Load(fsPayments);
                        }
    
                        using (FileStream fsPayments = new FileStream(strFilePayments, FileMode.Truncate, FileAccess.Write, FileShare.Write))
                        {
                            XmlNodeList xnlPayments = xdPayments.GetElementsByTagName("payment");
    
                            foreach (XmlNode xnPayment in xnlPayments)
                            {
    
                                if (xnPayment.FirstChild.InnerText == id.ToString())
                                {
                                    xnPayment.ParentNode.RemoveChild(xnPayment);
                                    xdPayments.Save(fsPayments);
                                    break;
                                }
                            }
                        }
                    }
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
        }
    }
  6. In the class, right-click Index() and click Add View...
  7. Make sure the View Name text box is displaying Index. Click Add
  8. Change document as follows:
    @{
        ViewBag.Title = "Water Bills Payments";
    }
    
    <div class="push-down">
        <h2 class="common-font bold text-center">Water Bills Payments</h2>
    </div>
    
    <hr />
    
    <table class="table table-striped common-font">
        <tr>
            <th class="fw-bold">Payment ID</th>
            <th class="fw-bold">Receipt #</th>
            <th class="fw-bold">Water Bill ID</th>
            <th class="fw-bold">Payment Date</th>
            <th class="fw-bold">Payment Amount</th>
            <th>@Html.ActionLink("New Bill Payment", "Create", null, htmlAttributes: new { @class = "water-nav" })</th>
        </tr>
    
        @if (ViewData["Payments"] != null)
        {
            foreach (System.Xml.XmlNode pmt in ViewData["Payments"] as System.Xml.XmlNodeList)
            {
                <tr>
                    <td class="text-center">@pmt.FirstChild.InnerText</td>
                    <td>@pmt.FirstChild.NextSibling.InnerText</td>
                    <td>@pmt.FirstChild.NextSibling.NextSibling.InnerText</td>
                    <td>@pmt.FirstChild.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>@pmt.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.InnerText</td>
                    <td>
                        @Html.ActionLink("Update", "Edit", new { id = pmt.FirstChild.InnerText }) <span style="color: aquamarine">::</span>
                        @Html.ActionLink("Review", "Details", new { id = pmt.FirstChild.InnerText }) <span style="color: aquamarine">::</span>
                        @Html.ActionLink("Remove", "Delete", new { id = pmt.FirstChild.InnerText })
                    </td>
                </tr>
            }
        }
    </table>
  9. Click the PaymentsController.cs tab
  10. In the class, right-click Details() and click Add View...
  11. Make sure the View Name text box is displaying Details. Click Add
  12. Chang the document as follows:
    @{
        ViewBag.Title = "Water Bill Details";
    }
    
    <div class="push-down">
        <h2>Water Bill Details</h2>
    </div>
    
    <hr />
    
    <div class="containment">
        <dl class="dl-horizontal common-font caption">
            <dt class="fw-bold">Payment ID:</dt>
            <dd>@ViewData["PaymentID"]</dd>
            <dt>Receipt #:</dt>
            <dd>@ViewData["ReceiptNumber"]</dd>
            <dt>Water Bill ID:</dt>
            <dd>@ViewData["WaterBillID"]</dd>
            <dt>Payment Date:</dt>
            <dd>@ViewData["PaymentDate"]</dd>
            <dt>Payment Amount:</dt>
            <dd>@ViewData["PaymentAmount"]</dd>
        </dl>
    </div>
    
    <p class="text-center">
        @Html.ActionLink("Edit/Update Bill Payment", "Edit",
                          new { id = ViewData["PaymentID"] }, htmlAttributes: new { @class = "water-nav" }) ::
        @Html.ActionLink("Water Bill Payments", "Index",
                         null, new { @class = "water-nav" })
    </p>
  13. Click the PaymentsController.cs tab
  14. In the document, right-click Create() and click Add View...
  15. In the Add View dialog box, make sure the View Name text box is displaying Create. Click Add
  16. Change the document as follows:
    @{
        ViewBag.Title = "Create Bill Payment";
    }
    
    <div class="push-down">
        <h2>Make Water Bill Payment</h2>
    </div>
    
    <hr />
    
    @using (Html.BeginForm())
    {
    <div class="form-horizontal">
        <div class="form-group">
            <label for="pmtId" class="control-label col-md-4 caption">Payment ID:</label>
            <div class="col-md-8">
                @Html.TextBox("PaymentID", null, htmlAttributes: new { @class = "form-control", id = "pmtId" })
            </div>
        </div>
    
        <div class="form-group">
            <label for="mtrNbr" class="control-label col-md-4 caption">Receipt #:</label>
            <div class="col-md-8">
                @Html.TextBox("ReceiptNumber", null, htmlAttributes: new { @class = "form-control", id = "mtrNbr" })
            </div>
        </div>
    
        <div class="form-group">
            <label for="billId" class="control-label col-md-4 caption">Water Bill ID:</label>
            <div class="col-md-8">
                @Html.TextBox("WaterBillID", null, htmlAttributes: new { @class = "form-control", id = "billId" })
            </div>
        </div>
    
        <div class="form-group">
            <label for="pmtDate" class="control-label col-md-4 caption">Payment Date:</label>
            <div class="col-md-8">
                @Html.TextBox("PaymentDate", null, htmlAttributes: new { @class = "form-control", id = "pmtDate" })
            </div>
        </div>
    
        <div class="form-group">
            <label for="pmtAmt" class="control-label col-md-4 caption">Payment Amount:</label>
            <div class="col-md-8">
                @Html.TextBox("PaymentAmount", null, htmlAttributes: new { @class = "form-control", id = "pmtAmt" })
            </div>
        </div>
    
        <div class="form-group">
            <label class="control-label col-md-5">
                @Html.ActionLink("Bills Payments", "Index", null, htmlAttributes: new { @class = "water-nav" })
            </label>
            <div class="col-md-7">
                <input type="submit" value="Create Bill Payment" class="btn btn-primary" />
            </div>
        </div>
    </div>
    }
  17. Click the PaymntsControllers.cs tab
  18. In the PaymentsController class, right-click one of the Edit() methods and click Add View...
  19. Make sure the View Name text box is displaying Edit. Click Add
  20. Change the webpage as follows:
    @{
        ViewBag.Title = "Edit/Update Bill Payment";
    }
    
    <div class="push-down">
        <h2>Edit/Update Bill Payment</h2>
    </div>
    
    <hr />
    
    @using (Html.BeginForm())
    {
        <div class="form-horizontal">
            <div class="form-group">
                <label class="control-label col-md-4 caption">Payment ID:</label>
                <div class="col-md-8">
                    @Html.TextBox("PaymentID", null,
                                  htmlAttributes: new { @class = "form-control", disabled = "disabled" })
                </div>
            </div>
    
            <div class="form-group">
                <label for="recNbr" class="control-label col-md-4 caption">Receipt #:</label>
                <div class="col-md-8">
                    @Html.TextBox("ReceiptNumber", null, htmlAttributes: new { @class = "form-control", id = "recNbr" })
                </div>
            </div>
    
            <div class="form-group">
                <label for="billNbr" class="control-label col-md-4 caption">Account #:</label>
                <div class="col-md-8">
                    @Html.TextBox("WaterBillID", null, htmlAttributes: new { @class = "form-control", id = "billNbr" })
                </div>
            </div>
    
            <div class="form-group">
                <label for="pmtDate" class="control-label col-md-4 caption">Payment Date:</label>
                <div class="col-md-8">
                    @Html.TextBox("PaymentDate", null,
                                  htmlAttributes: new { @class = "form-control", type = "date", id = "pmtDate" })
                </div>
            </div>
    
            <div class="form-group">
                <label for="pmtAmt" class="control-label col-md-4 caption">Payment Amount:</label>
                <div class="col-md-8">
                    @Html.TextBox("PaymentAmount", null, htmlAttributes: new { @class = "form-control", id = "pmtAmt" })
                </div>
            </div>
    
            <div class="form-group">
                <label class="control-label col-md-5">
                    @Html.ActionLink("Bills Payments", "Index", null, htmlAttributes: new { @class = "water-nav" })
                </label>
                <div class="col-md-7">
                    <input type="submit" value="Update this Payment" class="btn btn-primary" />
                </div>
            </div>
        </div>
    }
  21. Click the PaymentsController.cs tab
  22. In the document, right-click one of the Delete() methods and click Add View...
  23. Make sure the View Name text box is displaying Delete. Click Add
  24. Create the wepbpage as follows:
    @{
        ViewBag.Title = "Delete Bill Payment";
    }
    
    <div class="push-down">
        <h2>Delete Bill Payment</h2>
    </div>
    
    <hr />
    
    <div class="containment">
        <dl class="dl-horizontal common-font caption">
            <dt>Payment ID:</dt>
            <dd>@ViewData["PaymentID"]</dd>
            <dt>Receipt #:</dt>
            <dd>@ViewData["ReceiptNumber"]</dd>
            <dt>Water Bill ID:</dt>
            <dd>@ViewData["WaterBillID"]</dd>
            <dt>Payment Date:</dt>
            <dd>@ViewData["PaymentDate"]</dd>
            <dt>Payment Amount:</dt>
            <dd>@ViewData["PaymentAmount"]</dd>
        </dl>
    
        <h3 class="common-font caption">Are you sure you want to cancel this payment?</h3>
    
        @using (Html.BeginForm())
        {
            <div class="form-actions no-color">
                <input type="submit" value="Cancel this bill payment" class="btn btn-primary" /> |
                @Html.ActionLink("Water Bills Payments", "Index", null, new { @class = "water-nav" })
            </div>
        }
    </div>

Finalizing the Application

Most of the time, it is a good idea to apply a common design to most webpages of a website. In ASP.NET, such a design is created as a layout view.

Practical LearningPractical Learning: Finalizing the Application

  1. In the Solution Explorer, under Views, expand Shared
  2. Click _Layout.cshtml and change the document as follows:
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>@ViewBag.Title - Water for a Shining Life</title>
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
    </head>
    <body>
        <div class="top-bar">
            <div class="containment"><img src="~/Images/wsl1.png" alt="Water for a Shining Life" width="490" height="92" /></div>
        </div>
    
        <div class="navbar navbar-inverse navbar-fixed-top">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    @Html.ActionLink("Home", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
                </div>
                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li>@Html.ActionLink("Emergency Services", "Index", "Home")</li>
                        <li>@Html.ActionLink("Cummunity", "Index", "Home")</li>
                        <li>@Html.ActionLink("Environment", "Index", "Home")</li>
                        <li>@Html.ActionLink("Resources", "Index", "Home")</li>
                        <li>@Html.ActionLink("Projects", "Index", "Home")</li>
                        <li>@Html.ActionLink("Customer Service", "Index", "Home")</li>
                        <li>@Html.ActionLink("Employment", "Index", "Home")</li>
                        <li>@Html.ActionLink("Questions?", "Index", "Home")</li>
                        <li>@Html.ActionLink("About", "About", "Home")</li>
                        <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="container body-content">
            @RenderBody()
            <hr />
            <footer>
                <p class="copyright text-center common-font">&copy; @DateTime.Now.Year - Water for a Shining Life</p>
            </footer>
        </div>
    
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/bootstrap")
        @RenderSection("scripts", required: false)
    </body>
    </html>
  3. In the Solution Explorer, under Views, expand Home, and click Index.cshtml
  4. Change the document as follows:
    <div class="jumbotron">
        <h2>.</h2>
        <p class="lead">
            Our water utility company provides energy, greatness, and warmth
            for a everyday life, a shining life. We provide solutions to families, businesses,
            and the community.
        </p>
    
        <p class="lead">
            This is the employees portal section of the company. From here,
            employees can register a new water meter, manage a customer account, or
            create a water bill.
        </p>
    </div>
    
    <div class="row">
        <div class="col-md-3">
            <h2>Water Meters</h2>
            <p>
                Our company uses the most accurate, sophisticated, and environment-friendly
                water meters on the market.
            </p>
            <p>@Html.ActionLink("Water Meters", "Index", "WaterMeters", null, new { @class = "btn btn-primary" })</p>
        </div>
        <div class="col-md-3">
            <h2>Customers</h2>
            <p>
                We supply water to individuals, families, small
                businesses, as well as enterprises or government agencies.
            </p>
            <p>@Html.ActionLink("Customers", "Index", "Customers", null, new { @class = "btn btn-primary" })</p>
        </div>
        <div class="col-md-3">
            <h2>Water Bills</h2>
            <p>
                Our water rates are very competitive nationwide. We use precise,
                effective, and strict algorithms when calculating our bills.
            </p>
            <p>@Html.ActionLink("Bills/Invoices", "Index", "WaterBills", null, new { @class = "btn btn-primary" })</p>
        </div>
        <div class="col-md-3">
            <h2>Payments</h2>
            <p>
                Our payment system is the simplest, the fairest, and the fastest. Our custiomer's service
                is highly rated.
            </p>
            <p>@Html.ActionLink("Bills Payments", "Index", "Payments", null, new { @class = "btn btn-primary" })</p>
        </div>
    </div>
  5. To execute the application, on the main menu, click Debug -> Start Without Debugging

    Switching a String

  6. Click the Water Meters button

    Switching a String

  7. Click the New Water Meter link

    Water Distribution Company - New Water Meter

  8. Click the Review link that corresponds to the fourth record

    Editing - Updating a Record

  9. Click the Water Meters link
  10. Click the Review link of the last record

    Editing - Updating a Record

  11. Click the Edit/Update Water Meter Information link
  12. Change the following values:
    Meter #:         283-58-958
    Make:            Constance Technologies
    Model:           TR-6224
    Date Last Udate: 04/22/2022

    Introducing Interfaces

  13. Click Update Water Meter Details

    Editing - Updating a Record

  14. Click the Home link
  15. Click the Home link
  16. Click the Bills/Invoices button

    Text Box

  17. Click the New Water Bill link

    Water Distribution Company - New Water Bill

  18. To start a record, in the Customer Account # text box, type 7518-302-6895 and press Tab

    Edit - Update XML Elements

  19. In the Meter Reading End Date text box, type 7/30/2022 and press Tab

    Edit - Update XML Elements

  20. In the Current Meter Reading text box, type 114 and press Tab

    Edit - Update XML Elements

  21. Enter the following two values:
    Payment Due Date:      08/27/2022
    Late Payment Due Date: 09/13/2022

    Edit - Update XML Elements

  22. Click the Save Water Bill button
  23. Click the New Water Bill link
  24. Create a few records with the following values:

    Account # Meter Reading End Date Current Meter Reading Payment Due Date Late Payment Due Date
    4820-375-2842 07/31/2022 109998 08/28/2022 09/14/2022
    2038-413-9680 7/30/2022 137975 8/27/2022 9/13/2022
    9279-570-8394 08/07/2022 6275 08/04/2022 08/20/2022
    7518-302-6895 11/07/2022 118 12/01/2022 12/15/2022
    2038-413-9680 10/27/2022 138012 11/24/2022 12/10/2022

    Water Distribution Comapny - Water Bills

  25. Click the Home link
  26. click the Bills Payments button

    Text Box

  27. Click the New Bill Payment link

    Water Distribution Company

  28. Create a few records with the following values:

    Receipt # Bill ID Payment Date Payment Amount
    625288 2 08/24/2022 145.75
    836168 3 09/12/2022 198.36
    886415 1 08/27/2022 93.34
    724705 4 09/01/2022 41.31
    141806 6 12/05/2022 163.40
    706953 5 12/10/2022 27.44

    Water Distribution Company

  29. Close the browser and return to your programming environment
  30. Close your programming environment

Home Copyright © 2005-2022, FunctionX Friday 06 May 2022 Home