Files
ayanova7/API/VBCreatePart/Form1.vb
2018-06-29 19:47:36 +00:00

300 lines
12 KiB
VB.net

'The AyaNova business object library
Imports GZTW.AyaNova.BLL
'The following two imports are used only to process a login
'Typically they will only be required for the form where you process the login
'Once a user is logged in that is active until the application closes
Imports CSLA.Security
Imports System.Threading
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents cbSuppliers As System.Windows.Forms.ComboBox
Friend WithEvents lblSupplier As System.Windows.Forms.Label
Friend WithEvents edPartNumber As System.Windows.Forms.TextBox
Friend WithEvents lblPartNumber As System.Windows.Forms.Label
Friend WithEvents btnAdd As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.cbSuppliers = New System.Windows.Forms.ComboBox
Me.lblSupplier = New System.Windows.Forms.Label
Me.edPartNumber = New System.Windows.Forms.TextBox
Me.lblPartNumber = New System.Windows.Forms.Label
Me.btnAdd = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'cbSuppliers
'
Me.cbSuppliers.Location = New System.Drawing.Point(17, 45)
Me.cbSuppliers.Name = "cbSuppliers"
Me.cbSuppliers.Size = New System.Drawing.Size(292, 21)
Me.cbSuppliers.Sorted = True
Me.cbSuppliers.TabIndex = 0
'
'lblSupplier
'
Me.lblSupplier.Location = New System.Drawing.Point(17, 22)
Me.lblSupplier.Name = "lblSupplier"
Me.lblSupplier.Size = New System.Drawing.Size(292, 23)
Me.lblSupplier.TabIndex = 1
Me.lblSupplier.Text = "Supplier:"
'
'edPartNumber
'
Me.edPartNumber.Location = New System.Drawing.Point(17, 117)
Me.edPartNumber.Name = "edPartNumber"
Me.edPartNumber.Size = New System.Drawing.Size(292, 20)
Me.edPartNumber.TabIndex = 2
Me.edPartNumber.Text = ""
'
'lblPartNumber
'
Me.lblPartNumber.Location = New System.Drawing.Point(17, 89)
Me.lblPartNumber.Name = "lblPartNumber"
Me.lblPartNumber.Size = New System.Drawing.Size(292, 23)
Me.lblPartNumber.TabIndex = 3
Me.lblPartNumber.Text = "Part number:"
'
'btnAdd
'
Me.btnAdd.Location = New System.Drawing.Point(218, 194)
Me.btnAdd.Name = "btnAdd"
Me.btnAdd.TabIndex = 4
Me.btnAdd.Text = "Add part"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(325, 237)
Me.Controls.Add(Me.btnAdd)
Me.Controls.Add(Me.lblPartNumber)
Me.Controls.Add(Me.edPartNumber)
Me.Controls.Add(Me.lblSupplier)
Me.Controls.Add(Me.cbSuppliers)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
Me.Name = "Form1"
Me.Text = "Add part sample"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'THIS IS AN EXAMPLE ONLY, YOU APPLICATION SHOULD IMPLEMENT
'ERROR HANDLING AND RECOVERY AND CHECK FOR EXCEPTIONS
'The call to Initialize is always the first required in any
'application working with AyaNova, it initializes the database
'connection and business object framework
'IMPORTANT: If you receive an exception when calling the Initialize method it is most likely
'a connection string problem in your application configuration file causing the
'database provider to throw a not found exception. Or a problem with the configuration
'file itself like it's missing or not configured properly.
'Remember that in the case of any exception using the AyaNova business object library
'it is that exceptions Inner exception which is most likely the actual error (if present)
'because AyaNova objects are invoked through a factory method which wraps any actual exception
'generated by the business object inside a TargeInvocationException
GZTW.AyaNova.BLL.AyaBizUtils.Initialize()
'Once your application gets to this point right after Initialize without error
'you can be assured that you have a clean configuration file and a connection
'to the database and are ready to log in
'this is the default AyaNova trial login and password
'and this is how you login to AyaNova
'typically you would, of course, use a dialog and prompt
'the user for the login and password
AyaBizUtils.Login("manager", "letmein")
'if a login fails no operations can take place with AyaNova
'so let's confirm the login was sucessful:
If Thread.CurrentPrincipal.Identity.IsAuthenticated.Equals(False) Then
'Nope, they are not authenticated so
'show and error and bail out
'Or more typically re-prompt to login
MessageBox.Show("Login failed")
Application.Exit()
Return
End If
'At this point the user is logged in and
'you can now work with any AyaNova business objects that
'the login account used above has rights to.
'Logging OUT: There is technically no need to logout
'Let's just make sure the database hasn't expired,
'an expired AyaNova database is read only so
'the example code will not work if that's true.
'AyaBizUtils handles all manner of general operations
'that aren't related to a particular object
If AyaBizUtils.Expired Then
MessageBox.Show("Sorry the AyaNova database has an expired license: " & AyaBizUtils.ExpiryDate.ToString())
Application.Exit()
Return
End If
'Populate the list of vendors
'It's not absolutely necessary to set a vendor when creating
'a part, only the Number field is mandatory, but it will
'show how to work with a pick list
PopulateVendorList()
End Sub
'Populate the list of vendors on the main form
'using an AyaNova read only collection object
Private Sub PopulateVendorList()
'Declare a VendorPick list and retrieve it from the database:
Dim vendorlist As VendorPickList = VendorPickList.GetList()
'Declare an info object to contain each item in the list
'temporarily as it's iterated over in the For each loop below
Dim vendorInfo As VendorPickList.VendorPickListInfo
'Tell the combo to use the Name field for displaying
Me.cbSuppliers.DisplayMember = "Name"
'Tell the combo to use the ID field for the actual value
Me.cbSuppliers.ValueMember = "ID"
'Iterate over the list of vendors
'and add each active one that is a wholesaler to the combo box
For Each vendorInfo In vendorlist
If vendorInfo.Active = True And vendorInfo.VendorType = VendorTypes.Wholesaler Then
Me.cbSuppliers.Items.Add(vendorInfo)
End If
Next
End Sub
'Create a new part in the AyaNova database based
'on the users selection of Wholesaler (supplier) and
'the part number
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
'A guid value to hold the selected supplier's ID value
Dim vendorInfo As VendorPickList.VendorPickListInfo
'A Part object
Dim p As Part
'Create a new part object
'Note that it's not saved to database until the save method is called
'so it's in memory only for now.
'when you call newitem an ID is automatically generated for the new object
p = Part.NewItem()
'Let's save the ID of the new part so we can retrieve it later to prove
'that it was really saved to the database:
Dim NewPartID As Guid
NewPartID = p.ID
'Set the required part number field
p.PartNumber = Me.edPartNumber.Text
'Let's set some other values to just for the heck of it
p.Retail = 19.95
p.Cost = 10.5
p.Notes = "This part was created using the API sample VBCreatepart"
'At this point we can just save it, all that is absolutely required
'is the part number, but let's set it's wholesaler to the selected value
'in the combo box
'Get the selected vendorInfo object if present
If Me.cbSuppliers.SelectedItem Is Nothing Then
p.WholesalerID = Guid.Empty
Else
p.WholesalerID = CType(Me.cbSuppliers.SelectedItem(), VendorPickList.VendorPickListInfo).ID
End If
'Lets make sure the part can be saved and is valid
'Each business object implements it's own business rules
'and decides if an object is valid or not and will not let an
'object be saved if it's not valid.
'to test this out try leaving the part number out and saving the part
'(note that the message you get is a localized text string which will be covered in other examples)
If Not p.IsSavable Then
MessageBox.Show("The part is not valid and can't be saved" & vbCrLf & "It has one or more broken rules:" & vbCrLf & vbCrLf & p.GetBrokenRulesString())
Return
End If
'Now save the part to the database
p.Save()
'Note that save always returns an object when called
'the object returned is the new and valid object and the original,
'once saved is no longer valid.
'This means that if you wanted to continue to work with the part object
'as in the example above you would call the save method like this instead:
'p = CType(p.Save(), Part)
'In any case where you want to save an AyaNova object but keep working with it remember that
'you will need to set the variable you use for that object by casting the returned object
'from the save method to that type of object
'lets show that it worked:
Dim MyPart As Part
MyPart = Part.GetItem(NewPartID)
MessageBox.Show("Part " & MyPart.PartNumber & " created sucessfully @ " & MyPart.Created.ToString())
'If you run AyaNova now pointed to the same database used for this example
'you will see and be able to use the new part you just created
End Sub
End Class