JayBeeOH

ADDnet Master / Detail Maintenance

Dec 2nd, 2016
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.67 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:   ADOnet Master / Detail Maintenance
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   30 NOV 2016
  16. '
  17. ' Description:    Demostrate how to use the ADO.NET with Windows Forms
  18. '                 in a Master / Detail maintenance program.
  19. '
  20. '                 Documentation is at:
  21. '                   App's screen image is at: http://imgur.com/cB5ZUAw
  22. '                   App's Visual Basic .NET code is at: http://pastebin.com/iGDWxFYM
  23. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  24. '
  25. '-------------------------------------------------------------------------------------------
  26.  
  27. Imports System.Data.SqlClient
  28.  
  29. Public Class MainForm
  30.  
  31.     ' Do intial housekeeping activites and get the data before displaying the form.
  32.     Private Sub MainForm_Load(sender As Object, e As EventArgs) _
  33.         Handles MyBase.Load
  34.  
  35.         ' Make DataGridView ColumnHeader Bold
  36.         OrdersDataGridView.ColumnHeadersDefaultCellStyle.Font =
  37.             New Font(OrdersDataGridView.ColumnHeadersDefaultCellStyle.Font, FontStyle.Bold)
  38.  
  39.         Try
  40.             ' Retrieve for table data from the database and place it in
  41.             ' strongly typed datatables. Load master before detail.
  42.             Me.CustomersTableAdapter.Fill(Me.SampleDBDataSet.Customers)
  43.             Me.OrdersTableAdapter.Fill(Me.SampleDBDataSet.Orders)
  44.  
  45.         Catch ex As SqlException
  46.             MessageBox.Show("Database error # " & ex.Number & ": " & ex.Message,
  47.                             ex.GetType.ToString,
  48.                             MessageBoxButtons.OK,
  49.                             MessageBoxIcon.Error)
  50.  
  51.         Catch ex As Exception
  52.             MessageBox.Show(ex.GetType.ToString & ControlChars.NewLine &
  53.                         ControlChars.NewLine & ex.Message,
  54.                         Me.Text,
  55.                         MessageBoxButtons.OK,
  56.                         MessageBoxIcon.Error)
  57.         End Try
  58.     End Sub
  59.  
  60.     ' Validate, commit changes, then update the database.
  61.     Private Sub CustomersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
  62.         Handles CustomersBindingNavigatorSaveItem.Click
  63.  
  64.         Try
  65.             Me.Validate()
  66.             Me.CustomersBindingSource.EndEdit()
  67.             Me.OrdersBindingSource.EndEdit()
  68.  
  69.             Me.TableAdapterManager.UpdateAll(Me.SampleDBDataSet)
  70.  
  71.         Catch ex As DBConcurrencyException
  72.             MessageBox.Show("A concurrency error occurred. Data will be refreshed.",
  73.                             "Concurrency Error",
  74.                             MessageBoxButtons.OK,
  75.                             MessageBoxIcon.Error)
  76.             Me.CustomersTableAdapter.Fill(Me.SampleDBDataSet.Customers)
  77.             Me.OrdersTableAdapter.Fill(Me.SampleDBDataSet.Orders)
  78.  
  79.         Catch ex As DataException
  80.             MessageBox.Show(ex.Message,
  81.                             ex.GetType.ToString,
  82.                             MessageBoxButtons.OK,
  83.                             MessageBoxIcon.Error)
  84.             Me.CustomersBindingSource.CancelEdit()
  85.             Me.OrdersBindingSource.CancelEdit()
  86.  
  87.         Catch ex As SqlException
  88.             MessageBox.Show("Database error # " & ex.Number & ": " & ex.Message,
  89.                             ex.GetType.ToString,
  90.                             MessageBoxButtons.OK,
  91.                             MessageBoxIcon.Error)
  92.  
  93.         Catch ex As Exception
  94.             MessageBox.Show(ex.GetType.ToString & ControlChars.NewLine &
  95.                         ControlChars.NewLine & ex.Message,
  96.                         Me.Text,
  97.                         MessageBoxButtons.OK,
  98.                         MessageBoxIcon.Error)
  99.         End Try
  100.  
  101.     End Sub
  102.  
  103.  
  104.     ' Commit the master table records changes, when enter datagridview.
  105.     Private Sub OrdersDataGridView_Enter(sender As Object, e As EventArgs) _
  106.         Handles OrdersDataGridView.Enter
  107.  
  108.         Me.CustomersBindingSource.EndEdit()
  109.     End Sub
  110. End Class
Advertisement
Add Comment
Please, Sign In to add comment