Advertisement
JayBeeOH

Non Database Demo

Jul 20th, 2016
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.65 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2016. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Non Database Demo
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   20 JUL 2016
  16. '
  17. ' Description:    A non-database Contact Maintenance Demo using the BindingList(of T)
  18. '                 with a DataGridView and file serialization to persist data. Demo shows
  19. '                 how to do both binary and XML file I/O processing.
  20. '
  21. '                 Documentation is at:
  22. '                   App's screen image is at: http://imgur.com/lhW13gs
  23. '                   App's Visual Basic .NET code is at http://pastebin.com/76e6VwXN
  24. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  25. '-------------------------------------------------------------------------------------------
  26.  
  27. Imports System.ComponentModel
  28. Imports System.IO
  29. Imports System.Runtime.Serialization.Formatters.Binary
  30. Imports System.Security
  31. Imports System.Xml.Serialization
  32.  
  33. Public Class MainForm
  34.  
  35. #Region "     Class Level Variables Instantiated"
  36.  
  37.     Const BIN_DATA_FILE As String = "contacts.bin"
  38.     Const XML_DATA_FILE As String = "contacts.xml"
  39.     Private contacts As New BindingList(Of Contact)
  40.     Private highWater As Integer
  41.     Private binSwitch As Boolean = True
  42.  
  43. #End Region
  44.  
  45. #Region "     Form Events"
  46.  
  47.     ' Perform initialization and housekeeping activities prior to showing form.
  48.     Private Sub MainForm_Load(sender As Object, e As EventArgs) _
  49.         Handles MyBase.Load
  50.  
  51.         'Bold DataGrid Column Headers.
  52.         ContactDataGridView.ColumnHeadersDefaultCellStyle.Font =
  53.             New Font(ContactDataGridView.ColumnHeadersDefaultCellStyle.Font, FontStyle.Bold)
  54.  
  55.         ' Load contacts from binary or XML file.
  56.         If binSwitch Then
  57.             BinLoadContacts()
  58.         Else
  59.             XmlLoadContacts()
  60.         End If
  61.  
  62.         ' Assign contacts BindingList to Binding Source.
  63.         ContactBindingSource.DataSource = contacts
  64.  
  65.         ' Get highest ContactId value in contacts
  66.         For Each item In contacts
  67.             If highWater < item.ContactId Then
  68.                 highWater = item.ContactId
  69.             End If
  70.         Next
  71.     End Sub
  72.  
  73.     ' Before ending program, save contacts to binary file.
  74.     Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) _
  75.         Handles Me.FormClosing
  76.  
  77.         ' Save contacts to binary or XML file.
  78.         If binSwitch Then
  79.             BinSaveContacts()
  80.         Else
  81.             XmlSaveContacts()
  82.         End If
  83.     End Sub
  84.  
  85.     ' Validate and save changes within DataGridView.
  86.     Private Sub ContactBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
  87.         Handles ContactBindingNavigatorSaveItem.Click
  88.  
  89.         Validate()
  90.  
  91.         ' Update ContactId for new records.
  92.         For Each newContact In contacts.Where(Function(c) c.ContactId = 0)
  93.             highWater += 1
  94.             newContact.ContactId = highWater
  95.         Next
  96.  
  97.         ContactBindingSource.EndEdit()
  98.  
  99.         ' Refresh DataGridView after edits.
  100.         ContactBindingSource.ResetBindings(False)
  101.     End Sub
  102.  
  103.     ' Validate that all cells in DataGrid meet requirements.
  104.     Private Sub ContactDataGridView_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) _
  105.         Handles ContactDataGridView.CellValidating
  106.  
  107.         ' Validate that LastName is not empty.
  108.         If ContactDataGridView.Columns(e.ColumnIndex).DataPropertyName = "LastName" Then
  109.             If (String.IsNullOrWhiteSpace(e.FormattedValue.ToString())) Then
  110.                 ContactDataGridView.Rows(e.RowIndex).ErrorText =
  111.                                  "Last Name is a required value."
  112.                 e.Cancel = True
  113.             End If
  114.         End If
  115.     End Sub
  116.  
  117.     ' Clear the row error in case the user presses ESC key.
  118.     Private Sub ContactDataGridView_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) _
  119.         Handles ContactDataGridView.CellEndEdit
  120.  
  121.         ContactDataGridView.Rows(e.RowIndex).ErrorText = String.Empty
  122.     End Sub
  123.  
  124. #End Region
  125.  
  126. #Region "     File I/O Processing"
  127.  
  128.     ' Load Contacts BindingList from a binary file.
  129.     Private Sub BinLoadContacts()
  130.  
  131.         Try
  132.             Using stream As Stream = File.Open(BIN_DATA_FILE, FileMode.OpenOrCreate)
  133.                 Dim bin As New BinaryFormatter()
  134.  
  135.                 If stream.Length > 0 Then
  136.                     contacts = CType(bin.Deserialize(stream), BindingList(Of Contact))
  137.                 End If
  138.             End Using
  139.         Catch ex As IOException
  140.             MessageBox.Show(ex.Message, "Accessing File Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  141.         Catch ex As SecurityException
  142.             MessageBox.Show(ex.Message, "Security Permissions Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  143.         Catch ex As Exception
  144.             MessageBox.Show(ex.Message, ex.GetType.ToString, MessageBoxButtons.OK, MessageBoxIcon.Error)
  145.         End Try
  146.     End Sub
  147.  
  148.     ' Save Contacts BindingList to a binary file.
  149.     Private Sub BinSaveContacts()
  150.  
  151.         If contacts.Count > 0 Then
  152.  
  153.             Try
  154.                 Using stream As Stream = File.Open(BIN_DATA_FILE, FileMode.Create)
  155.                     Dim bin As New BinaryFormatter()
  156.  
  157.                     bin.Serialize(stream, contacts)
  158.                 End Using
  159.             Catch ex As IOException
  160.                 MessageBox.Show(ex.Message, "Accessing File Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  161.             Catch ex As SecurityException
  162.                 MessageBox.Show(ex.Message, "Security Permissions Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  163.             Catch ex As Exception
  164.                 MessageBox.Show(ex.Message, ex.GetType.ToString, MessageBoxButtons.OK, MessageBoxIcon.Error)
  165.             End Try
  166.         End If
  167.     End Sub
  168.  
  169.     ' Load Contacts BindingList from a XML file.
  170.     Private Sub XmlLoadContacts()
  171.  
  172.         Try
  173.             Using stream As Stream = File.Open(XML_DATA_FILE, FileMode.OpenOrCreate)
  174.                 Dim xml As New XmlSerializer(contacts.GetType)
  175.  
  176.                 If stream.Length > 0 Then
  177.                     contacts = CType(xml.Deserialize(stream), BindingList(Of Contact))
  178.                 End If
  179.             End Using
  180.         Catch ex As IOException
  181.             MessageBox.Show(ex.Message, "Accessing File Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  182.         Catch ex As SecurityException
  183.             MessageBox.Show(ex.Message, "Security Permissions Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  184.         Catch ex As Exception
  185.             MessageBox.Show(ex.Message, ex.GetType.ToString, MessageBoxButtons.OK, MessageBoxIcon.Error)
  186.         End Try
  187.     End Sub
  188.  
  189.     ' Save Contacts BindingList to a XML file.
  190.     Private Sub XmlSaveContacts()
  191.  
  192.         If contacts.Count > 0 Then
  193.  
  194.             Try
  195.                 Using stream As Stream = File.Open(XML_DATA_FILE, FileMode.Create)
  196.                     Dim xml As New XmlSerializer(contacts.GetType)
  197.  
  198.                     xml.Serialize(stream, contacts)
  199.                 End Using
  200.             Catch ex As IOException
  201.                 MessageBox.Show(ex.Message, "Accessing File Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  202.             Catch ex As SecurityException
  203.                 MessageBox.Show(ex.Message, "Security Permissions Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  204.             Catch ex As Exception
  205.                 MessageBox.Show(ex.Message, ex.GetType.ToString, MessageBoxButtons.OK, MessageBoxIcon.Error)
  206.             End Try
  207.         End If
  208.     End Sub
  209.  
  210. #End Region
  211.  
  212. End Class
  213.  
  214. '-------------------------------------------------------------------------------------------
  215. ' Class Name:     Contact
  216. '
  217. ' Author:         Joseph L. Bolen
  218. ' Date Created:   20 JUL 2016
  219. '
  220. ' Description:    Simple auto-implemented properties for a Contacts list with a default and
  221. '                 one parameterized constructor.
  222. '-------------------------------------------------------------------------------------------
  223.  
  224. <Serializable>
  225. Public Class Contact
  226.  
  227.     Public Property ContactId As Integer
  228.     Public Property FirstName As String
  229.     Public Property MiddleName As String
  230.     Public Property LastName As String
  231.     Public Property StreetAddress As String
  232.     Public Property ExtendedAddress As String
  233.     Public Property City As String
  234.     Public Property State As String
  235.     Public Property PostalCode As String
  236.     Public Property CountryCode As String
  237.     Public Property PrimaryTelephone As String
  238.     Public Property SecondaryTelephone As String
  239.     Public Property DateOfBirth As Nullable(Of Date)
  240.     Public Property Gender As String
  241.     Public Property Notes As String
  242.  
  243.     ' Default Constructor
  244.     Public Sub New()
  245.         MyBase.New
  246.     End Sub
  247.  
  248.     ' Parameterized Constructor
  249.     Public Sub New(ByVal id As Integer, ByVal fName As String, ByVal lName As String)
  250.  
  251.         MyBase.New
  252.         ContactId = id
  253.         FirstName = fName
  254.         LastName = lName
  255.     End Sub
  256.  
  257. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement