The Order Client

This case study includes a stripped-down order client that includes the ability to send orders, download the product catalog, and create orders based on the current products and choice groupings. However, it lacks many features that would be part of a complete application, including the ability to save a local copy of an order file and print order information. Figure 17-6 shows the main window for the order client.

When the application first launches, it attempts to load the DataSet XML file. If it can't locate or read the file, it warns the user and attempts to download the product catalog. If it finds the DataSet XML but the DataSet object's expiration date has passed, it recommends an update, but gives the user a choice. This enables the user to continue working with a slightly outdated pricing file if the client computer doesn't currently have an Internet connection.

Figure 17-6. The order client

graphics/f17dp06.jpg

Note

Technically, the DataSet serialization is a form of client-side caching. It ensures that the client won't need to download huge amounts of data each time the application is started.


All the download logic is wrapped into a special startup module shown in Listing 17-17. (Remember that a module is just a class in which all the members are shared.) Other parts of the program can retrieve the pricing information from the public Startup.Pricing member variable.

Listing 17-17 The startup module
 Public Module Startup     Public Pricing As New DataSet()     Public Sub Main()         ' Check if it's time to download the pricing file.         CheckExpiration()         Application.Run(New Main())     End Sub     Private Sub CheckExpiration()         Dim ExpiryDate As DateTime 
         Try             ' Attempt to open the DataSet and read the expiry date.             Pricing.ReadXmlSchema(Application.StartupPath & _               "\pricing.xsd")             Pricing.ReadXml(Application.StartupPath & "\pricing.xml")             ExpiryDate = CType( _               Pricing.ExtendedProperties("ExpiryDate"), DateTime)             ' Determine if an upload is required.             If ExpiryDate < DateTime.Now Then                 ' Recommend a download.                 Dim Result As DialogResult                 Result = MessageBox.Show("Your current pricing " & _                   "data is out-of-date. It is recommended that " & _                   "you click OK to perform an automatic pricing " & _                   "download. This requires an Internet connection.", _                   "Confirm Download", MessageBoxButtons.OKCancel, _                   MessageBoxIcon.Question)                 If Result = DialogResult.OK Then                     DownloadData()                 End If             End If         Catch             MessageBox.Show("Error reading file. Click OK to " & _               "attempt to download the price data.", "Error", _                MessageBoxButtons.OK)             DownloadData()         End Try     End Sub     Private Sub DownloadData()         ' Download the DataSet.         Dim Proxy As New localhost.OrderService()         Pricing = Proxy.GetPricingDataSet()         ' Save the DataSet file.         Pricing.WriteXmlSchema(Application.StartupPath & _           "\pricing.xsd")         Pricing.WriteXml(Application.StartupPath & "\pricing.xml")     End Sub End Module 

When the user chooses to add an item, a second window appears (as shown in Figure 17-7). This window dynamically creates and fills the required drop-down list controls by inspecting the choice groupings (as shown in Listing 17-18). For each row in the Choices table it creates a new label and combo box control. It then fills the combo box with all the rows from the Products table that match the corresponding choice.

Listing 17-18 Dynamically creating the order interface
 Private Sub Choices_Load(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles MyBase.Load     Dim y As Integer = 20     Dim rowChoice, rowProduct As DataRow     For Each rowChoice In Startup.Pricing.Tables(0).Rows         ' Create the text label.         Dim lbl As New Label()         lbl.Text = rowChoice("Name") & ":"         lbl.Left = 10         lbl.Top = y + 5         ' Create the listbox.         Dim lst As New ComboBox()         lst.DropDownStyle = ComboBoxStyle.DropDownList         lst.Width = 200         lst.Left = 90         lst.Top = y         y += 40         For Each rowProduct In rowChoice.GetChildRows( _           Startup.Pricing.Relations(0))             lst.Items.Add(New OrderItem(rowProduct("ID"), _               rowProduct("Name"), rowProduct("Price")))         Next         lst.SelectedIndex = 0         pnl.Controls.Add(lst)         pnl.Controls.Add(lbl)         Choices.Add(lst)     Next End Sub 
Figure 17-7. Adding an order station

graphics/f17dp07.jpg

When the window closes, the current selections are provided in the Choice property of the form (as shown in Listing 17-19).

Listing 17-19 Returning a new station
 Public Choice As Station Private Sub cmdOK_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles cmdOK.Click     Choice = New Station()     Choice.Name = txtStation.Text     Dim lst As ComboBox     For Each lst In Choices         Choice.OrderItems.Add(lst.SelectedItem)     Next     Me.DialogResult = DialogResult.OK     Me.Close() End Sub 

The main form then adds the completed station to the list of computers that represents the current order:

 Dim frm As New Choices() If frm.ShowDialog() = DialogResult.OK Then     ' Add station.     lstStations.Items.Add(frm.Choice)     frm.Dispose() End If 

Figure 17-8 shows an order with several stations.

Figure 17-8. A sample order

graphics/f17dp08.jpg

The order can then be sent using the OrderService. The code in Listing 17-20 reacts when the client clicks the Send Order button.

Listing 17-20 Submitting an order
 Private Sub cmdSend_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles cmdSend.Click     ' Change the cursor to indicate that the operation is in progress.     Me.Cursor = Cursors.WaitCursor     Dim Ord As New Order()     Dim OrdStation As Station     Dim Stations As New ArrayList()     For Each OrdStation In lstStations.Items         Stations.Add(OrdStation)     Next     Ord.Stations = Stations.ToArray(GetType(localhost.Station))     Dim Proxy As New localhost.OrderService() 
     If Proxy.SubmitOrder(Ord, CustomerGuid()) = _       OrderState.LoggedInDatabaseOnly Then         MessageBox.Show("Order sent successfully.", _           "Order Registered")     Else         MessageBox.Show("Order sent successfully and is currently " & _           "in progress.", "Order Registered")     End If     lstStations.Items.Clear()     Me.Cursor = Cursors.Default End Sub 

The client displays a confirmation message that varies based on the return value of the service. If the OrderService was able to send a notification message, the client indicates to the user that the order is currently being processed.



Microsoft. NET Distributed Applications(c) Integrating XML Web Services and. NET Remoting
MicrosoftВ® .NET Distributed Applications: Integrating XML Web Services and .NET Remoting (Pro-Developer)
ISBN: 0735619336
EAN: 2147483647
Year: 2005
Pages: 174

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net