To create the Order Processing application, you need to create the classes and windows that enable customers and the administrator to use the Order Processing application.
The Customers.vb file defines the Customers class that provides the functions to register new customers and retrieve information on an existing customer.
Listing 2-2 shows the code for the Customers.vb file:
'Static Model Imports System.Data.SqlClient Public Class Customers Public customerId As Integer Public username As String Public password As String Public firstName As String Public lastName As String Public address1 As String Public address2 As String Public city As String Public country As String Public zipCode As String Private ds As DataSet Private sqlCommand As sqlCommand Private sqlda As SqlDataAdapter 'This method registers a new customer and returns the id of the newly registered customer Public Function addCustomerInformation() As Integer sqlCommand = New SqlCommand("insert into customers values('" & username & "','" & password & "','" & firstName & "','" & lastName & "','" & address1 & "','" & address2 & "','" & city & "','" & country & "','" & zipCode & "' )", sqlConnection) sqlCommand.ExecuteNonQuery() sqlCommand = New SqlCommand("select max(customerId) from customers", sqlConnection) sqlda = New SqlDataAdapter(sqlCommand) ds = New DataSet sqlda.Fill(ds) If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then Return ds.Tables(0).Rows(0).Item(0) Else Return 0 End If End Function 'This method retrieves the information for a specified customer Public Sub retrieveCustomerInformation(ByVal cId As Integer) sqlCommand = New SqlCommand("select firstname,lastname,address1,address2, city,country,zipcode from Customers where customerId='" & cId & "'", sqlConnection) sqlda = New SqlDataAdapter(sqlCommand) ds = New DataSet sqlda.Fill(ds) If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then firstName = ds.Tables(0).Rows(0).Item(0) If Not IsDBNull(ds.Tables(0).Rows(0).Item(1)) Then lastName = ds.Tables(0).Rows(0).Item(1) Else lastName = "" End If address1 = ds.Tables(0).Rows(0).Item(2) If Not IsDBNull(ds.Tables(0).Rows(0).Item(3)) Then address2 = ds.Tables(0).Rows(0).Item(3) Else address2 = "" End If city = ds.Tables(0).Rows(0).Item(4) country = ds.Tables(0).Rows(0).Item(5) zipCode = ds.Tables(0).Rows(0).Item(6) Else firstName = "" lastName = "" address1 = "" address2 = "" city = "" country = "" zipCode = "" End If End Sub End Class 'END CLASS DEFINITION Customers
Download this Listing .
The above listing defines the following methods :
addCustomerInformation() : Registers a new customer and returns the id of the newly registered customer.
retrieveCustomerInformation() : Retrieves the information for a specific customer.
The Orders.vb file defines the Orders class that saves and retrieves information about the orders placed by a customer using the Place Order window.
Listing 2-3 shows the code for the Orders.vb file:
'Static Model Imports System.Data.SqlClient Public Class Orders Public orderDate As Date Public customerId As Integer Public status As String Public completionDate As Date Public amount As Double Public orderId As Integer Dim sqlCommand As sqlCommand Dim sqlda As SqlDataAdapter Dim ds As DataSet 'This method saves the order in the database and returns the order Id of the newly saved order. If this method fails to execute it returns 0 Public Function SaveOrder() As Integer Try sqlCommand = New SqlCommand("insert into orders(orderDate,amount,customerId,status) values('" & orderDate & "'," & amount & "," & customerId & ",'P')", sqlConnection) sqlCommand.ExecuteNonQuery() sqlCommand = New SqlCommand("select max(orderId) from orders", sqlConnection) sqlda = New SqlDataAdapter(sqlCommand) ds = New DataSet sqlda.Fill(ds) If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then Return ds.Tables(0).Rows(0).Item(0) Else Return 0 End If Catch ex As Exception Return 0 End Try End Function Public Sub New() orderId = 0 End Sub 'This method retrieves the orders according to their status. This method returns an ArrayList containing Orders objects that represents the orders placed by the customers Public Function RetrieveOrderByStatus(ByVal strstatus As String) As ArrayList Dim arrayOrders As New ArrayList sqlCommand = New SqlCommand("select orderId,orderDate,amount, customerID,completionDate from orders where status='" & strstatus & "'", sqlConnection) sqlda = New SqlDataAdapter(sqlCommand) ds = New DataSet sqlda.Fill(ds) If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then Dim rowCount As Integer For rowCount = 0 To ds.Tables(0).Rows.Count - 1 Dim objOrders As New Orders 'Retrieve orderId objOrders.orderId = ds.Tables(0).Rows(rowCount).Item(0) 'Retrieve orderDate objOrders.orderDate = ds.Tables(0).Rows(rowCount).Item(1) 'Retrieve customer id objOrders.customerId = ds.Tables(0).Rows(rowCount).Item(3) 'Retrieve amount objOrders.amount = ds.Tables(0).Rows(rowCount).Item(2) 'Retrieve completion date If Not IsDBNull(ds.Tables(0).Rows(rowCount).Item(4)) Then objOrders.completionDate = ds.Tables(0).Rows(rowCount).Item(4) End If arrayOrders.Add(objOrders) Next Return arrayOrders Else 'return empty arraylist Return arrayOrders End If End Function 'This method retrieves the status of the order. This method returns the status as pending, completed, or invoiced. If the status is completed then this method returns the completion date along with the status. If the status is invoiced, this method returns the invoice id and invoice date along with the status. Public Function RetrieveOrderStatus(ByVal orderId As Integer) As String sqlCommand = New SqlCommand("select status,completionDate from orders where orderId=" & orderId & " and customerId=" & custId & "", sqlConnection) sqlda = New SqlDataAdapter(sqlCommand) ds = New DataSet sqlda.Fill(ds) If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then If ds.Tables(0).Rows(0).Item(0) = "P" Then Return "pending" ElseIf ds.Tables(0).Rows(0).Item(0) = "C" Then Return "completed;" & ds.Tables(0).Rows(0).Item(1) Else 'Retrieve the invoice id and invoice date for the specified order Dim objInvoices As New Invoices Return "invoiced;" & objInvoices.retrieveInvoiceInfo(orderId) End If Else Return "" End If End Function 'This method updates the status of the order as completed (in the database as C) or invoiced (in the database as I) depending on the update mode. Public Sub UpdateOrderStatus(ByVal Id As Integer, ByVal mode As String) If mode = "completed" Then Dim dt As System.DateTime sqlCommand = New SqlCommand("update Orders set status='C', completiondate='" & dt.Today & "' where orderId=" & Id & "", sqlConnection) sqlCommand.ExecuteNonQuery() ElseIf mode = "invoiced" Then sqlCommand = New SqlCommand("update Orders set status='I' where orderId=" & Id & "", sqlConnection) sqlCommand.ExecuteNonQuery() End If End Sub End Class 'END CLASS DEFINITION Orders
Download this Listing .
The above listing defines the following methods:
SaveOrder() : Saves the order in the Orders table and returns the order id of the newly saved order.
RetrieveOrderByStatus() : Accepts the order id as a parameter and retrieves the order status whether it is pending, completed, or invoiced.
UpdateOrderStatus() : Accepts the order id and status mode as parameters and updates the order according to the status mode. This method updates the order status as ˜C if the status mode is ˜completed and ˜I if the status mode is ˜invoiced .
The OrderInformation.vb file defines the OrderInformation class that saves and retrieves information about the products in an order that a customer places using the Place Order window.
Listing 2-4 shows the code for the OrderInformation.vb file:
'Static Model Imports System.Data.SqlClient Public Class OrderInformation Public orderId As Integer Public productId As Integer Public quantity As Integer Dim sqlCommand As sqlCommand Dim sqlda As SqlDataAdapter Dim ds As DataSet 'This method saves the product wise order information Public Sub SaveOrderInfo() Try sqlCommand = New SqlCommand("insert into OrderInformation values(" & orderId & "," & productId & "," & quantity & ")", sqlConnection) sqlCommand.ExecuteNonQuery() Catch ex As Exception End Try End Sub 'This method retrieves the product wise information for an order Public Function RetrieveOrderInfo(ByVal Id As Integer) As ArrayList Dim objOrderInformation As New ArrayList sqlCommand = New SqlCommand("select OrderId,ProductId,sum(qty) as total from OrderInformation where OrderId=" & Id & " group by OrderId,ProductId", sqlConnection) sqlda = New SqlDataAdapter(sqlCommand) ds = New DataSet sqlda.Fill(ds) If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then Dim rowcount As Integer For rowcount = 0 To ds.Tables(0).Rows.Count - 1 Dim tempObjOrderInformation As New OrderInformation tempObjOrderInformation.orderId = Id 'Retrieve product id tempObjOrderInformation.productId = ds.Tables(0).Rows(rowcount).Item(1) 'Retrieve quantity ordered for that product tempObjOrderInformation.quantity = ds.Tables(0).Rows(rowcount).Item(2) objOrderInformation.Add(tempObjOrderInformation) Next End If Return objOrderInformation End Function End Class 'END CLASS DEFINITION OrderInformation
Download this Listing .
The above listing defines the following methods:
SaveOrderInfo() : Saves the product information provided in an order, such as the product id and quantity of that product.
RetrieveOrderInfo() : Accepts the order id and returns product information for the order referenced by that order id.
The Invoices.vb file defines the Invoices class that saves and retrieves information about the invoice that the administrator generates using the Invoice window.
Listing 2-5 shows the code for the Invoices.vb file:
'Static Model Imports System.Data.SqlClient Public Class Invoices Public invoiceId As Integer Public invoiceDate As Date Public orderId As Integer Public customerId As Integer Public subTotal As Double Public paymentMode As String Public shipping_handlingCost As Double Public totalAmount As Double Private sqlCommand As sqlCommand Private sqlda As SqlDataAdapter Private ds As DataSet 'This method generates the invoice Public Sub generateInvoice() sqlCommand = New SqlCommand("insert into Invoices values('" & invoiceDate & "'," & orderId & "," & customerId & "," & subTotal & "," & shipping_handlingCost & "," & totalAmount & ",'" & paymentMode & "')", sqlConnection) sqlCommand.ExecuteNonQuery() End Sub 'This method returns the combination of Invoice Id and invoice date as string for the specified order Public Function retrieveInvoiceInfo(ByVal Id As Integer) As String sqlCommand = New SqlCommand("select invoiceId,invoiceDate from invoices where orderId=" & Id & "", sqlConnection) sqlda = New SqlDataAdapter(sqlCommand) ds = New DataSet sqlda.Fill(ds) If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then Return ds.Tables(0).Rows(0).Item(0) & ";" & ds.Tables(0).Rows(0).Item(1) Else Return "" End If End Function 'This method returns the Invoice Id of the last invoice raised Public Function retrieveInvoiceId() As Integer sqlCommand = New SqlCommand("select max(invoiceId) from invoices", sqlConnection) sqlda = New SqlDataAdapter(sqlCommand) ds = New DataSet sqlda.Fill(ds) If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then Return ds.Tables(0).Rows(0).Item(0) Else Return 0 End If End Function End Class 'END CLASS DEFINITION Invoices
Download this Listing .
The above listing defines the following methods:
generateInvoice() : Generates a new invoice and stores the invoice information in the Invoices table.
retrieveInvoiceInfo() : Accepts the order id as a parameter and returns the combination of an invoice id and invoice date as a string that corresponds to the order id.
retrieveInvoiceId() : Retrieves the invoice id of the last invoice generated.
The Invoices.vb file defines the InvoiceCreditCardInfo class that saves the credit card information of a customer that the administrator specifies using the Invoice window.
Listing 2-6 shows the code for the InvoiceCreditCardInfo.vb file:
'Static Model Imports System.Data.SqlClient Public Class InvoiceCreditCardInfo Public invoiceId As Integer Public nameOnCard As String Public creditCardNumber As String Private sqlCommand As sqlCommand Public Sub New(ByVal invoiceId As Integer, ByVal nameOnCard As String, ByVal creditCardNumber As String) Me.invoiceId = invoiceId Me.nameOnCard = nameOnCard Me.creditCardNumber = creditCardNumber End Sub 'This method saves the credit card information of the customer. Public Sub saveCreditCardInfo() sqlCommand = New SqlCommand("insert into InvoiceCreditCardInfo values(" & invoiceId & ",'" & nameOnCard & "','" & creditCardNumber & "')", sqlConnection) sqlCommand.ExecuteNonQuery() End Sub End Class 'END CLASS DEFINITION InvoiceCreditCardInfo
Download this Listing .
In the above listing, the saveCreditCardInfo method saves the customer s credit card information into the InvoiceCreditCardInfo table.
The Shared.vb file defines the Shared module that provides the main() function to initiate the Order Processing application.
Listing 2-7 shows the code for the Shared.vb file:
Imports System.Data.SqlClient Imports System.IO Module _Shared 'A variable to store the login role. The role can be of administrator or customer. Public loginRole As String 'Stores the id of the customer Public custId As Integer 'A boolean variable that keeps in track the success and failure of invoice generation process Public boolInvoiceGeneration As Boolean Dim strSqlConnection As String Public sqlConnection As New sqlConnection Public Sub main() 'Try to establish connection with database OrderProcessing through SQLClient Connection object Try Try 'Read connection string from a text file settings,text located in /bin folder of the application Dim sr As New StreamReader("Settings.txt") strSqlConnection = sr.ReadLine sr.Close() Catch ex As Exception 'If file settings.txt not found strSqlConnection = "SERVER=localhost;UID=sa;PWD=sa;Initial Catalog=OrderProcessing" End Try sqlConnection.ConnectionString = strSqlConnection sqlConnection.Open() Dim objLoginManager As New LoginManager objLoginManager.ShowDialog() Catch ex As Exception MsgBox("Error in establishing connection", MsgBoxStyle.OKOnly, "CONNECTION ERROR") Exit Sub End Try End Sub End Module
Download this Listing .
In the above listing, the main() function establishes a connection with the OrderProcessing database through a SqlConnection object and invokes the Login Manager window.
The LoginManager class defines the Login Manager window. The LoginManager class enables a customer to specify the login credentials to place an order. The LoginManager class also enables a new customer to register.
Listing 2-8 shows the code for the frmLoginManager.vb that defines the LoginManager class:
Imports System.Data.SqlClient Imports System.IO Public Class LoginManager Inherits System.Windows.Forms.Form Dim strSqlConnection As String Dim ds As DataSet Dim sqlda As SqlDataAdapter Dim sqlCommand As sqlCommand Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click 'Verifying the login credentials Try sqlCommand = New SqlCommand("select customerId from Customers where username='" & txtUserName.Text & "' and password='" & txtPassWord.Text & "'", sqlConnection) sqlda = New SqlDataAdapter(sqlCommand) ds = New DataSet sqlda.Fill(ds) Catch ex As Exception MsgBox("Error in login.", MsgBoxStyle.OKOnly, "LOGIN ERROR") txtUserName.Focus() Exit Sub End Try 'To check whether the login attempt is successful If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then If txtUserName.Text = "administrator" Then loginRole = "administrator" Else loginRole = "customer" End If custId = ds.Tables(0).Rows(0).Item(0) 'An object reference to the ApplicationManager class Dim objApplicationManager As New ApplicationManager objApplicationManager.ShowDialog() txtUserName.Text = "" txtPassWord.Text = "" Else MsgBox("Login Unsuccessful.", MsgBoxStyle.OKOnly, "INVALID LOGIN CREDENTIALS") txtUserName.Focus() Exit Sub End If End Sub Private Sub cmdRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRegister.Click Dim objCustomerRegisteration As New CustomerRegistration objCustomerRegisteration.ShowDialog() If Not custId = 0 Then loginRole = "customer" txtUserName.Text = "" txtPassWord.Text = "" 'An object reference to the ApplicationManager class Dim objApplicationManager As New ApplicationManager objApplicationManager.ShowDialog() End If End Sub End Class
Download this Listing .
The above listing defines the following methods:
cmdGo_Click() : Executes when a customer specifies the user name and password and clicks the Login button. This method checks for the validity of the login information and invokes the Application Manager window if the login information is correct.
cmdRegister_Click() : Executes when a prospective customer clicks the New User Sign In Here button to register and obtain access to the Place Order window. This method invokes the Customer Registration window.
Figure 2-4 shows the output of Listing 2-8:
The ApplicationManager class defines the Application Manager window that enables a customer to access the Place Order and the View Order Status windows, and enables an administrator to access the Update Order Status and the Raise Invoice windows.
Listing 2-9 shows the code for the frmApplicationManager.vb that defines the ApplicationManager class:
Public Class ApplicationManager Inherits System.Windows.Forms.Form Private Sub ApplicationManager_Load(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles MyBase.Load If loginRole = "administrator" Then mnuItemUpdateOrder.Enabled = True mnuitemInvoice.Enabled = True mnuViewOrderStatus.Enabled = False mnuPlaceOrder.Enabled = False Else mnuItemUpdateOrder.Enabled = False mnuitemInvoice.Enabled = False mnuViewOrderStatus.Enabled = True mnuPlaceOrder.Enabled = True End If End Sub Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MenuItem6.Click Dim objPlaceOrder As New PlaceOrder objPlaceOrder.ShowDialog() End Sub Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click Dim objViewOrderStatus As New ViewOrderStatus objViewOrderStatus.ShowDialog() End Sub Private Sub mnuItemUpdateOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuItemUpdateOrder.Click Dim objUpdateOrderStatus As New UpdateOrderStatus objUpdateOrderStatus.ShowDialog() End Sub Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click Dim objRaiseInvoice As New RaiseInvoice objRaiseInvoice.ShowDialog() End Sub End Class
Download this Listing .
The above listing defines the following methods:
MenuItem6_Click() : Executes when a customer selects the Orders->Place Order option to place orders. This method invokes the Place Order window.
MenuItem1_Click() : Executes when a customer selects the Orders->View Order Status option to view the status of orders. This method invokes the View Order Status window.
mnuItemUpdateOrder_Click() : Executes when the administrator selects the Orders->Update Order option to update the status of orders. This method invokes the Update Order Status window.
MenuItem4_Click() : Executes when the administrator selects the Invoice->Raise Invoice option to raise invoices for completed orders. This method invokes the Raise Invoice window.
Figure 2-5 shows the output of Listing 2-9:
The customer uses the Orders menu to place orders and view the status of orders. The administrator uses the Orders menu to update the status of orders and uses the Invoices menu to raise the invoices for completed orders.
The PlaceOrder class defines the Place Order window. The PlaceOrder class displays the available products and enables a customer to select products and place an order.
Listing 2-10 shows the code for the frmPlaceOrder.vb that defines the PlaceOrder class:
Imports System.Data.SqlClient Public Class PlaceOrder Inherits System.Windows.Forms.Form Dim ds As DataSet Dim sqlda As SqlDataAdapter Dim sqlCommand As sqlCommand Dim tempListViewItem As ListViewItem Private Sub PlaceOrder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Retrieve product list from the database. Try sqlCommand = New SqlCommand("select product from products", sqlConnection) sqlda = New SqlDataAdapter(sqlCommand) ds = New DataSet sqlda.Fill(ds) Catch ex As Exception MsgBox("Unable to retrieve products.", MsgBoxStyle.OKOnly, "Retrieval Error") Exit Sub End Try If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then Dim rowcount As Integer For rowcount = 0 To ds.Tables(0).Rows.Count - 1 productCombo.Items.Add(ds.Tables(0).Rows(rowcount).Item(0)) Next productCombo.SelectedIndex = 0 Else MsgBox("No product found.", MsgBoxStyle.OKOnly, "First update product information") Exit Sub End If End Sub Private Sub productCombo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles productCombo.SelectedIndexChanged If Not productCombo.Text = "" Then 'Retrieve quantity available and unit price of the selected product. Try sqlCommand = New SqlCommand("select unitPrice,qtyInHand from products where product='" & productCombo.Text & "'", sqlConnection) sqlda = New SqlDataAdapter(sqlCommand) ds = New DataSet sqlda.Fill(ds) If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then txtUnitPrice.Text = ds.Tables(0).Rows(0).Item(0) Dim itemCount As Integer Dim qtyAvailable As Integer = ds.Tables(0).Rows(0).Item(1) For itemCount = 0 To listviewOrder.Items.Count - 1 If listviewOrder.Items(itemCount).Text = productCombo.Text Then qtyAvailable = qtyAvailable - Integer.Parse(listviewOrder.Items(itemCount).SubItems(1).Text) End If Next txtQuantityAvailable.Text = qtyAvailable End If Catch ex As Exception MsgBox("Unable to retrieve information for the selected product.", MsgBoxStyle.OKOnly, "Retrieval Error") Exit Sub End Try End If End Sub Private Sub txtQuantity_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtQuantity.KeyPress If Not Char.IsDigit(e.KeyChar) Then If e.KeyChar = vbBack Then Exit Sub End If e.Handled = True End If End Sub Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click If Not productCombo.Text = "" And Not txtQuantity.Text = "" And Not txtQuantity.Text = "0" Then If Double.Parse(txtQuantity.Text) > Double.Parse(txtQuantityAvailable.Text) Then MsgBox("Order quantity cannot be greater than available quantity.", MsgBoxStyle.OKOnly, "Please check the value") txtQuantity.Focus() Exit Sub End If 'Add the product into the listviewOrder as listviewOrder displays the products in your current order tempListViewItem = listviewOrder.Items.Add(productCombo.Text) tempListViewItem.SubItems.Add(txtQuantity.Text) tempListViewItem.SubItems.Add(Double.Parse(txtUnitPrice.Text) * Double.Parse(txtQuantity.Text)) txtQuantityAvailable.Text = Integer.Parse(txtQuantityAvailable.Text) - Integer.Parse(txtQuantity.Text) Dim itemCount As Integer Dim dblTotalAmount As Double For itemCount = 0 To listviewOrder.Items.Count - 1 dblTotalAmount = dblTotalAmount + Double.Parse(listviewOrder.Items(itemCount).SubItems(2).Text) Next txtAmount.Text = dblTotalAmount txtQuantity.Text = "" productCombo.Focus() End If End Sub Private Sub cmdPlaceOrder_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles cmdPlaceOrder.Click If Not listviewOrder.Items.Count = 0 Then Dim dt As System.DateTime 'Creating an object of type Orders Dim objOrders As New Orders objOrders.customerId = custId objOrders.amount = Double.Parse(txtAmount.Text) objOrders.orderDate = dt.Today 'Call the SaveOrder method of Orders class to save the order Dim orderId As Integer = objOrders.SaveOrder 'If the order is successfully saved then If orderId <> 0 Then Try Dim itemCount As Integer For itemCount = 0 To listviewOrder.Items.Count - 1 'Save the product wise order information into the database Dim objOrderInformation As New OrderInformation objOrderInformation.orderId = orderId 'Retrieve the product Id of the selected product sqlCommand = New SqlCommand("select productId from products where product='" & listviewOrder.Items(itemCount).Text & "'", sqlConnection) sqlda = New SqlDataAdapter(sqlCommand) ds = New DataSet sqlda.Fill(ds) objOrderInformation.productId = ds.Tables(0).Rows(0).Item(0) objOrderInformation.quantity = Integer.Parse(listviewOrder.Items(itemCount).SubItems(1).Text) 'Call the SaveOrderInfo method of OrderInformantion class to save the product wise information of the order objOrderInformation.SaveOrderInfo() 'Update the product quantity sqlCommand = New SqlCommand("update products set qtyInHand=qtyInHand-" & Integer.Parse(listviewOrder.Items(itemCount).SubItems(1).Text) & " where product='" & listviewOrder.Items(itemCount).Text & "'", sqlConnection) sqlCommand.ExecuteNonQuery() Next Catch ex As Exception MsgBox("Your order cannot be saved.", MsgBoxStyle.OKOnly, "Database Error") Exit Sub End Try Else MsgBox("Your order cannot be saved.", MsgBoxStyle.OKOnly, "Database Error") Exit Sub End If MsgBox("Your order is saved successfully. Your ORDER ID is " & orderId & ".", MsgBoxStyle.OKOnly, "Success") listviewOrder.Items.Clear() txtAmount.Text = "" productCombo.Focus() End If End Sub Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click Me.Close() End Sub End Class
Download this Listing .
The above listing defines the following methods:
PlaceOrder_Load() : Retrieves the available products from the database and displays the products in the productCombo drop-down list box.
cmdAdd_Click() : Executes when a customer selects a product, specifies the quantity, and clicks the Add to Order button to add the selected product in the current order. This method adds the selected product in the listviewOrder list view control that represents the current order.
cmdPlaceOrder_Click() : Executes when a customer clicks the Place Order button to place an order for the selected products. This method calls the SaveOrder() method of the Orders class to save the order information, such as the order date, customer id, and total amount. It then calls the SaveOrderInfo() method of the OrderInformation class to save the product information, such as the product id and the quantity for that product in the order.
Figure 2-6 shows the output of Listing 2-10:
The ViewOrderStatus class defines the View Order Status window. The ViewOrderStatus class enables a customer to view the status of an order.
Listing 2-11 shows the code for the frmViewOrderStatus.vb that defines the ViewOrderStatus class:
Imports System.Data.SqlClient Public Class ViewOrderStatus Inherits System.Windows.Forms.Form Dim ds As DataSet Dim sqlda As SqlDataAdapter Dim sqlCommand As sqlCommand Private Sub txtOrderId_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtOrderId.KeyPress If Not Char.IsDigit(e.KeyChar) Then If e.KeyChar = vbBack Then Exit Sub End If e.Handled = True End If End Sub Private Sub cmdViewStatus_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdViewStatus.Click If Not txtOrderId.Text = "" Then Dim strStatus As String Try Dim objOrders As New Orders strStatus = objOrders.RetrieveOrderStatus(txtOrderId.Text) If strStatus = "" Then MsgBox("Invalid OrderId", MsgBoxStyle.OKOnly, "Please check the orderId") Exit Sub End If If strStatus = "pending" Then lblStatus.Text = "Your order referenced by Order Id " & txtOrderId.Text & " is being processed." Else Dim tempArray() As String tempArray = strStatus.Split(";") 'If the status of the order is completed then show the completion date If tempArray(0) = "completed" Then lblStatus.Text = "Your order has been processed. Completion date for your order is " & tempArray(1) & "." Else 'If the status of the order is invoiced then show the invoice id and invoice date lblStatus.Text = "Your invoice for this order has been generated. Invoice Id is " & tempArray(1) & " and date of generation is " & tempArray(2) & "." End If End If Catch ex As Exception MsgBox("Unable to view the order status", MsgBoxStyle.OKOnly, "Database Error") End Try End If End Sub Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click Me.Close() End Sub End Class
Download this Listing .
In the above listing, the cmdViewStatus_Click() method executes when a customer specifies an order id and clicks the View Status button to view the status of the order referred to by that order id. This method calls the RetrieveOrderStatus() method of the Orders class to retrieve the status and displays the retrieved status in the lblStatus label.
Figure 2-7 shows the output of Listing 2-11:
The UpdateOrderStatus class defines the Update Order Status window. The UpdateOrderStatus class displays all pending orders and enables the administrator to update the status of pending orders as completed.
Listing 2-12 shows the code for the frmUpdateOrderStatus.vb that defines the UpdateOrderStatus class:
Imports System.Data.SqlClient Public Class UpdateOrderStatus Inherits System.Windows.Forms.Form Dim tempListViewItem As ListViewItem Dim sqlCommand As sqlCommand Dim sqlda As SqlDataAdapter Dim ds As DataSet Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles cmdUpdate.Click If Not listviewOrders.SelectedItems.Count = 0 Then 'Update the status of the order as completed Dim dt As System.DateTime Try Dim objOrders As New Orders 'Call the UpdateOrderStatus method of the class Orders to update the status of _ the selected order as completed objOrders.UpdateOrderStatus(listviewOrders.SelectedItems(0).Text, "completed") listviewOrders.Items.Remove(listviewOrders.SelectedItems(0)) Catch ex As Exception MsgBox("Unable to update the status of the selected order.", MsgBoxStyle.OKOnly, "Database Error") Exit Sub End Try End If End Sub Private Sub UpdateOrderStatus_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles MyBase.Load 'An arraylist object to hold the order objects passed by RetrieveOrderByStatus method of Orders class Dim arrayOrders As ArrayList Try Dim objOrders As New Orders arrayOrders = objOrders.RetrieveOrderByStatus("P") If Not arrayOrders.Count = 0 Then Dim cnt As Integer For cnt = 0 To arrayOrders.Count - 1 Dim tempObjOrders As Orders tempObjOrders = arrayOrders(cnt) tempListViewItem = listviewOrders.Items.Add(tempObjOrders.orderId) tempListViewItem.SubItems.Add(tempObjOrders.orderDate) 'Retrieving the customer name sqlCommand = New SqlCommand("select firstname, lastname from customers where customerId=" & tempObjOrders.customerId & "", sqlConnection) sqlda = New SqlDataAdapter(sqlCommand) ds = New DataSet sqlda.Fill(ds) If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then tempListViewItem.SubItems.Add(ds.Tables(0).Rows(0).Item(0) & " " & ds.Tables(0).Rows(0).Item(1)) Else tempListViewItem.SubItems.Add("") End If tempListViewItem.SubItems.Add(tempObjOrders.amount) Next Else MsgBox("No orders are pending.", MsgBoxStyle.OKOnly, "No orders to be completed") Exit Sub End If Catch ex As Exception MsgBox("Unable to retrieve pending orders.", MsgBoxStyle.OKOnly, "Retrieval Error") Exit Sub End Try End Sub Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click Me.Close() End Sub Private Sub cmdViewInformation_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles cmdViewInformation.Click 'Retrieve product wise information for the selected order. If Not listviewOrders.SelectedItems.Count = 0 Then Dim objViewOrderInfo As New ViewOrderInfo(listviewOrders.SelectedItems(0).Text) objViewOrderInfo.ShowDialog() End If End Sub End Class
Download this Listing .
The above listing defines the following methods:
UpdateOrderStatus_Load() : Calls the RetrieveOrderByStatus() method of the Orders class to retrieve the pending orders and displays the order information in the listViewOrders list view control.
cmdViewInformation_Click() : Executes when the administrator selects an order from listViewOrders and clicks the View Information button to view the product information for the selected order. This method invokes the View Order Information window that displays the product information.
cmdUpdate_Click() : Executes when the administrator selects an order from the listViewOrders list view control and clicks the Update Order button to update the status of the order as completed. This method calls the UpdateOrderStatus() method of the Orders class to update the status.
Figure 2-8 shows the output of Listing 2-12:
The ViewOrderInfo class defines the View Order Information window. The ViewOrderInfo class displays the products and the quantities required for a specific order.
Listing 2-13 shows the code for the frmViewOrderInfo.vb that defines the ViewOrderInfo class:
'This class displays the product wise information for an order Imports System.Data.SqlClient Public Class ViewOrderInfo Inherits System.Windows.Forms.Form Dim sqlCommand As sqlCommand Dim sqlda As SqlDataAdapter Dim ds As DataSet Dim orderId As Integer Public Sub New(ByVal id As Integer) MyBase.New() InitializeComponent() orderId = id End Sub Dim tempListViewItem As ListViewItem Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click Me.Close() End Sub Private Sub ViewOrderInfo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load 'An arraylist object to hold the OrderInformation objects passed by RetrieveOrderInfo _ method of OrderInformation class. Dim arrayOrderInformation As ArrayList Dim objOrderInformation As New OrderInformation Try arrayOrderInformation = objOrderInformation.RetrieveOrderInfo(orderId) If Not arrayOrderInformation.Count = 0 Then Dim cnt As Integer For cnt = 0 To arrayOrderInformation.Count - 1 Dim tempObjOrderInformation As OrderInformation tempObjOrderInformation = arrayOrderInformation(cnt) tempListViewItem = listviewOrders.Items.Add(tempObjOrderInformation.orderId) 'Retrieving the product name sqlCommand = New SqlCommand("select product from products where productId=" & tempObjOrderInformation.productId & "", sqlConnection) sqlda = New SqlDataAdapter(sqlCommand) ds = New DataSet sqlda.Fill(ds) If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then tempListViewItem.SubItems.Add(ds.Tables(0).Rows(0).Item(0)) Else tempListViewItem.SubItems.Add("") End If tempListViewItem.SubItems.Add(tempObjOrderInformation.quantity) Next End If Catch ex As Exception MsgBox("Unable to retrieve order information.", MsgBoxStyle.OKOnly, "Retrieval Error") Me.Close() End Try End Sub End Class
Download this Listing .
In the above listing, the ViewOrderInfo_Load() method calls the RetrieveOrderInfo() method of the Orders class to retrieve the product information for the order and displays the information in the listviewOrders list view control.
Figure 2-9 shows the output of Listing 2-13:
The RaiseInvoice class defines the Raise Invoice window. The RaiseInvoice class displays all the orders that have been completed and enables the administrator to select an order and raise an invoice for that order.
Listing 2-14 shows the code for the frmRaiseInvoice.vb that defines the RaiseInvoice class:
Imports System.Data.SqlClient Public Class RaiseInvoice Inherits System.Windows.Forms.Form Dim sqlCommand As sqlCommand Dim sqlda As SqlDataAdapter Dim ds As DataSet Dim tempListViewItem As ListViewItem Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click Me.Close() End Sub Private Sub RaiseInvoice_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load 'An arraylist object to hold the order objects passed by RetrieveOrderByStatus method of Orders class Dim arrayOrders As ArrayList Try Dim objOrders As New Orders 'Call the RetrieveOrderByStatus method to retrieve all completed orders arrayOrders = objOrders.RetrieveOrderByStatus("C") If Not arrayOrders.Count = 0 Then Dim cnt As Integer For cnt = 0 To arrayOrders.Count - 1 Dim tempObjOrders As Orders tempObjOrders = arrayOrders(cnt) tempListViewItem = listviewOrders.Items.Add(tempObjOrders.orderId) tempListViewItem.SubItems.Add(tempObjOrders.orderDate) tempListViewItem.SubItems.Add(tempObjOrders.customerId) 'Retrieving the customer name sqlCommand = New SqlCommand("select firstname, lastname from customers where customerId=" & tempObjOrders.customerId & "", sqlConnection) sqlda = New SqlDataAdapter(sqlCommand) ds = New DataSet sqlda.Fill(ds) If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then tempListViewItem.SubItems.Add(ds.Tables(0).Rows(0).Item(0) & " " & ds.Tables(0).Rows(0).Item(1)) Else tempListViewItem.SubItems.Add("") End If tempListViewItem.SubItems.Add(tempObjOrders.completionDate) Next Else MsgBox("No orders are left to be invoiced.", MsgBoxStyle.OKOnly, "No orders left") Me.Close() End If Catch ex As Exception MsgBox("Unable to retrieve completed orders.", MsgBoxStyle.OKOnly, "Retrieval Error") Me.Close() End Try End Sub Private Sub cmdRaise_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRaise.Click If Not listviewOrders.SelectedItems.Count = 0 Then Dim objInvoice As New Invoice(listviewOrders.SelectedItems(0).Text, _ listviewOrders.SelectedItems(0).SubItems(2).Text) objInvoice.ShowDialog() End If 'If invoice generation was a success If boolInvoiceGeneration = True Then 'Update the status of the selected order to invoiced(I) Dim objOrders As New Orders objOrders.UpdateOrderStatus(listviewOrders.SelectedItems(0).Text, "invoiced") listviewOrders.Items.Remove(listviewOrders.SelectedItems(0)) End If End Sub End Class
Download this Listing .
The above listing defines the following methods:
RaiseInvoice_Load() : Calls the RetrieveOrderByStatus() method of the Orders class to retrieve information about the orders that have been completed and displays the information in the listviewOrders list view control.
cmdRaise_Click() : Executes when the administrator selects an order and clicks the Raise button to raise an invoice for the selected order. This method invokes the Invoice window.
Figure 2-10 shows the output of Listing 2-14:
The Invoice class defines the Invoice window. The Invoice class displays the order, customer, and company information and enables the administrator to generate the invoice for an order selected from the Raise Invoice window.
Listing 2-15 shows the code for the frmInvoice.vb that defines the Invoice class:
Imports System.Data.SqlClient Public Class Invoice Inherits System.Windows.Forms.Form Dim sqlCommand As sqlCommand Dim sqlda As SqlDataAdapter Dim ds As DataSet Dim orderId As Integer Dim customerId As Integer Dim tempListViewItem As ListViewItem Dim invoiceId As Integer Public Sub New(ByVal oId As Integer, ByVal cId As Integer) MyBase.New() InitializeComponent() orderId = oId customerId = cId End Sub Private Sub Invoice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Dim dt As System.DateTime dtPicker.Value = dt.Today 'Retrieve the invoice id of the last invoice generated Try Dim objInvoices As New Invoices invoiceId = objInvoices.retrieveInvoiceId() + 1 Catch ex As Exception invoiceId = 1 End Try lblInvoiceId.Text = lblInvoiceId.Text & " " & invoiceId lblOrderId.Text = lblOrderId.Text & " " & orderId 'Retrieve company information Try sqlCommand = New SqlCommand("select * from Company", sqlConnection) sqlda = New SqlDataAdapter(sqlCommand) ds = New DataSet sqlda.Fill(ds) If Not ds.Tables(0).Rows.Count = 0 Then lblCompanyName.Text = lblCompanyName.Text & " : " & _ ds.Tables(0).Rows(0).Item(0) lblCompanyAddress.Text = lblCompanyAddress.Text & " : " & _ ds.Tables(0).Rows(0).Item(1) & ", " & ds.Tables(0).Rows(0).Item(2) & ", " & _ ds.Tables(0).Rows(0).Item(3) & ", " & ds.Tables(0).Rows(0).Item(4) lblCompanyPhone.Text = lblCompanyPhone.Text & " : " & _ ds.Tables(0).Rows(0).Item(5) End If Catch ex As Exception MsgBox("Unable to retrieve company information.", MsgBoxStyle.OKOnly, "Retrieval Error") Me.Close() End Try 'Retrieve Customer Information Try Dim objCustomers As New Customers objCustomers.retrieveCustomerInformation(customerId) If objCustomers.firstName = "" Then MsgBox("Customer information not found.", MsgBoxStyle.OKOnly, "Retrieval Error") Me.Close() Else lblCustomerName.Text = lblCustomerName.Text & " : " & objCustomers.firstName & " " & objCustomers.lastName lblAddress.Text = lblAddress.Text & " : " & objCustomers.address1 & " " _ & objCustomers.address2 lblCity.Text = lblCity.Text & " : " & objCustomers.city lblCountry.Text = lblCountry.Text & " : " & objCustomers.country lblZipCode.Text = lblZipCode.Text & " : " & objCustomers.zipCode End If Catch ex As Exception MsgBox("Unable to retrieve customer information.", MsgBoxStyle.OKOnly, "Retrieval Error") Me.Close() End Try 'Retrieve Order Information. An arraylist object to hold the OrderInformation objects passed by RetrieveOrderInfo method of OrderInformation class. Dim arrayOrderInformation As ArrayList Dim objOrderInformation As New OrderInformation Try arrayOrderInformation = objOrderInformation.RetrieveOrderInfo(orderId) If Not arrayOrderInformation.Count = 0 Then Dim cnt As Integer 'A variable to calculate total billing amount for the order Dim dblTotalAmount As Double = 0 For cnt = 0 To arrayOrderInformation.Count - 1 Dim tempObjOrderInformation As OrderInformation tempObjOrderInformation = arrayOrderInformation(cnt) 'Retrieving the product name sqlCommand = New SqlCommand("select product,unitPrice from products where productId=" & tempObjOrderInformation.productId & "", sqlConnection) sqlda = New SqlDataAdapter(sqlCommand) ds = New DataSet sqlda.Fill(ds) If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then tempListViewItem = listviewOrders.Items.Add(ds.Tables(0).Rows(0).Item(0)) tempListViewItem.SubItems.Add(ds.Tables(0).Rows(0).Item(1)) Else tempListViewItem = listviewOrders.Items.Add("") tempListViewItem.SubItems.Add("") End If tempListViewItem.SubItems.Add(tempObjOrderInformation.quantity) tempListViewItem.SubItems.Add(tempObjOrderInformation.quantity * Double.Parse(ds.Tables(0).Rows(0).Item(1))) dblTotalAmount = dblTotalAmount + tempObjOrderInformation.quantity * Double.Parse(ds.Tables(0).Rows(0).Item(1)) Next txtSubTotal.Text = dblTotalAmount txtTotal.Text = dblTotalAmount End If Catch ex As Exception MsgBox("Unable to retrieve order information.", MsgBoxStyle.OKOnly, "Retrieval Error") Me.Close() End Try End Sub Private Sub txtSubTotal_KeyPress(ByVal sender As Object, ByVal e As _ System.Windows.Forms.KeyPressEventArgs) Handles txtSubTotal.KeyPress e.Handled = True End Sub Private Sub txtTotal_KeyPress(ByVal sender As Object, ByVal e As _ System.Windows.Forms.KeyPressEventArgs) Handles txtTotal.KeyPress e.Handled = True End Sub Private Sub txtshipping_KeyPress(ByVal sender As Object, ByVal e As _ System.Windows.Forms.KeyPressEventArgs) Handles txtshipping.KeyPress If Not Char.IsDigit(e.KeyChar) Then If e.KeyChar = vbBack Then Exit Sub End If e.Handled = True End If End Sub Private Sub txtshipping_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtshipping.TextChanged If txtshipping.Text = "" Then txtshipping.Text = "0" End If txtTotal.Text = Double.Parse(txtSubTotal.Text) + Double.Parse(txtshipping.Text) End Sub Private Sub txtCreditCardNumber_KeyPress(ByVal sender As Object, ByVal e As _ System.Windows.Forms.KeyPressEventArgs) Handles txtCreditCardNumber.KeyPress If Not Char.IsDigit(e.KeyChar) Then If e.KeyChar = vbBack Then Exit Sub End If e.Handled = True End If End Sub Private Sub cashRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles cashRadioButton.CheckedChanged If cashRadioButton.Checked = True Then creditCardGroupBox.Enabled = False Else creditCardGroupBox.Enabled = True End If End Sub Private Sub creditRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles creditRadioButton.CheckedChanged If creditRadioButton.Checked = True Then creditCardGroupBox.Enabled = True Else creditCardGroupBox.Enabled = False End If End Sub Private Sub cmdGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles cmdGenerate.Click Dim dt As System.DateTime If creditRadioButton.Checked = True Then If txtNameOnCard.Text = "" Then MsgBox("Please specify Name as on the credit card.", MsgBoxStyle.OKOnly, "Please _ specify the information") Exit Sub End If If txtCreditCardNumber.Text = "" Then MsgBox("Please specify credit card number.", MsgBoxStyle.OKOnly, "Please specify the information") Exit Sub End If If dt.Today > dtPicker.Value Then MsgBox("Your credit card has expired.", MsgBoxStyle.OKOnly, "Please check the information") Exit Sub End If End If Try If txtshipping.Text = "" Then txtshipping.Text = "0" End If 'Create an object of the type Invoices that represent as invoice Dim objInvoices As New Invoices objInvoices.invoiceDate = dt.Today objInvoices.orderId = orderId objInvoices.shipping_handlingCost = txtshipping.Text objInvoices.subTotal = txtSubTotal.Text objInvoices.totalAmount = txtTotal.Text If creditRadioButton.Checked = True Then objInvoices.paymentMode = "credit card" Else objInvoices.paymentMode = "cash" End If 'Call the generateInvoice method of the class Invoices to save the Invoice objInvoices.generateInvoice() If creditRadioButton.Checked = True Then 'Create an object of the type InvoiceCreditCardInfo Dim objInvoiceCreditCardInfo As New InvoiceCreditCardInfo(invoiceId, txtNameOnCard.Text, txtCreditCardNumber.Text) 'Call the saveCreditCardInfo method to save the credit card information objInvoiceCreditCardInfo.saveCreditCardInfo() End If boolInvoiceGeneration = True Me.Close() Catch ex As Exception MsgBox("Unable to generate the invoice.", MsgBoxStyle.OKOnly, "Database Error") boolInvoiceGeneration = False Me.Close() End Try End Sub End Class
Download this Listing .
The above listing defines the following methods:
Invoice_Load() : Calls the retrieveInvoiceId() method of the Invoices class to retrieve the id of the last invoice generated. This method also calls the retriveCustomerInformation() method of the Customer class to retrieve the information of the customer who placed a particular order. Then this method calls the RetrieveOrderInfo() method of the OrderInformation class to display the product information in the listviewOrders list view control.
cmdGenerate_Click() : Executes when the administrator specifies the shipping and handling cost, selects the payment mode, and clicks the Generate Invoice button to generate the invoice. This method calls the generateInvoice() method of the Invoice class that saves the invoice information in the Invoices table.
Figure 2-11 shows the output of Listing 2-15:
The CustomerRegistration class defines the CustomerRegistration window. The CustomerRegistration class enables a prospective customer to specify the necessary information for registration.
Listing 2-16 shows the code for the frmCustomerRegistration.vb that defines the CustomerRegistration class:
Public Class CustomerRegistration Inherits System.Windows.Forms.Form Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles cmdAdd.Click 'Check for the completion of required information If txtUsername.Text = "" _ ThenMsgBox("Please specify user name.", MsgBoxStyle.OKOnly, "Please check the information") Exit Sub End If If txtPassword.Text = "" Then MsgBox("Please specify the password.", MsgBoxStyle.OKOnly, "Please check the information") Exit Sub End If If txtaddress1.Text = "" Then MsgBox("Please specify address1.", MsgBoxStyle.OKOnly, "Please check the information") Exit Sub End If If txtCity.Text = "" Then MsgBox("Please specify city.", MsgBoxStyle.OKOnly, "Please check the information") Exit Sub End If If txtCountry.Text = "" Then MsgBox("Please specify country.", MsgBoxStyle.OKOnly, "Please check the information") Exit Sub End If If txtZipcode.Text = "" Then MsgBox("Please specify zip code.", MsgBoxStyle.OKOnly, "Please check the information") Exit Sub End If If txtfirstname.Text = "" Then MsgBox("Please specify user name.", MsgBoxStyle.OKOnly, "Please check the information") Exit Sub End If Try 'An object of the class Customers that represents a customer Dim objCustomers As New Customers objCustomers.address1 = txtaddress1.Text objCustomers.address2 = txtAddress2.Text objCustomers.city = txtCity.Text objCustomers.country = txtCountry.Text objCustomers.firstName = txtFirstName.Text objCustomers.lastName = txtLastName.Text objCustomers.password = txtPassword.Text objCustomers.username = txtUsername.Text objCustomers.zipCode = txtZipcode.Text custId = objCustomers.addCustomerInformation If custId = 0 Then MsgBox("Unable to save the information.", MsgBoxStyle.OKOnly, "Database Error") Else MsgBox("You are registered sucessfully.Your username is " & txtUsername.Text & ".", MsgBoxStyle.OKOnly, "SUCCESS") End If Me.Close() Catch ex As Exception MsgBox("Unable to save the information", MsgBoxStyle.OKOnly, "Database Error") Me.Close() End Try End Sub Private Sub txtZipcode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtZipcode.KeyPress If Not Char.IsDigit(e.KeyChar) Then If e.KeyChar = vbBack Then Exit Sub End If e.Handled = True End If End Sub End Class
Download this Listing .
In the above listing, the cmdAdd_Click() method executes when a prospective customer specifies customer information and clicks the Save button to save the information and register. This method checks the validity of the information and calls the addCustomerInformation() method of the Customers class that inserts the customer information in the Customers table.
Figure 2-12 shows the output of Listing 2-16: