Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2016. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: ADOnet Master / Detail Maintenance
- '
- ' Author: Joseph L. Bolen
- ' Date Created: 30 NOV 2016
- '
- ' Description: Demostrate how to use the ADO.NET with Windows Forms
- ' in a Master / Detail maintenance program.
- '
- ' Documentation is at:
- ' App's screen image is at: http://imgur.com/cB5ZUAw
- ' App's Visual Basic .NET code is at: http://pastebin.com/iGDWxFYM
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '
- '-------------------------------------------------------------------------------------------
- Imports System.Data.SqlClient
- Public Class MainForm
- ' Do intial housekeeping activites and get the data before displaying the form.
- Private Sub MainForm_Load(sender As Object, e As EventArgs) _
- Handles MyBase.Load
- ' Make DataGridView ColumnHeader Bold
- OrdersDataGridView.ColumnHeadersDefaultCellStyle.Font =
- New Font(OrdersDataGridView.ColumnHeadersDefaultCellStyle.Font, FontStyle.Bold)
- Try
- ' Retrieve for table data from the database and place it in
- ' strongly typed datatables. Load master before detail.
- Me.CustomersTableAdapter.Fill(Me.SampleDBDataSet.Customers)
- Me.OrdersTableAdapter.Fill(Me.SampleDBDataSet.Orders)
- Catch ex As SqlException
- MessageBox.Show("Database error # " & ex.Number & ": " & ex.Message,
- ex.GetType.ToString,
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- Catch ex As Exception
- MessageBox.Show(ex.GetType.ToString & ControlChars.NewLine &
- ControlChars.NewLine & ex.Message,
- Me.Text,
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- End Try
- End Sub
- ' Validate, commit changes, then update the database.
- Private Sub CustomersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
- Handles CustomersBindingNavigatorSaveItem.Click
- Try
- Me.Validate()
- Me.CustomersBindingSource.EndEdit()
- Me.OrdersBindingSource.EndEdit()
- Me.TableAdapterManager.UpdateAll(Me.SampleDBDataSet)
- Catch ex As DBConcurrencyException
- MessageBox.Show("A concurrency error occurred. Data will be refreshed.",
- "Concurrency Error",
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- Me.CustomersTableAdapter.Fill(Me.SampleDBDataSet.Customers)
- Me.OrdersTableAdapter.Fill(Me.SampleDBDataSet.Orders)
- Catch ex As DataException
- MessageBox.Show(ex.Message,
- ex.GetType.ToString,
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- Me.CustomersBindingSource.CancelEdit()
- Me.OrdersBindingSource.CancelEdit()
- Catch ex As SqlException
- MessageBox.Show("Database error # " & ex.Number & ": " & ex.Message,
- ex.GetType.ToString,
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- Catch ex As Exception
- MessageBox.Show(ex.GetType.ToString & ControlChars.NewLine &
- ControlChars.NewLine & ex.Message,
- Me.Text,
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- End Try
- End Sub
- ' Commit the master table records changes, when enter datagridview.
- Private Sub OrdersDataGridView_Enter(sender As Object, e As EventArgs) _
- Handles OrdersDataGridView.Enter
- Me.CustomersBindingSource.EndEdit()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment