WinForms ADO.NET: Stellar Water Point
WinForms ADO.NET: Stellar Water Point
Application Setup
Introduction
A Windows Forms application is a graphical program that uses one or more forms with Windows controls that make an application very user-friendly. One of the options offered by a Windows Forms application is to support a database where values or records can be created and managed.
In this exercise, we will create a Microsoft SQL Server database used in a Windows Forms application. To create and manage records, we will use ADO.NET abd the C# language.
Practical Learning: Introducing the Application
A Database for an Application
As mentioned above, our water distribution application will use a database. You can create the database using Microsoft SQL Server or in the Visual Studio application as a local database. When setting up the database, we will create tables for water meters, customers, and water bills.
Practical Learning: Preparing a Database
USE master; GO IF DB_ID (N'StellarWaterPoint1') IS NOT NULL DROP DATABASE StellarWaterPoint1; GO CREATE DATABASE StellarWaterPoint1; GO USE StellarWaterPoint1; GO
CREATE TABLE WaterMeters ( WaterMeterId int identity(1, 1), MeterNumber nvarchar(10) not null, Make nvarchar(25) null, Model nvarchar(15) not null, MeterSize nvarchar(15), CONSTRAINT PK_WaterMeters PRIMARY KEY(WaterMeterId) ); GO CREATE TABLE Customers ( CustomerId int identity(1, 1), AccountNumber nvarchar(15) not null, MeterNumber nvarchar(10), FirstName nvarchar(18), LastName nvarchar(18) not null, [Address] nvarchar(150), City nvarchar(25), County nvarchar(35), [State] nvarchar(35) not null, ZIPCode nvarchar(12), CONSTRAINT PK_Customers PRIMARY KEY(CustomerId) ); GO CREATE TABLE WaterBills ( WaterBillId int identity(1, 1), AccountNumber nvarchar(15) not null, InvoiceNumber int not null, MeterReadingStartDate nvarchar(50) not null, MeterReadingEndDate nvarchar(50) not null, BillingDays int not null, CounterReadingStart nvarchar(15), CounterReadingEnd nvarchar(15), Consumption nvarchar(15), TotalGallons nvarchar(15), FirstTierConsumption nvarchar(15), SecondTierConsumption nvarchar(15), LastTierConsumption nvarchar(15), WaterCharges nvarchar(15), SewerCharges nvarchar(15), EnvironmentCharges nvarchar(15), TotalCharges nvarchar(15), LocalTaxes nvarchar(15), StateTaxes nvarchar(15), PaymentDueDate nvarchar(50), AmountDue nvarchar(15), LatePaymentDueDate nvarchar(50), LateAmountDue nvarchar(15), CONSTRAINT PK_WaterBills PRIMARY KEY(WaterBillId) ); GO
Supporting ADO.NET
To perform the database operations of our application, we will use ADO.NET.
Practical Learning: Supporting ADO.NET
Water Meters
Setting Up a Water Meter
Before consuming water, a customer must have a water meter installed at his/her residence or in the business location. To represent such a water meter, we had created a table for water meters. We would have created forms to let the user create records.
Practical Learning: Setting Up a Water Meter
Control | (Name) | Text | Other Properties | |
Label | &Meter #: | |||
MaskedTextBox | mtbMeterNumber | Masked: 000-00-000 | ||
Label | M&ake: | |||
TextBox | txtMake | |||
Label | M&odel: | |||
TextBox | txtModel | Modifiers: Public | ||
Label | Me&ter Size: | |||
TextBox | txtMeterSize | |||
Button | btnSaveWateMeter | S&ave Water Meter |
Practical Learning: Presenting Water Meters
Control | Text | Name | Other Properties | |
ListView | lvwWaterMeters | FullRowSelect:True GridLines: True View: Details |
||
Button | Close | btnClose |
List View Columns
(Name) | Text | TextAlign | Width |
colWaterMeterId | Id | 40 | |
colMeterNumber | Meter # | 120 | |
colMake | Make | 225 | |
colModel | Model | 105 | |
colMeterSize | Meter Size | 105 |
Customers
Customers are entities that use a product. For a water distribution company, customers use water provided by the company. We had already created a table for customers, and we added some sample records. To show a list of customers, we will create a form equiped with a list view to display a list of customers.
Practical Learning: Introducing Customers
Control | Text | Name | Other Properties | |
ListView | lvwCustomers | FullRowSelect:True GridLines: True View: Details |
||
Button | Close | btnClose |
List View Columns
(Name) | Text | TextAlign | Width |
colCustomerId | Id | 40 | |
colAccountNumber | Account # | Center | 175 |
colMeterNumber | Meter # | 120 | |
colFirstName | First Name | 105 | |
colLastName | Last Name | 105 | |
colAddress | Address | 250 | |
colCity | City | 120 | |
colCounty | County | 150 | |
colState | State | ||
colZIPCode | ZIP Code | 100 |
New Water Bill
The main subject of a business-customer relationship is a money transaction. Customers receive bills and have to pay. In our database, we created a table for water bills. Now we need a tool, a form, to help an employee create a water bill.
Practical Learning: Introducing Water Bills
Control | Text | Name | Other Properties | |
Label | &Invoice #: | |||
TextBox | txtInvoiceNumber | |||
GroupBox | Customer Information | |||
Label | &Account #: | |||
TextBox | txtAccountNumber | |||
Label | C&ustomer Name: | |||
TextBox | txtCustomerName | |||
Label | Address: | |||
TextBox | txtAddress | |||
TextBox | txtCity | |||
TextBox | txtCounty | |||
TextBox | txtState | |||
TextBox | txtZIPCode | |||
Label | _________________________________________________ | |||
Label | Meter Details: | |||
TextBox | txtMeterDetails | |||
GroupBox | Meter Reading | |||
Label | Meter &Reading Start Date: | |||
Date Time Picker | dtpMeterReadingStartDate | |||
Label | Meter Reading &End Date: | |||
Date Time Picker | dtpMeterReadingEndDate | |||
Label | Coun&ter Reading Start: | |||
TextBox | txtCounterReadingStart | |||
Label | Counter Read&ing End: | |||
TextBox | txtCounterReadingStart | |||
GroupBox | Meter Result | |||
Label | &Billing Days: | |||
TextBox | txtBillingDays | |||
Label | Consu&mption: | |||
TextBox | txtConsumption | |||
Label | Total &Gallons: | |||
TextBox | txtTotalGallons | |||
Label | &First Tier Consumption: | |||
TextBox | txtFirstTierConsumption | |||
Label | Sec&ond Tier: | |||
TextBox | txtSecondTierConsumption | |||
Label | &Last Tier: | |||
TextBox | txtLastTierConsumption | |||
GroupBox | Consumption Charges | |||
Label | &Water Charges: | |||
TextBox | txtWaterCharges | |||
Label | &Sewer Charges: | |||
TextBox | txtSewerCharges | |||
Label | &Environment Charges: | |||
TextBox | txtEnvironmentCharges | |||
Label | &Total Charges: | |||
TextBox | txtTotalCharges | |||
GroupBox | Taxes | |||
Label | Local &Taxes: | |||
TextBox | txtLocalTaxes | |||
Label | &State Taxes: | |||
TextBox | txtStateTaxes | |||
GroupBox | Water Bill Payment | |||
Label | Payment Due Date: | |||
Date Time Picker | dtpPaymentDueDate | |||
Label | &Amount Due: | |||
TextBox | txtAmountDue | |||
Label | Late Payment Due Date: | |||
Date Time Picker | dtpLatePaymentDueDate | |||
Label | &Late Amount Due: | |||
TextBox | txtLateAmountDue | |||
Button | Save Water Bill | btnSaveWaterBill | ||
Button | Close | btnClose |
Form Properties
Form Property | Value |
FormBorderStyle | FixedDialog |
Text | Stellar Water Point - New Water Bill |
StartPosition | CenterScreen |
MaximizeBox | False |
using System.Data; using System.Data.SqlClient; namespace StellarWaterPoint1 { public partial class WaterBillNew : Form { public WaterBillNew() { InitializeComponent(); } private void InitializeWaterBill() { Random rndNumber = new Random(); txtInvoiceNumber.Text = rndNumber.Next(100000, 999999).ToString(); txtAccountNumber.Text = string.Empty; txtCustomerName.Text = string.Empty; txtAddress.Text = string.Empty; txtCity.Text = string.Empty; txtCounty.Text = string.Empty; txtState.Text = string.Empty; txtZIPCode.Text = string.Empty; txtMeterDetails.Text = string.Empty; dtpMeterReadingStartDate.Value = DateTime.Now; dtpMeterReadingEndDate.Value = DateTime.Now; txtBillingDays.Text = string.Empty; txtCounterReadingStart.Text = string.Empty; txtCounterReadingEnd.Text = string.Empty; txtConsumption.Text = string.Empty; txtTotalGallons.Text = string.Empty; txtFirstTierConsumption.Text = string.Empty; txtSecondTierConsumption.Text = string.Empty; txtLastTierConsumption.Text = string.Empty; txtWaterCharges.Text = string.Empty; txtSewerCharges.Text = string.Empty; txtEnvironmentCharges.Text = string.Empty; txtTotalCharges.Text = string.Empty; txtLocalTaxes.Text = string.Empty; txtStateTaxes.Text = string.Empty; dtpPaymentDueDate.Value = DateTime.Now; txtAmountDue.Text = string.Empty; dtpLatePaymentDueDate.Value = DateTime.Now; txtLateAmountDue.Text = string.Empty; } private void WaterBillNew_Load(object sender, EventArgs e) { InitializeWaterBill(); } private void txtAccountNumber_Leave(object sender, EventArgs e) { string meterNumber = string.Empty; using (SqlConnection scStellarWaterPoint = new SqlConnection("Data Source=(local);" + "Database=StellarWaterPoint;" + "Integrated Security=SSPI;")) { SqlCommand cmdCustomers = new SqlCommand("IdentifyClient", scStellarWaterPoint); cmdCustomers.CommandType = CommandType.StoredProcedure; SqlParameter spCustomer = new SqlParameter(); spCustomer.ParameterName = "@AcntNbr"; spCustomer.DbType = DbType.String; spCustomer.Value = txtAccountNumber.Text; spCustomer.Direction = ParameterDirection.Input; cmdCustomers.Parameters.Add(spCustomer); scStellarWaterPoint.Open(); cmdCustomers.ExecuteNonQuery(); SqlDataAdapter sdaCustomers = new SqlDataAdapter(cmdCustomers); DataSet dsCustomers = new DataSet("CustomersSet"); sdaCustomers.Fill(dsCustomers); if (dsCustomers.Tables[0].Rows.Count > 0) { foreach (DataRow drClient in dsCustomers.Tables[0].Rows) { meterNumber = drClient[1].ToString()!; txtCustomerName.Text = string.Format("{0} {1}", drClient[2].ToString(), drClient[3].ToString()); txtAddress.Text = drClient[4].ToString(); txtCity.Text = drClient[5].ToString(); txtCounty.Text = drClient[6].ToString(); txtState.Text = drClient[7].ToString(); txtZIPCode.Text = drClient[8].ToString(); } } else { txtCustomerName.Text = string.Empty; txtAddress.Text = string.Empty; txtCity.Text = string.Empty; txtCounty.Text = string.Empty; txtState.Text = string.Empty; txtZIPCode.Text = string.Empty; txtMeterDetails.Text = string.Empty; } } if (!string.IsNullOrEmpty(meterNumber)) { using (SqlConnection scStellarWaterPoint = new SqlConnection("Data Source=(local);" + "Database=StellarWaterPoint;" + "Integrated Security=SSPI;")) { SqlCommand cmdWaterMeters = new SqlCommand("GetWaterMonitor", scStellarWaterPoint); cmdWaterMeters.CommandType = CommandType.StoredProcedure; SqlParameter spWaterMeter = new SqlParameter(); spWaterMeter.ParameterName = "@MtrNbr"; spWaterMeter.DbType = DbType.String; spWaterMeter.Value = meterNumber; spWaterMeter.Direction = ParameterDirection.Input; cmdWaterMeters.Parameters.Add(spWaterMeter); scStellarWaterPoint.Open(); cmdWaterMeters.ExecuteNonQuery(); SqlDataAdapter sdaWaterMeters = new SqlDataAdapter(cmdWaterMeters); DataSet dsWaterMeters = new DataSet("WaterMetersSet"); sdaWaterMeters.Fill(dsWaterMeters); foreach (DataRow drWaterMeter in dsWaterMeters.Tables[0].Rows) { txtMeterDetails.Text = drWaterMeter[0].ToString() + " - " + drWaterMeter[1].ToString() + " " + drWaterMeter[2].ToString() + " (Size: " + drWaterMeter[3].ToString() + ")"; } } } } private void dtpMeterReadingEndDate_ValueChanged(object sender, EventArgs e) { TimeSpan tsDays = dtpMeterReadingEndDate.Value - dtpMeterReadingStartDate.Value; txtBillingDays.Text = tsDays.Days.ToString(); } private void txtCounterReadingEnd_Leave(object sender, EventArgs e) { double counterStart = 0, counterEnd = 0; try { counterStart = double.Parse(txtCounterReadingStart.Text); } catch (FormatException feCRStart) { MessageBox.Show("There was a problem with the value of the " + "Counter Reading Start. The error produced is: " + feCRStart.Message, "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); } try { counterEnd = double.Parse(txtCounterReadingEnd.Text); } catch (FormatException feCREnd) { MessageBox.Show("There was a problem with the value of the " + "Counter Reading End. The error produced is: " + feCREnd.Message, "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); } double consumption = counterEnd - counterStart; double gallons = consumption * 748.05; double firstTier = gallons * (48.00 / 10000.00); double secondTier = gallons * (32.00 / 10000.00); double lastTier = gallons * (20.00 / 10000.00); double waterCharges = firstTier + secondTier + lastTier; double sewerCharges = waterCharges * 28.65 / 100; double envCharges = waterCharges * 0.22184; double totalCharges = waterCharges + sewerCharges + envCharges; double localTaxes = totalCharges * 0.06148; double stateTaxes = totalCharges * 0.01374; double amtDue = totalCharges + localTaxes + stateTaxes; TimeSpan tsPaymentDueDate = new TimeSpan(15, 0, 0, 0); txtConsumption.Text = consumption.ToString(); txtTotalGallons.Text = gallons.ToString("F"); txtFirstTierConsumption.Text = firstTier.ToString("F"); txtSecondTierConsumption.Text = secondTier.ToString("F"); txtLastTierConsumption.Text = lastTier.ToString("F"); txtWaterCharges.Text = waterCharges.ToString("F"); txtSewerCharges.Text = sewerCharges.ToString("F"); txtEnvironmentCharges.Text = envCharges.ToString("F"); txtTotalCharges.Text = totalCharges.ToString("F"); txtLocalTaxes.Text = localTaxes.ToString("F"); txtStateTaxes.Text = stateTaxes.ToString("F"); dtpPaymentDueDate.Value = dtpMeterReadingEndDate.Value + tsPaymentDueDate; txtAmountDue.Text = amtDue.ToString("F"); dtpLatePaymentDueDate.Value = dtpMeterReadingEndDate.Value + new TimeSpan(30, 0, 0, 0); txtLateAmountDue.Text = (amtDue + 8.95).ToString("F"); } private void btnSaveWaterBill_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtInvoiceNumber.Text)) { MessageBox.Show("You must enter an invoice number. Otherwise, the record cannot be saved.", "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrEmpty(txtAccountNumber.Text)) { MessageBox.Show("You must enter the account number of the customer whose bill is being prepare. " + "Otherwise, the record cannot be saved.", "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrEmpty(txtCounterReadingStart.Text)) { MessageBox.Show("You must type the starting value of the water meter for this customer bill.", "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrEmpty(txtCounterReadingEnd.Text)) { MessageBox.Show("You must type the ending value of the water meter for this bill.", "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } using (SqlConnection scStellarWaterPoint = new SqlConnection("Data Source=(local);" + "Database=StellarWaterPoint;" + "Integrated Security=SSPI;")) { SqlCommand cmdWaterBills = new SqlCommand("INSERT INTO WaterBills(AccountNumber, " + " InvoiceNumber, " + " MeterReadingStartDate, " + " MeterReadingEndDate, " + " BillingDays, " + " CounterReadingStart, " + " CounterReadingEnd, " + " Consumption, " + " TotalGallons, " + " FirstTierConsumption, " + " SecondTierConsumption, " + " LastTierConsumption, " + " WaterCharges, " + " SewerCharges, " + " EnvironmentCharges, " + " TotalCharges, " + " LocalTaxes, " + " StateTaxes, " + " PaymentDueDate, " + " AmountDue, " + " LatePaymentDueDate, " + " LateAmountDue) " + "VALUES(N'" + txtAccountNumber.Text + "', " + txtInvoiceNumber.Text + ", N'" + dtpMeterReadingStartDate.Value.ToShortDateString() + "', N'" + dtpMeterReadingEndDate.Value.ToShortDateString() + "', " + txtBillingDays.Text + " , N'" + txtCounterReadingStart.Text + "', N'" + txtCounterReadingEnd.Text + "', N'" + txtConsumption.Text + "', N'" + txtTotalGallons.Text + "', N'" + txtFirstTierConsumption.Text + "', N'" + txtSecondTierConsumption.Text + "', N'" + txtLastTierConsumption.Text + "', N'" + txtWaterCharges.Text + "', N'" + txtSewerCharges.Text + "', N'" + txtEnvironmentCharges.Text + "', N'" + txtTotalCharges.Text + "', N'" + txtLocalTaxes.Text + "', N'" + txtStateTaxes.Text + "', N'" + dtpPaymentDueDate.Value.ToShortDateString() + "', N'" + txtAmountDue.Text + "', N'" + dtpLatePaymentDueDate.Value.ToShortDateString() + "', N'" + txtLateAmountDue.Text + "')", scStellarWaterPoint); scStellarWaterPoint.Open(); cmdWaterBills.ExecuteNonQuery(); MessageBox.Show("The customer's water bill has been saved.", "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); InitializeWaterBill(); } } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
Water Bill Edition
Records of a database have to be managed. While the primary operation of managing a record is to locate it, the next operation is to change it, or edit or update it. We will provide a form that can allow a user to take care of that.
Practical Learning: Creating a Water Bill Editor
Control | (Name) | Text | Additional Properties | |
Button | btnFindWaterBill | &Find | ||
Button | btnUpdateWaterBill | &Update Water Bill |
using System.Data; using System.Data.SqlClient; namespace StellarWaterPoint1 { public partial class WaterBillEditor : Form { public WaterBillEditor() { InitializeComponent(); } private void btnFindWaterBill_Click(object sender, EventArgs e) { if(string.IsNullOrEmpty(txtInvoiceNumber.Text)) { MessageBox.Show("Please type an invoice number so the system can attempt to find it.", "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } string meterNumber = string.Empty; using (SqlConnection scStellarWaterPoint = new SqlConnection("Data Source=(local);" + "Database=StellarWaterPoint;" + "Integrated Security=SSPI;")) { SqlCommand cmdWaterBills = new SqlCommand("LocateBill", scStellarWaterPoint); cmdWaterBills.CommandType = CommandType.StoredProcedure; SqlParameter spWaterBill = new SqlParameter(); spWaterBill.ParameterName = "@invNbr"; spWaterBill.DbType = DbType.String; spWaterBill.Value = txtInvoiceNumber.Text; spWaterBill.Direction = ParameterDirection.Input; cmdWaterBills.Parameters.Add(spWaterBill); scStellarWaterPoint.Open(); cmdWaterBills.ExecuteNonQuery(); SqlDataAdapter sdaWaterMeters = new SqlDataAdapter(cmdWaterBills); DataSet dsWaterBills = new DataSet("WaterBillsSet"); sdaWaterMeters.Fill(dsWaterBills); if (dsWaterBills.Tables[0].Rows.Count > 0) { foreach (DataRow drBill in dsWaterBills.Tables[0].Rows) { txtAccountNumber.Text = drBill[1].ToString()!; dtpMeterReadingStartDate.Value = DateTime.Parse(drBill[2].ToString()!); dtpMeterReadingEndDate.Value = DateTime.Parse(drBill[3].ToString()!); txtBillingDays.Text = drBill[4].ToString(); txtCounterReadingStart.Text = drBill[5].ToString(); txtCounterReadingEnd.Text = drBill[6].ToString(); txtConsumption.Text = drBill[7].ToString(); txtTotalGallons.Text = drBill[8].ToString(); txtFirstTierConsumption.Text = drBill[9].ToString()!; txtSecondTierConsumption.Text = drBill[10].ToString()!; txtLastTierConsumption.Text = drBill[11].ToString()!; txtWaterCharges.Text = drBill[12].ToString()!; txtSewerCharges.Text = drBill[13].ToString()!; txtEnvironmentCharges.Text = drBill[14].ToString()!; txtTotalCharges.Text = drBill[15].ToString()!; txtLocalTaxes.Text = drBill[16].ToString()!; txtStateTaxes.Text = drBill[17].ToString()!; dtpPaymentDueDate.Value = DateTime.Parse(drBill[18].ToString()!); txtAmountDue.Text = drBill[19].ToString()!; dtpLatePaymentDueDate.Value = DateTime.Parse(drBill[20].ToString()!); txtLateAmountDue.Text = drBill[21].ToString()!; } } else { MessageBox.Show("There is no water bill with that number.", "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); txtAccountNumber.Text = string.Empty; txtCustomerName.Text = string.Empty; txtAddress.Text = string.Empty; txtCity.Text = string.Empty; txtCounty.Text = string.Empty; txtState.Text = string.Empty; txtZIPCode.Text = string.Empty; txtMeterDetails.Text = string.Empty; dtpMeterReadingStartDate.Value = DateTime.Now; dtpMeterReadingEndDate.Value = DateTime.Now; txtBillingDays.Text = string.Empty; txtCounterReadingStart.Text = string.Empty; txtCounterReadingEnd.Text = string.Empty; txtConsumption.Text = string.Empty; txtTotalGallons.Text = string.Empty; txtFirstTierConsumption.Text = string.Empty; txtSecondTierConsumption.Text = string.Empty; txtLastTierConsumption.Text = string.Empty; txtWaterCharges.Text = string.Empty; txtSewerCharges.Text = string.Empty; txtEnvironmentCharges.Text = string.Empty; txtTotalCharges.Text = string.Empty; txtLocalTaxes.Text = string.Empty; txtStateTaxes.Text = string.Empty; dtpPaymentDueDate.Value = DateTime.Now; txtAmountDue.Text = string.Empty; dtpLatePaymentDueDate.Value = DateTime.Now; txtLateAmountDue.Text = string.Empty; return; } } using (SqlConnection scStellarWaterPoint = new SqlConnection("Data Source=(local);" + "Database=StellarWaterPoint;" + "Integrated Security=SSPI;")) { SqlCommand cmdCustomers = new SqlCommand("IdentifyClient", scStellarWaterPoint); cmdCustomers.CommandType = CommandType.StoredProcedure; SqlParameter spCustomer = new SqlParameter(); spCustomer.ParameterName = "@AcntNbr"; spCustomer.DbType = DbType.String; spCustomer.Value = txtAccountNumber.Text; spCustomer.Direction = ParameterDirection.Input; cmdCustomers.Parameters.Add(spCustomer); scStellarWaterPoint.Open(); cmdCustomers.ExecuteNonQuery(); SqlDataAdapter sdaCustomers = new SqlDataAdapter(cmdCustomers); DataSet dsCustomers = new DataSet("CustomersSet"); sdaCustomers.Fill(dsCustomers); if (dsCustomers.Tables[0].Rows.Count > 0) { foreach (DataRow drClient in dsCustomers.Tables[0].Rows) { meterNumber = drClient[1].ToString()!; txtCustomerName.Text = string.Format("{0} {1}", drClient[2].ToString(), drClient[3].ToString()); txtAddress.Text = drClient[4].ToString(); txtCity.Text = drClient[5].ToString(); txtCounty.Text = drClient[6].ToString(); txtState.Text = drClient[7].ToString(); txtZIPCode.Text = drClient[8].ToString(); } } } if (!string.IsNullOrEmpty(meterNumber)) { using (SqlConnection scStellarWaterPoint = new SqlConnection("Data Source=(local);" + "Database=StellarWaterPoint;" + "Integrated Security=SSPI;")) { SqlCommand cmdWaterMeters = new SqlCommand("GetWaterMonitor", scStellarWaterPoint); cmdWaterMeters.CommandType = CommandType.StoredProcedure; SqlParameter spWaterMeter = new SqlParameter(); spWaterMeter.ParameterName = "@MtrNbr"; spWaterMeter.DbType = DbType.String; spWaterMeter.Value = meterNumber; spWaterMeter.Direction = ParameterDirection.Input; cmdWaterMeters.Parameters.Add(spWaterMeter); scStellarWaterPoint.Open(); cmdWaterMeters.ExecuteNonQuery(); SqlDataAdapter sdaWaterMeters = new SqlDataAdapter(cmdWaterMeters); DataSet dsWaterMeters = new DataSet("WaterMetersSet"); sdaWaterMeters.Fill(dsWaterMeters); foreach (DataRow drWaterMeter in dsWaterMeters.Tables[0].Rows) { txtMeterDetails.Text = drWaterMeter[0].ToString() + " - " + drWaterMeter[1].ToString() + " " + drWaterMeter[2].ToString() + " (Size: " + drWaterMeter[3].ToString() + ")"; } } } } private void txtCounterReadingEnd_Leave(object sender, EventArgs e) { double counterStart = 0, counterEnd = 0; try { counterStart = double.Parse(txtCounterReadingStart.Text); } catch (FormatException feCRStart) { MessageBox.Show("There was a problem with the value of the " + "Counter Reading Start. The error produced is: " + feCRStart.Message, "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); } try { counterEnd = double.Parse(txtCounterReadingEnd.Text); } catch (FormatException feCREnd) { MessageBox.Show("There was a problem with the value of the " + "Counter Reading End. The error produced is: " + feCREnd.Message, "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); } double consumption = counterEnd - counterStart; double gallons = consumption * 748.05; double firstTier = gallons * (48.00 / 10000.00); double secondTier = gallons * (32.00 / 10000.00); double lastTier = gallons * (20.00 / 10000.00); double waterCharges = firstTier + secondTier + lastTier; double sewerCharges = waterCharges * 28.65 / 100; double envCharges = waterCharges * 0.22184; double totalCharges = waterCharges + sewerCharges + envCharges; double localTaxes = totalCharges * 0.06148; double stateTaxes = totalCharges * 0.01374; double amtDue = totalCharges + localTaxes + stateTaxes; txtConsumption.Text = consumption.ToString(); txtTotalGallons.Text = gallons.ToString("F"); txtFirstTierConsumption.Text = firstTier.ToString("F"); txtSecondTierConsumption.Text = secondTier.ToString("F"); txtLastTierConsumption.Text = lastTier.ToString("F"); txtWaterCharges.Text = waterCharges.ToString("F"); txtSewerCharges.Text = sewerCharges.ToString("F"); txtEnvironmentCharges.Text = envCharges.ToString("F"); txtTotalCharges.Text = totalCharges.ToString("F"); txtLocalTaxes.Text = localTaxes.ToString("F"); txtStateTaxes.Text = stateTaxes.ToString("F"); dtpPaymentDueDate.Value = dtpMeterReadingEndDate.Value + new TimeSpan(15, 0, 0, 0); txtAmountDue.Text = amtDue.ToString("F"); dtpLatePaymentDueDate.Value = dtpMeterReadingEndDate.Value + new TimeSpan(30, 0, 0, 0); txtLateAmountDue.Text = (amtDue + 8.95).ToString("F"); } private void btnUpdateWaterBill_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtInvoiceNumber.Text)) { MessageBox.Show("You must enter an invoice number. Otherwise, the record cannot be saved.", "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrEmpty(txtAccountNumber.Text)) { MessageBox.Show("You must enter the account number of the customer whose bill is being prepared. " + "Otherwise, the record cannot be saved.", "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } using (SqlConnection scStellarWaterPoint = new SqlConnection("Data Source=(local);" + "Database=StellarWaterPoint;" + "Integrated Security=SSPI;")) { SqlCommand cmdWaterBills = new SqlCommand("UPDATE WaterBills SET AccountNumber = N'" + txtAccountNumber.Text + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET BillingDays = " + txtBillingDays.Text + " WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET CounterReadingStart = N'" + txtCounterReadingStart.Text + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET CounterReadingEnd = N'" + txtCounterReadingEnd.Text + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET Consumption = N'" + txtConsumption.Text + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET TotalGallons = N'" + txtTotalGallons.Text + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET FirstTierConsumption = N'" + txtFirstTierConsumption.Text + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET SecondTierConsumption = N'" + txtSecondTierConsumption.Text + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET LastTierConsumption = N'" + txtLastTierConsumption.Text + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET WaterCharges = N'" + txtWaterCharges.Text + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET SewerCharges = N'" + txtSewerCharges.Text + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET EnvironmentCharges = N'" + txtEnvironmentCharges.Text + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET TotalCharges = N'" + txtTotalCharges.Text + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET LocalTaxes = N'" + txtLocalTaxes.Text + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET StateTaxes = N'" + txtStateTaxes.Text + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET AmountDue = N'" + txtAmountDue.Text + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET LateAmountDue = N'" + txtLateAmountDue.Text + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET MeterReadingStartDate = N'" + dtpMeterReadingStartDate.Value.ToShortDateString() + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET MeterReadingEndDate = N'" + dtpMeterReadingEndDate.Value.ToShortDateString() + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET PaymentDueDate = N'" + dtpPaymentDueDate.Value.ToShortDateString() + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";" + "UPDATE WaterBills SET LatePaymentDueDate = N'" + dtpLatePaymentDueDate.Value.ToShortDateString() + "' WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";", scStellarWaterPoint); scStellarWaterPoint.Open(); cmdWaterBills.ExecuteNonQuery(); MessageBox.Show("The customer's water bill has been updated.", "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); Close(); } } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
Sometimes, a table may have a useless record, in which case it must be deleted. We will create a form that can let an employee locate and delete a record.
Practical Learning: Editing/Updating a Record
Control | (Name) | Text | |
Button | btnDeleteWaterBill | &Delete Water Bill |
using System.Data; using System.Data.SqlClient; namespace StellarWaterPoint1 { public partial class WaterBillDelete : Form { public WaterBillDelete() { InitializeComponent(); } private void btnFindWaterBill_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtInvoiceNumber.Text)) { MessageBox.Show("Please type an invoice number so the system can attempt to find it.", "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } string meterNumber = string.Empty; using (SqlConnection scStellarWaterPoint = new SqlConnection("Data Source=(local);" + "Database=StellarWaterPoint;" + "Integrated Security=SSPI;")) { SqlCommand cmdWaterBills = new SqlCommand("LocateBill", scStellarWaterPoint); cmdWaterBills.CommandType = CommandType.StoredProcedure; SqlParameter spWaterBill = new SqlParameter(); spWaterBill.ParameterName = "@invNbr"; spWaterBill.DbType = DbType.String; spWaterBill.Value = txtInvoiceNumber.Text; spWaterBill.Direction = ParameterDirection.Input; cmdWaterBills.Parameters.Add(spWaterBill); scStellarWaterPoint.Open(); cmdWaterBills.ExecuteNonQuery(); SqlDataAdapter sdaWaterMeters = new SqlDataAdapter(cmdWaterBills); DataSet dsWaterBills = new DataSet("WaterBillsSet"); sdaWaterMeters.Fill(dsWaterBills); if (dsWaterBills.Tables[0].Rows.Count > 0) { foreach (DataRow drBill in dsWaterBills.Tables[0].Rows) { txtAccountNumber.Text = drBill[1].ToString()!; dtpMeterReadingStartDate.Value = DateTime.Parse(drBill[2].ToString()!); dtpMeterReadingEndDate.Value = DateTime.Parse(drBill[3].ToString()!); txtBillingDays.Text = drBill[4].ToString(); txtCounterReadingStart.Text = drBill[5].ToString(); txtCounterReadingEnd.Text = drBill[6].ToString(); txtConsumption.Text = drBill[7].ToString(); txtTotalGallons.Text = drBill[8].ToString(); txtFirstTierConsumption.Text = drBill[9].ToString()!; txtSecondTierConsumption.Text = drBill[10].ToString()!; txtLastTierConsumption.Text = drBill[11].ToString()!; txtWaterCharges.Text = drBill[12].ToString()!; txtSewerCharges.Text = drBill[13].ToString()!; txtEnvironmentCharges.Text = drBill[14].ToString()!; txtTotalCharges.Text = drBill[15].ToString()!; txtLocalTaxes.Text = drBill[16].ToString()!; txtStateTaxes.Text = drBill[17].ToString()!; dtpPaymentDueDate.Value = DateTime.Parse(drBill[18].ToString()!); txtAmountDue.Text = drBill[19].ToString()!; dtpLatePaymentDueDate.Value = DateTime.Parse(drBill[20].ToString()!); txtLateAmountDue.Text = drBill[21].ToString()!; } } else { MessageBox.Show("There is no water bill with that number.", "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); txtAccountNumber.Text = string.Empty; txtCustomerName.Text = string.Empty; txtAddress.Text = string.Empty; txtCity.Text = string.Empty; txtCounty.Text = string.Empty; txtState.Text = string.Empty; txtZIPCode.Text = string.Empty; txtMeterDetails.Text = string.Empty; dtpMeterReadingStartDate.Value = DateTime.Now; dtpMeterReadingEndDate.Value = DateTime.Now; txtBillingDays.Text = string.Empty; txtCounterReadingStart.Text = string.Empty; txtCounterReadingEnd.Text = string.Empty; txtConsumption.Text = string.Empty; txtTotalGallons.Text = string.Empty; txtFirstTierConsumption.Text = string.Empty; txtSecondTierConsumption.Text = string.Empty; txtLastTierConsumption.Text = string.Empty; txtWaterCharges.Text = string.Empty; txtSewerCharges.Text = string.Empty; txtEnvironmentCharges.Text = string.Empty; txtTotalCharges.Text = string.Empty; txtLocalTaxes.Text = string.Empty; txtStateTaxes.Text = string.Empty; dtpPaymentDueDate.Value = DateTime.Now; txtAmountDue.Text = string.Empty; dtpLatePaymentDueDate.Value = DateTime.Now; txtLateAmountDue.Text = string.Empty; return; } } using (SqlConnection scStellarWaterPoint = new SqlConnection("Data Source=(local);" + "Database=StellarWaterPoint;" + "Integrated Security=SSPI;")) { SqlCommand cmdCustomers = new SqlCommand("IdentifyClient", scStellarWaterPoint); cmdCustomers.CommandType = CommandType.StoredProcedure; SqlParameter spCustomer = new SqlParameter(); spCustomer.ParameterName = "@AcntNbr"; spCustomer.DbType = DbType.String; spCustomer.Value = txtAccountNumber.Text; spCustomer.Direction = ParameterDirection.Input; cmdCustomers.Parameters.Add(spCustomer); scStellarWaterPoint.Open(); cmdCustomers.ExecuteNonQuery(); SqlDataAdapter sdaCustomers = new SqlDataAdapter(cmdCustomers); DataSet dsCustomers = new DataSet("CustomersSet"); sdaCustomers.Fill(dsCustomers); if (dsCustomers.Tables[0].Rows.Count > 0) { foreach (DataRow drClient in dsCustomers.Tables[0].Rows) { meterNumber = drClient[1].ToString()!; txtCustomerName.Text = string.Format("{0} {1}", drClient[2].ToString(), drClient[3].ToString()); txtAddress.Text = drClient[4].ToString(); txtCity.Text = drClient[5].ToString(); txtCounty.Text = drClient[6].ToString(); txtState.Text = drClient[7].ToString(); txtZIPCode.Text = drClient[8].ToString(); } } } if (!string.IsNullOrEmpty(meterNumber)) { using (SqlConnection scStellarWaterPoint = new SqlConnection("Data Source=(local);" + "Database=StellarWaterPoint;" + "Integrated Security=SSPI;")) { SqlCommand cmdWaterMeters = new SqlCommand("GetWaterMonitor", scStellarWaterPoint); cmdWaterMeters.CommandType = CommandType.StoredProcedure; SqlParameter spWaterMeter = new SqlParameter(); spWaterMeter.ParameterName = "@MtrNbr"; spWaterMeter.DbType = DbType.String; spWaterMeter.Value = meterNumber; spWaterMeter.Direction = ParameterDirection.Input; cmdWaterMeters.Parameters.Add(spWaterMeter); scStellarWaterPoint.Open(); cmdWaterMeters.ExecuteNonQuery(); SqlDataAdapter sdaWaterMeters = new SqlDataAdapter(cmdWaterMeters); DataSet dsWaterMeters = new DataSet("WaterMetersSet"); sdaWaterMeters.Fill(dsWaterMeters); foreach (DataRow drWaterMeter in dsWaterMeters.Tables[0].Rows) { txtMeterDetails.Text = drWaterMeter[0].ToString() + " - " + drWaterMeter[1].ToString() + " " + drWaterMeter[2].ToString() + " (Size: " + drWaterMeter[3].ToString() + ")"; } } } } private void btnDeleteWaterBill_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtInvoiceNumber.Text)) { MessageBox.Show("You must enter an invoice number. Otherwise, the record cannot be saved.", "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } using (SqlConnection scStellarWaterPoint = new SqlConnection("Data Source=(local);" + "Database=StellarWaterPoint;" + "Integrated Security=SSPI;")) { SqlCommand cmdWaterBills = new SqlCommand("DELETE WaterBills WHERE InvoiceNumber = " + txtInvoiceNumber.Text + ";", scStellarWaterPoint); scStellarWaterPoint.Open(); cmdWaterBills.ExecuteNonQuery(); MessageBox.Show("The water bill has been deleted.", "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); Close(); } } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
A Starting Form
An application should have a point that leads to the other objects. Fortunately, when you create a Windows Forms application, it creates a default starting form. We will simply change the design of that form to suit our needs.
Practical Learning: Finalizing the Application
Control | (Name) | Text | |
Button | btnCreateWaterBill | Create Water Bill... | |
Button | btnUpdateWaterBill | Update Water Bill... | |
Button | btnDeleteWaterBill | Delete Water Bill... | |
Button | btnCustomers | Customers Accounts... | |
Button | btnWaterMeters | Water Meters... | |
Button | btnClose | Close |
namespace StellarWaterPoint1 { public partial class StellarWaterPoint : Form { public StellarWaterPoint() { InitializeComponent(); } private void btnCreateWaterBill_Click(object sender, EventArgs e) { WaterBillNew wbn = new WaterBillNew(); wbn.ShowDialog(this); } private void btnUpdateWaterBill_Click(object sender, EventArgs e) { WaterBillEditor wbe = new(); wbe.ShowDialog(this); } private void btnDeleteWaterBill_Click(object sender, EventArgs e) { WaterBillDelete wbd = new WaterBillDelete(); wbd.ShowDialog(this); } private void btnCustomers_Click(object sender, EventArgs e) { Customers clients = new Customers(); clients.ShowDialog(); } private void btnWaterMeters_Click(object sender, EventArgs e) { WaterMeters wms = new WaterMeters(); wms.ShowDialog(); } private void btnSaveWaterBill_Click(object sender, EventArgs e) { } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
Executing and Testing the Application
After creating and application, you can execute it. You can then test it with sample values.
Practical Learning: Executing and Testing the Application
Invoice #: 468550 Account #: 7518-302-6895 Meter Reading Start Date: 10/5/2022 Meter Reading End Date: 1/9/2023 Counter Reading Start: 96 Counter Reading End: 114
Invoice # | Account # | Meter Reading Start Date | Meter Reading End Date | Counter Reading Start | Counter Reading End |
682416 | 4820-375-2842 | 10/18/2022 | 1/20/2023 | 109992 | 109998 |
306646 | 2038-413-9680 | 10/24/2022 | 1/27/2023 | 137926 | 137975 |
614081 | 9279-570-8394 | 11/14/2022 | 2/15/2023 | 6268 | 6275 |
468206 | 7518-302-6895 | 1/9/2023 | 4/8/2023 | 114 | 118 |
242974 | 2038-413-9680 | 1/27/2023 | 4/27/2023 | 137975 | 138012 |
656117 | 4820-375-2842 | 1/20/2023 | 4/24/2023 | 109998 | 110052 |
764183 | 9279-570-8394 | 2/15/2023 | 5/13/2023 | 6275 | 6295 |
252018 | 7518-302-6895 | 4/8/2023 | 7/11/2023 | 118 | 126 |
503888 | 4820-375-2842 | 4/24/2023 | 7/18/2023 | 110052 | 110102 |
548358 | 2038-413-9680 | 4/27/2023 | 7/28/2023 | 138012 | 138054 |
834278 | 5938-074-5293 | 5/5/2023 | 8/5/2023 | 49 | 52 |
567507 | 9279-570-8394 | 5/13/2023 | 8/12/2023 | 6295 | 6312 |
CREATE PROCEDURE LocateBill @InvNbr nvarchar(15) AS BEGIN SELECT InvoiceNumber, AccountNumber, MeterReadingStartDate, MeterReadingEndDate, BillingDays, CounterReadingStart, CounterReadingEnd, Consumption, TotalGallons, FirstTierConsumption, SecondTierConsumption, LastTierConsumption, WaterCharges, SewerCharges, EnvironmentCharges, TotalCharges, LocalTaxes, StateTaxes, PaymentDueDate, AmountDue, LatePaymentDueDate, LateAmountDue FROM WaterBills WHERE InvoiceNumber = @InvNbr END; GO CREATE PROCEDURE GetWaterMonitor @MtrNbr nvarchar(15) AS BEGIN SELECT MeterNumber, Make, Model, MeterSize FROM WaterMeters WHERE MeterNumber = @MtrNbr END; GO CREATE PROCEDURE IdentifyClient @AcntNbr nvarchar(15) AS BEGIN SELECT AccountNumber, MeterNumber, FirstName, LastName, [Address], City, County, [State], ZIPCode FROM Customers WHERE AccountNumber = @AcntNbr END; GO ------------------------------------------------------------------------------- INSERT INTO WaterMeters(MeterNumber, Make, Model, MeterSize) VALUES(N'392-44-572', N'Constance Technologies', N'TG-4822', N'5/8 Inches'), (N'938-75-869', N'Stanford Trend', N'266G', N'1 1/2 Inches'), (N'799-28-461', N'Constance Technologies', N'BD-7000', N'3/4 Inches'), (N'207-94-835', N'Constance Technologies', N'TG-6220', N'5/8 Inches'), (N'592-84-957', N'Standard Trend', N'428T', N'3/4 Inches'), (N'374-06-284', N'Raynes Energica', N'i2022', N'3/4 Inches'); GO INSERT INTO Customers(AccountNumber, MeterNumber, FirstName, LastName, [Address], City, County, [State], ZIPCode) VALUES(N'9279-570-8394', N'799-28-461', N'Thomas', N'Stones', N'10252 Broward Ave #D4', N'Frederick', N'Frederick', N'MD', N'21703'); GO INSERT INTO Customers(AccountNumber, MeterNumber, FirstName, LastName, [Address], City, [State], ZIPCode) VALUES(N'4820-375-2842', N'392-44-572', N'Akhil', N'Koumari', N'748 Red Hills Rd', N'Roanoke', N'VA', N'24012'); GO INSERT INTO Customers(AccountNumber, MeterNumber, FirstName, LastName, [Address], City, County, [State], ZIPCode) VALUES(N'7518-302-6895', N'207-94-835', N'Grace', N'Brenner', N'4299 Peachtree Court', N'Rockville', N'Montgomery', N'MD', N'20853'); GO INSERT INTO Customers(AccountNumber, MeterNumber, FirstName, LastName, [Address], City, [State], ZIPCode) VALUES(N'2038-413-9680', N'938-75-869', N'Amidou', N'Gomah', N'2075 Rose Hills Ave', N'Washington', N'DC', N'20004'); GO INSERT INTO Customers(AccountNumber, MeterNumber, FirstName, LastName, [Address], City, County, [State], ZIPCode) VALUES(N'5938-074-5293', N'592-84-957', N'Marie', N'Rath', N'582G Dunhill Ave', N'Lanham', N'Prince Georges', N'MD', N'20706'); GO
|
|||
Home | Copyright © 2003-2023, FunctionX | Saturday 01 April 2023 | |
|