Home

Lists-Based Applications: The Music Store

 

Introduction

The DataSet of the .NET Framework is a very impressive class. It allows you to create and manage any type of list, making it a central point for a list-based application, although it is highly used in formal database applications such as those intended for Microsoft SQL Server or Microsoft Access.

To support the creation and management of any type of list, the DataSet class has complete built-in mechanisms for creating tables, their associated columns, and to perform data entry. To demonstrate these functionalities of the DataSet, we are going to create a list-based application that can serve as another introduction to databases. The application simulates a music instrument store that processes orders for customers.

When a customer places an order in the store to purchase a product, the employee can select items by categories, specify the quantity, calculate the total order, and save it.

 

Practical Learning Practical Learning: Creating the Application

  1. Start Microsoft Visual Studio .NET and create a new Windows Application named MusicStore2
  2. To add a new form, on the main menu, click Project -> Windows Form...
  3. Set the Name to DataCenter and press Enter
  4. To add a new form, on the main menu, click Project -> Windows Form...
  5. Set the Name to Categories and press Enter
  6. Design the form as follows:
     
    Control Name Text Other Properties
    DataGrid     Auto Format: Professional 3
    Label   New Category:  
    TextBox txtNewCategory    
    Button btnAdd Add  
    Button btnClose Close  
    Form     AcceptButton: btnAdd
    CancelButton: btnClose
    FormBorderStyle: FixedDialog
    MaximizeBox: False
    ShowInTaskbar: False
    StartPosition: CenterScreen
  7. To add a new form, on the main menu, click Project -> Windows Form...
  8. Set the Name to ItemTypes and press Enter
  9. Design the form as follows:
     
    Control Name Text Other Properties
    DataGrid     Auto Format: Professional 3
    Label   New Item Type:  
    TextBox txtNewItemType    
    Button btnAdd Add  
    Button btnClose Close  
    Form     AcceptButton: btnAdd
    CancelButton: btnClose
    FormBorderStyle: FixedDialog
    MaximizeBox: False
    ShowInTaskbar: False
    StartPosition: CenterScreen
  10. To add a new form, on the main menu, click Project -> Windows Form...
  11. Set the Name to NewStoreItem and press Enter
  12. Design the form as follows:
     
    Control Name Text Other Properties
    Label   Category:  
    ComboBox cboCategories   DropDownStyle: DropDownList
    Sorted: True
    Button btnNewCategory New  
    Label   Item Type:  
    ComboBox cboItemTypes   DropDownStyle: DropDownList
    Sorted: True
    Button btnNewType New  
    Label   Item Name:  
    TextBox txtItemName    
    Label   Unit Price:  
    TextBox txtUnitPrice 0.00 AlignText: Right
    Label   Item #:  
    TextBox txtItemNumber    
    Button btnCreate Create  
    Button btnClose Close  
    Form     AcceptButton: btnCreate
    CancelButton: btnClose
    MaximizeBox: False
    StartPosition: CenterScreen
  13. To add a new form, on the main menu, click Project -> Add Windows Form...
  14. Set the Name to PurchaseOrder and press Enter
  15. Design the form as follows:
     
    Control Name Text Other Properties
    GroupBox   Selection of Sale Item  
    Label   Category  
    Label   Types  
    Label   Available Items  
    Button btnNewItem New Item  
    ListBox lbxCategories    
    ListBox lbxItemTypes    
    ListBox lbxAvailableItems    
    GroupBox   Items Sold  
    Label   Item #  
    Label   Item Name  
    Label   Unit Price  
    Label   Qty  
    Label   Sub Total  
    TextBox txtItemNumber1    
    TextBox txtItemName1    
    TextBox txtUnitPrice1 0.00 AlignText: Right
    TextBox txtQuantity1 0 AlignText: Right
    TextBox txtSubTotal1 0.00 AlignText: Right
    TextBox txtItemNumber2    
    TextBox txtItemName2    
    TextBox txtUnitPrice2 0.00 AlignText: Right
    TextBox txtQuantity2 0 AlignText: Right
    TextBox txtSubTotal2 0.00 AlignText: Right
    TextBox txtItemNumber3    
    TextBox txtItemName3    
    TextBox txtUnitPrice3 0.00 AlignText: Right
    TextBox txtQuantity3 0 AlignText: Right
    TextBox txtSubTotal3 0.00 AlignText: Right
    TextBox txtItemNumber4    
    TextBox txtItemName4    
    TextBox txtUnitPrice4 0.00 AlignText: Right
    TextBox txtQuantity4 0 AlignText: Right
    TextBox txtSubTotal4 0.00 AlignText: Right
    TextBox txtItemNumber5    
    TextBox txtItemName5    
    TextBox txtUnitPrice5 0.00 AlignText: Right
    TextBox txtQuantity5 0 AlignText: Right
    TextBox txtSubTotal5 0.00 AlignText: Right
    TextBox txtItemNumber6    
    TextBox txtItemName6    
    TextBox txtUnitPrice6 0.00 AlignText: Right
    TextBox txtQuantity6 0 AlignText: Right
    TextBox txtSubTotal6 0.00 AlignText: Right
    GroupBox   Order Processing  
    Button btnCalculate Calculate Order  
    Button btnSave Add to Today's Orders  
    Button btnClose Close  
    Label   Total Order:  
    TextBox txtTotalOrder 0.00 AlignText: Right
    Enabled: False
    Form     CancelButton: btnClose
    MaximizeBox: False
    StartPosition: CenterScreen
  16. Double-click the New Item button and implement its Click event as follows:
     
    Private Sub btnNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    		Handles btnNewItem.Click
            Dim frm As NewStoreItem = New NewStoreItem
            frm.ShowDialog()
    End Sub
  17. In the Class Name combo box, select btnCalculate
  18. In the Method Name combo box,, select Click and implement its event as follows:
     
    Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    		Handles btnCalculate.Click
            Dim item1UnitPrice As Decimal = 0.0
            Dim item2UnitPrice As Decimal = 0.0
            Dim item3UnitPrice As Decimal = 0.0
            Dim item4UnitPrice As Decimal = 0.0
            Dim item5UnitPrice As Decimal = 0.0
            Dim item6UnitPrice As Decimal = 0.0
            Dim quantity1 As Integer = 0
            Dim quantity2 As Integer = 0
            Dim quantity3 As Integer = 0
            Dim quantity4 As Integer = 0
            Dim quantity5 As Integer = 0
            Dim quantity6 As Integer = 0
            Dim item1SubTotal As Decimal = 0.0
            Dim item2SubTotal As Decimal = 0.0
            Dim item3SubTotal As Decimal = 0.0
            Dim item4SubTotal As Decimal = 0.0
            Dim item5SubTotal As Decimal = 0.0
            Dim item6SubTotal As Decimal = 0.0
            Dim totalOrder As Decimal
    
            Try
                item1UnitPrice = CDec(Me.txtUnitPrice1.Text)
            Catch ex As FormatException
                MsgBox("Invalid Unit Price")
                Me.txtUnitPrice1.Text = "0.00"
                Me.txtUnitPrice1.Focus()
            End Try
    
            Try
                quantity1 = CInt(Me.txtQuantity1.Text)
    
            Catch ex As FormatException
                MsgBox("Invalid Quantity")
                Me.txtQuantity1.Text = "0"
                Me.txtQuantity1.Focus()
            End Try
    
            Try
    
                item2UnitPrice = CDec(Me.txtUnitPrice2.Text)
            Catch ex As FormatException
                MsgBox("Invalid Unit Price")
                Me.txtUnitPrice2.Text = "0.00"
                Me.txtUnitPrice2.Focus()
            End Try
    
            Try
                quantity2 = CInt(Me.txtQuantity2.Text)
            Catch ex As FormatException
                MsgBox("Invalid Quantity")
                Me.txtQuantity2.Text = "0"
                Me.txtQuantity2.Focus()
            End Try
    
            Try
                item3UnitPrice = CDec(Me.txtUnitPrice3.Text)
            Catch ex As FormatException
                MsgBox("Invalid Unit Price")
                Me.txtUnitPrice3.Text = "0.00"
                Me.txtUnitPrice3.Focus()
            End Try
    
            Try
                quantity3 = CInt(Me.txtQuantity3.Text)
            Catch ex As FormatException
                MsgBox("Invalid Quantity")
                Me.txtQuantity3.Text = "0"
                Me.txtQuantity3.Focus()
            End Try
    
            Try
                item4UnitPrice = CDec(Me.txtUnitPrice4.Text)
            Catch ex As FormatException
                MsgBox("Invalid Unit Price")
                Me.txtUnitPrice4.Text = "0.00"
                Me.txtUnitPrice4.Focus()
            End Try
    
            Try
                quantity4 = CInt(Me.txtQuantity4.Text)
            Catch ex As FormatException
                MsgBox("Invalid Quantity")
                Me.txtQuantity4.Text = "0"
                Me.txtQuantity4.Focus()
            End Try
    
            Try
                item5UnitPrice = CDec(Me.txtUnitPrice5.Text)
            Catch ex As FormatException
                MsgBox("Invalid Unit Price")
                Me.txtUnitPrice5.Text = "0.00"
                Me.txtUnitPrice5.Focus()
            End Try
    
            Try
                quantity5 = CInt(Me.txtQuantity5.Text)
            Catch ex As FormatException
                MsgBox("Invalid Quantity")
                Me.txtQuantity5.Text = "0"
                Me.txtQuantity5.Focus()
            End Try
    
            Try
                item6UnitPrice = CDec(Me.txtUnitPrice6.Text)
            Catch ex As FormatException
                MsgBox("Invalid Unit Price")
                Me.txtUnitPrice6.Text = "0.00"
                Me.txtUnitPrice6.Focus()
            End Try
    
            Try
                quantity6 = CInt(Me.txtQuantity6.Text)
            Catch ex As FormatException
                MsgBox("Invalid Quantity")
                Me.txtQuantity6.Text = "0"
                Me.txtQuantity6.Focus()
            End Try
    
            item1SubTotal = item1UnitPrice * quantity1
            item2SubTotal = item2UnitPrice * quantity2
            item3SubTotal = item3UnitPrice * quantity3
            item4SubTotal = item4UnitPrice * quantity4
            item5SubTotal = item5UnitPrice * quantity5
            item6SubTotal = item6UnitPrice * quantity6
    
            totalOrder = item1SubTotal + item2SubTotal + item3SubTotal + _
                            item4SubTotal + item5SubTotal + item6SubTotal
    
            Me.txtSubTotal1.Text = Format(item1SubTotal, "F")
            Me.txtSubTotal2.Text = Format(item2SubTotal, "F")
            Me.txtSubTotal3.Text = Format(item3SubTotal, "F")
            Me.txtSubTotal4.Text = Format(item4SubTotal, "F")
            Me.txtSubTotal5.Text = Format(item5SubTotal, "F")
            Me.txtSubTotal6.Text = Format(item6SubTotal, "F")
            Me.txtTotalOrder.Text = Format(totalOrder, "F")
    End Sub
  19. Display the first form (Form1.cs (Design)) and design it as follows:
     
    Control Name Text
    Button btnNewItem New Store Item
    Button btnNewOrder New Purchase Order
    Button btnClose Close
  20. double-click the New Store Item button and implement its event as follows:
     
    Private Sub btnNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    	Handles btnNewItem.Click
            Dim frm As NewStoreItem = New NewStoreItem
            frm.ShowDialog()
    End Sub
  21. In the Class Name combo box, select btnNewOrder
  22. In the Method Name combo box, select Click and implement its event as follows:
     
    Private Sub btnNewOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    	Handles btnNewOrder.Click
            Dim frm As PurchaseOrder = New PurchaseOrder
            frm.ShowDialog()
    End Sub
  23. In the Class Name combo box, select btnClose
  24. In the Method Name combo box, select Click
  25. Return to the main form. double-click the Close button and implement its Click event as follows:
     
    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    	Handles btnClose.Click
            End
    End Sub
  26. Save all
 

Application Data Entry

To perform data entry, we will apply the concepts reviewed for data records.

 

Practical Learning Practical Learning: Introducing Data Entry

  1. Display the DataCenter form (DataCenter.vb (Design))
  2. In the Toolbox, click the Data button.
    To create a DataSet, click DataSet and click the DataModule form
     
  3. In the Add Dataset dialog box, click the Untyped Dataset radio button and click OK
  4. While the DataSet control is still selected, in the Properties window, click (Name) and type dsMusicStore
  5. Still in the Properties window, set the DataSetName to MusicStore
  6. Set its Modifiers property to Public so it can be easily accessed from other objects
  7. Click the Tables field to reveal its ellipsis button ellipsis and click the ellipsis button ellipsis
  8. In the Tables Collection Editor, click the Add button
  9. Click (Name) and type tblCategories
  10. Click TableName and type Categories
  11. Set its Modifiers property to Public
  12. Click the Columns field to reveal its ellipsis button ellipsis and click its ellipsis button ellipsis
  13. In the Columns Collection Editor, click the Add button
  14. Change the (Name) to colCategory and change the ColumnName field to Category
  15. Set its Modifiers property to Public
  16. Click Close
  17. In the same way, create a table as
    (Name): tblItemTypes
    TableName: ItemTypes
    Modifiers: Public
  18. Add a column with the following properties:
    (Name): colItemType
    ColumnName: ItemType
    Modifiers: Public

     
  19. In the same way, create a table as
    (Name): tblAvailableItems
    TableName: AvailableItems
    Modifiers: Public
  20. Add the following columns to it
     
    (Name) ColumnName Modifiers
    colItemNumber ItemNumber Public
    colItemCategory Category Public
    colTypeOfItem ItemType Public
    colItemName ItemName Public
    colUnitPrice UnitPrice Public
  21. Click Close twice
  22. Display the Categories form and double-click its Add button
  23. In the top section of the file, type the following:
     
    Imports System.IO
    Imports System.Xml
    Imports System.Data
    
    Public Class Categories
        Inherits System.Windows.Forms.Form
  24. Implement the event as follows:
     
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    		Handles btnAdd.Click
            ' Get a reference to the Data Center because 
            ' that's where the DataSet object resides
            Dim frmData As DataCenter = New DataCenter
            ' This is the XML file that will holds the Categories
            Dim strFilename As String = "Categories.xml"
    
            ' If the file exists already, open it
            If File.Exists(strFilename) Then
                frmData.dsMusicStore.ReadXml(strFilename)
            End If
    
            ' Create a new record for the Categories table
            Dim rowNewCategory As DataRow = frmData.dsMusicStore.Tables("Categories").NewRow()
            ' Specify only the Category column since the CategoryID is auto-incrementing
            rowNewCategory(0) = Me.txtNewCategory.Text
            ' Add the new record to the Categories table
            frmData.dsMusicStore.Tables("Categories").Rows.Add(rowNewCategory)
    
            ' Update the XML file
            frmData.dsMusicStore.WriteXml(strFilename)
    
            ' Display the current records of the Categories table so
            ' the user would know what categories are already in the table
            Me.dataGrid1.DataSource = frmData.dsMusicStore
            Me.dataGrid1.DataMember = "Categories"
    
            ' Reset the text box in case the user wants to add another category
            Me.txtNewCategory.Text = ""
            Me.txtNewCategory.Focus()
    End Sub
  25. Display the ItemTypes form and double-click its Add button
  26. In the top section of the file, type the following:
     
    Imports System.IO
    Imports System.Xml
    Imports System.Data
    
    Public Class ItemTypes
        Inherits System.Windows.Forms.Form
  27. Implement the event as follows:
     
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    		Handles btnAdd.Click
            Dim frmData As DataCenter = New DataCenter
            Dim strFilename As String = "ItemTypes.xml"
    
            If File.Exists(strFilename) Then
                frmData.dsMusicStore.ReadXml(strFilename)
            End If
    
            Dim rowNewType As DataRow = frmData.dsMusicStore.Tables("ItemTypes").NewRow()
            rowNewType(frmData.colItemType) = Me.txtNewItemType.Text
            frmData.dsMusicStore.Tables("ItemTypes").Rows.Add(rowNewType)
    
            frmData.dsMusicStore.WriteXml(strFilename)
    
            Me.dataGrid1.DataSource = frmData.dsMusicStore
            Me.dataGrid1.DataMember = "ItemTypes"
    
            Me.txtNewItemType.Text = ""
            Me.txtNewItemType.Focus()
    End Sub
  28. Save all

Saving the Items and the Orders

Based on the close relationship between the DataSet class and XML, we will save our records as XML.

 

Practical Learning Practical Learning: Opening an XML File

  1. Display the New Store Item form.
    double-click an unoccupied area of its body
  2. In the top section of the file, type the following:
     
    Imports System.IO
    Imports System.Xml
    Imports System.Data
    
    Public Class NewStoreItem
        Inherits System.Windows.Forms.Form
  3. Implement the Load event as follows:
     
    Private Sub NewStoreItem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    			Handles MyBase.Load
            Dim frmData As DataCenter = New DataCenter
            Dim rdrMusicStore As XmlTextReader = Nothing
    
            Try
                Dim strFilename As String = "Categories.xml"
    
                If File.Exists(strFilename) Then
                    rdrMusicStore = New XmlTextReader(strFilename)
    
                    ' Scan the XML file
                    While rdrMusicStore.Read()
                        ' every time you find an element, find out what type it is
                        ' If you find a category, put it in the Categories list box
                        If (XmlNodeType.Element <> 0) And (rdrMusicStore.Name = "Category") Then
                            Me.cboCategories.Items.Add(rdrMusicStore.ReadElementString("Category"))
                        End If
                    End While
                End If
    
                strFilename = "ItemTypes.xml"
    
                If File.Exists(strFilename) Then
                    rdrMusicStore = New XmlTextReader(strFilename)
    
                    ' Scan the XML file
                    While rdrMusicStore.Read()
                        ' every time you find an element, find out what type it is
                        ' If you find an ItemType, put it in the Item Types list box
                        If (XmlNodeType.Element <> 0) And (rdrMusicStore.Name = "ItemType") Then
                            Me.cboItemTypes.Items.Add(rdrMusicStore.ReadElementString("ItemType"))
                        End If
                    End While
                End If
            Catch ex As XmlException
                    MsgBox("Invalid XML file")
                Finally
                If Not (rdrMusicStore Is Nothing) Then rdrMusicStore.Close()
            End Try
    
            ' We will generate a random number for the store item
            Dim tmeNow As DateTime = DateTime.Now
            Dim rndNumber As Random = New Random(tmeNow.Millisecond)
            Dim strNumber As String = CStr(rndNumber.Next(100000, 999999))
    
            ' Display the new number in the Part # text box
            Me.txtItemNumber.Text = strNumber
    
            ' Disable the Create button to indicate that the item is not ready
            Me.btnCreate.Enabled = False
    End Sub	
  4. In the Class Name combo box, select btnNewCategory
  5. In the Method Name combo box, select Click
  6. Implement the event as follows:
     
    Private Sub btnNewCategory_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    			Handles btnNewCategory.Click
            Dim frmCat As Categories = New Categories
    
            frmCat.ShowDialog()
    
            Dim rdrMusicStore As XmlTextReader = Nothing
    
            Try
                Dim strFilename As String = "Categories.xml"
    
                If File.Exists(strFilename) Then
                    Me.cboCategories.Items.Clear()
    
                    rdrMusicStore = New XmlTextReader(strFilename)
    
                    ' Scan the XML file
                    While rdrMusicStore.Read()
                        ' every time you find an element, find out what type it is
                        ' If you find a category, put it in the Categories list box
                        If (XmlNodeType.Element <> Nothing) And (rdrMusicStore.Name = "Category") Then
                            Dim strNew As String = rdrMusicStore.ReadElementString("Category")
                            If Not (Me.cboCategories.Items.Contains(strNew)) Then
                                Me.cboCategories.Items.Add(strNew)
                            End If
                        End If
                    End While
                End If
            Catch ex As XmlException
                MsgBox("Invalid XML file")
            Finally
                If Not (rdrMusicStore Is Nothing) Then
                    rdrMusicStore.Close()
                End If
    	 end try
    End Sub
  7. In the Class Name combo box, select btnNewType
  8. In the Method Name combo box, select Click
  9. Implement the event as follows:
     
    Private Sub btnNewType_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    		Handles btnNewType.Click
            Dim frmTypes As ItemTypes = New ItemTypes
    
            frmTypes.ShowDialog()
    
            Dim rdrMusicStore As XmlTextReader = Nothing
    
            Try
                Dim strFilename As String = "ItemTypes.xml"
    
                If File.Exists(strFilename) Then
                    rdrMusicStore = New XmlTextReader(strFilename)
    
                    While rdrMusicStore.Read()
                        If (XmlNodeType.Element <> 0) And (rdrMusicStore.Name = "ItemType") Then
                            Dim strNewType As String = rdrMusicStore.ReadElementString("ItemType")
                            If Not Me.cboItemTypes.Items.Contains(strNewType) Then
                                Me.cboItemTypes.Items.Add(strNewType)
                            End If
                        End If
                    End While
                End If
            Catch ex As XmlException
                MsgBox("Invalid XML file")
            Finally
                If Not (rdrMusicStore Is Nothing) Then
                    rdrMusicStore.Close()
                End If
            End Try
    End Sub
  10. In the Class Name combo box, select txtItemName
  11. In the Method Name combo box, select TextChanged and implement its event as follows:
     
    Private Sub txtItemName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
    		Handles txtItemName.TextChanged
            If Me.txtItemName.Text = "" Then
                Me.btnCreate.Enabled = False
            Else
                Me.btnCreate.Enabled = True
            End If
    End Sub
  12. In the Class Name combo box, select txtUnitPrice
  13. In the Method Name combo box, select TextChanged and implement its event as follows:
     
    Private Sub txtUnitPrice_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
    		Handles txtUnitPrice.TextChanged
            If Me.txtUnitPrice.Text = "" Then
                Me.btnCreate.Enabled = False
            Else
                Me.btnCreate.Enabled = True
            End If
    End Sub
  14. In the Class Name combo box, select txtItemNumber
  15. In the Method Name combo box, select TextChanged and implement its event as follows:
     
    Private Sub txtItemNumber_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
    		Handles txtItemNumber.TextChanged
            If Me.txtItemNumber.Text = "" Then
                Me.btnCreate.Enabled = False
            Else
                Me.btnCreate.Enabled = True
            End If
    End Sub
  16. In the Class Name combo box, select btnCreate
  17. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    			Handles btnCreate.Click
            ' Get a reference to the Data Center because 
            ' that's where the DataSet object resides
            Dim frmData As DataCenter = New DataCenter
    
            ' This is the XML file that will holds the general inventory
            ' of the current items in the store
            Dim strAvailableItems As String = "Inventory.xml"
    
            ' If the file exists already, open it
            If File.Exists(strAvailableItems) Then
                frmData.dsMusicStore.ReadXml(strAvailableItems)
            End If
    
            ' Create a new record for the AvailableItems table
            Dim rowNewItem As DataRow = frmData.dsMusicStore.Tables("AvailableItems").NewRow()
    
            rowNewItem("ItemNumber") = Me.txtItemNumber.Text
            rowNewItem("Category") = Me.cboCategories.Text
            rowNewItem("ItemType") = Me.cboItemTypes.Text
            rowNewItem("ItemName") = Me.txtItemName.Text
            rowNewItem("UnitPrice") = Me.txtUnitPrice.Text
            ' Add the new record to the AvailableItems table
            frmData.dsMusicStore.Tables("AvailableItems").Rows.Add(rowNewItem)
    
            ' Update the XML file
            frmData.dsMusicStore.WriteXml(strAvailableItems)
    
            ' Reset the controls in case the user wants to add another record
            Me.cboCategories.SelectedIndex = -1
            Me.cboItemTypes.SelectedIndex = -1
            Me.txtItemName.Text = ""
            Me.txtUnitPrice.Text = "0.00"
    
            ' We will generate a random number for the store item
            Dim tmeNow As DateTime = DateTime.Now
            Dim rndNumber As Random = New Random(tmeNow.Millisecond)
            Dim strNumber As String = CStr(rndNumber.Next(100000, 999999))
    
            ' Display the new number in the Part # text box
            Me.txtItemNumber.Text = strNumber
    
            ' Disable the OK button to indicate that the item is not ready
            Me.btnCreate.Enabled = False
    End Sub
  18. Execute the application
  19. Click the New Store Item button
  20. Click the top New button to display the Categories form and create a few categories as follows:
     
    Category
    Electric Guitar
    Acoustic Guitar
    Bass
    Keyboard
    Stands
    Cables
    Cases
    DJ Equipment
  21. Close the Categories form and open the Item Types form
  22. Create a few item types as follows:
     
    Item Type
    4-String
    5-String
    6-String
    12-String
    Solid Body
    Hollow Body
    Left-Handed
    Guitar Stand
    DJ CD Players
  23. Close the Item Types form
  24. In the New Store Item form, create the following items (let the computer generate item numbers):
     
    Category Item Type Item Name Unit Price
    Electric Guitar Solid Body Gibson Les Paul Standard Electric Guitar 1950.00
    Bass 6-String Ibanez SR506 6-string Electric Bass 595.50
    Electric Guitar Hollow Body Oscar Schmidt OE40 Hollow Body Electric Guitar 205.95
    Acoustic Guitar Left-Handed Yamaha Left-Handed FG413SL Acoustic Guitar 295.95
    Cables Instrument Cable Monster Cable S-100 Straight Instrument Cable 12.95
    Bass Combo Amps Behringer BX1200 Ultrabass Amplifier (120 Watts, 2-Channel) 150.00
    Keyboard Synthesizers Korg Triton Le 61-Key Workstation Synth 895.95
    Stands Guitar Stand string Swing Metal Guitar Wall Hanger 9.95
    Electric Guitar Solid Body Epiphone LP-100 Electric Guitar 275.95
    Keyboard Digital Piano Yamaha YDP223 88-Key Graded Hammer Piano With Bench 1490.00
    Cables Guitar Cable Spectraflex Guitar Cable 42.25
    Electric Guitar Solid Body Gibson Les Paul Classic Electric Guitar 1625.95
    Stands Guitar Stand Locking Tubular Guitar Stand 4.95
    Electric Guitar Solid Body ESP LTD Viper 400 Electric Guitar 585.50
    Acoustic Guitar 12-String Washburn J28S12DL Cumberland 12-string Guitar 650.75
    Electric Guitar Hollow Body Ibanez Artcore AF75 Electric Guitar 315.95
    Keyboard Synthesizers Korg Triton Extreme 61-Key Synth Workstation 1895.00
    Electric Guitar Solid Body Epiphone Les Paul Standard 525.95
    Stands Guitar Stand Guitar Folding Stand 12.95
    Electric Guitar Hollow Body Gibson ES-335 Reissue 1850.00
    Acoustic Guitar 12-String Martin DM12 850.00
    Stands Guitar Stand Metal Guitar Wall Hanger 9.95
    Acoustic Guitar Left Handed Yamaha FG413SL 295.95
    Electric Guitar Solid Body Gibson Faded SG Special 625.95
    Bass 4-String Ibanez SR500 525.95
    Electric Guitar Hollow Body Oscar Schmidt OE40 225.95
    Electric Guitar Left Handed Schecher C-1 450.95
    Cases Cases Les Paul Hardshell Case 39.95
    Cables Instrument Cable Hosa Dual Instrument Cable 7.25
  25. Close the forms and return to your programming environment
  26. Display the Purchase Order form and double-click an unoccupied area of its body away from (outside of) any group box
  27. In the top section of the file, type the following:
     
    Imports System.IO
    Imports System.Xml
    Imports System.Data
    
    Public Class PurchaseOrder
        Inherits System.Windows.Forms.Form
  28. Implement the Load event as follows:
     
    Private Sub PurchaseOrder_Load(ByVal sender As System.Object, _
    	ByVal e As System.EventArgs) Handles MyBase.Load
            ' Get a reference to the object that holds the DataSet
            Dim frmData As DataCenter = New DataCenter
            ' Identify the file that holds the categories
            Dim strCategories As String = "Categories.xml"
    
            ' If that file exists, then open it
            If File.Exists(strCategories) Then
                frmData.dsMusicStore.ReadXml(strCategories)
    
                ' Just in case, empty the Categories list box
                Me.lbxCategories.Items.Clear()
    
                Dim row As DataRow = Nothing
                Dim i As Integer
                ' Scan the whole file to locate each category and retrieve it
                For i = 0 To frmData.tblCategories.Rows.Count - 1 Step 1
                    row = frmData.tblCategories.Rows(i)
                    Me.lbxCategories.Items.Add(row("Category"))
                Next
            End If
    End Sub
  29. In the Class Name combo box, select lbxCategories
  30. In the Method Name combo box, select SelectedIndexChanged and implement its event as follows:
     
    Private Sub lbxCategories_SelectedIndexChanged(ByVal sender As Object, _
    	ByVal e As System.EventArgs) Handles lbxCategories.SelectedIndexChanged
            ' Get a reference to the object that holds the DataSet
            Dim frmData As DataCenter = New DataCenter
            ' Identify the file that holds the items
            Dim strAvailableItems As String = "Inventory.xml"
            ' Identify the category that the user selected
            Dim strSelectedCategory As String = Me.lbxCategories.Text
    
            ' If that file exists, then open it
            If File.Exists(strAvailableItems) Then
                frmData.dsMusicStore.ReadXml(strAvailableItems)
            End If
    
            ' Empty the Item Types list box
            Me.lbxItemTypes.Items.Clear()
            ' Also empty the Available Items list box
            Me.lbxAvailableItems.Items.Clear()
    
            Dim row As DataRow = Nothing
            Dim i As Integer
            ' Scan the whole table to locate each record
            For i = 0 To frmData.tblAvailableItems.Rows.Count - 1 Step 1
                ' Get a reference to the current record
                row = frmData.tblAvailableItems.Rows(i)
                ' Get the name of the category of the current record
                Dim strCurrentCategory As String = row("Category")
    
                ' If the current category matches the one the user selected...
                If strCurrentCategory = strSelectedCategory Then
                    ' ... then get the corresponding item type
                    Dim strType As String = row("ItemType")
    
                    ' Find out if the Item Types list box already contains the item type
                    ' If it doesn't, then put it in the Item Types list box
                    If Not (Me.lbxItemTypes.Items.Contains(strType)) Then
                        Me.lbxItemTypes.Items.Add(strType)
                    End If
                End If
            Next
    End Sub
  31. In the Class Name combo box, select lbxItemTypes
  32. In the Method Name combo box, select SelectedIndexChanged
  33. Implement the event as follows:
     
    Private Sub lbxItemTypes_SelectedIndexChanged(ByVal sender As Object, _
    		ByVal e As System.EventArgs) Handles lbxItemTypes.SelectedIndexChanged
            ' Get a reference to the object that holds the DataSet
            Dim frmData As DataCenter = New DataCenter
            ' Identify the file that holds the items
            Dim strAvailableItems As String = "Inventory.xml"
            ' Identify the category that the user selected
            Dim strSelectedCategory As String = Me.lbxCategories.Text
            Dim strSelectedType As String = Me.lbxItemTypes.Text
    
            ' If file exists, then open it
            If File.Exists(strAvailableItems) Then
                frmData.dsMusicStore.ReadXml(strAvailableItems)
            End If
            ' Empty the Available Items list box
            Me.lbxAvailableItems.Items.Clear()
    
            Dim row As DataRow = Nothing
            Dim i As Integer
            ' Scan the whole table to locate each record
            For i = 0 To frmData.tblAvailableItems.Rows.Count - 1 Step 1
                ' Get a reference to the current record
                row = frmData.tblAvailableItems.Rows(i)
                ' Get the name of the category of the current record
                Dim strCurrentCategory As String = row("Category")
                ' Get the item type of the current record
                Dim strCurrentType As String = row("ItemType")
    
                ' If the current category matches the one the user selected
                ' and the current item type matches the type the user selected ...
                If (strCurrentCategory = strSelectedCategory) And (strCurrentType = strSelectedType) Then
                    ' ... then get the corresponding item name
                    Me.lbxAvailableItems.Items.Add(row("ItemName"))
                End If
            Next
    End Sub
  34. In the Class Name combo box, select lbxAvailableItems
  35. In the Method Name combo box, select DoubleClick
  36. Implement the DoubleClick event as follows:
     
    Private Sub lbxAvailableItems_DoubleClick(ByVal sender As Object, _
    		ByVal e As System.EventArgs) Handles lbxAvailableItems.DoubleClick
            ' Get a reference to the object that holds the DataSet
            Dim frmData As DataCenter = New DataCenter
            ' Identify the file that holds the items
            Dim strAvailableItems As String = "Inventory.xml"
            ' Identify the item that the user selected
            Dim strSelectedCategory As String = Me.lbxCategories.Text
            Dim strSelectedType As String = Me.lbxItemTypes.Text
            Dim strSelectedName As String = Me.lbxAvailableItems.Text
    
            ' If file exists, then open it
            If File.Exists(strAvailableItems) Then
                frmData.dsMusicStore.ReadXml(strAvailableItems)
    
                Dim row As DataRow = Nothing
                ' Scan the whole table to locate each record
                Dim i As Integer
                For i = 0 To frmData.tblAvailableItems.Rows.Count - 1 Step 1
                    ' Get a reference to the current record
                    row = frmData.tblAvailableItems.Rows(i)
                    ' Get the name of the category of the current record
                    Dim strCurrentCategory As String = row("Category")
                    ' Get the item type of the current record
                    Dim strCurrentType As String = row("ItemType")
                    ' Get the name of the current item
                    Dim strCurrentName As String = row("ItemName")
                    ' Get the item number of the current item
                    Dim strCurrentNumber As String = row("ItemNumber")
                    ' Get the unit price of the current item
                    Dim strCurrentPrice As String = row("UnitPrice")
    
                    ' If the current parts match the ones the user selected ...
                    If (strCurrentCategory = strSelectedCategory) And _
                       (strCurrentType = strSelectedType) And _
                        (strCurrentName = strSelectedName) Then
                        ' ... then consider the corresponding item
                        If Me.txtItemNumber1.Text = "" Then
                            Me.txtItemNumber1.Text = strCurrentNumber
                            Me.txtItemName1.Text = strCurrentName
                            Me.txtUnitPrice1.Text = strCurrentPrice
                            Me.txtQuantity1.Text = "1"
                            Me.txtSubTotal1.Text = strCurrentPrice
                        ElseIf Me.txtItemNumber2.Text = "" Then
                            Me.txtItemNumber2.Text = strCurrentNumber
                            Me.txtItemName2.Text = strCurrentName
                            Me.txtUnitPrice2.Text = strCurrentPrice
                            Me.txtQuantity2.Text = "1"
                            Me.txtSubTotal2.Text = strCurrentPrice
                        ElseIf Me.txtItemNumber3.Text = "" Then
                            Me.txtItemNumber3.Text = strCurrentNumber
                            Me.txtItemName3.Text = strCurrentName
                            Me.txtUnitPrice3.Text = strCurrentPrice
                            Me.txtQuantity3.Text = "1"
                            Me.txtSubTotal3.Text = strCurrentPrice
                        ElseIf Me.txtItemNumber4.Text = "" Then
                            Me.txtItemNumber4.Text = strCurrentNumber
                            Me.txtItemName4.Text = strCurrentName
                            Me.txtUnitPrice4.Text = strCurrentPrice
                            Me.txtQuantity4.Text = "1"
                            Me.txtSubTotal4.Text = strCurrentPrice
                        ElseIf Me.txtItemNumber5.Text = "" Then
                            Me.txtItemNumber5.Text = strCurrentNumber
                            Me.txtItemName5.Text = strCurrentName
                            Me.txtUnitPrice5.Text = strCurrentPrice
                            Me.txtQuantity5.Text = "1"
                            Me.txtSubTotal5.Text = strCurrentPrice
                        Else ' if me.txtItemNumber6.Text= "" then
                            Me.txtItemNumber6.Text = strCurrentNumber
                            Me.txtItemName6.Text = strCurrentName
                            Me.txtUnitPrice6.Text = strCurrentPrice
                            Me.txtQuantity6.Text = "1"
                            Me.txtSubTotal6.Text = strCurrentPrice
                        End If
                    End If
                Next
            End If
    
            Me.btnCalculate_Click(sender, e)
    End Sub
  37. Execute the application and perform a few transactions
 

Home Copyright © 2005-2012 FunctionX