Imports System
Imports System.ComponentModel
Imports System.Collections
Imports System.Diagnostics
Imports System.Data
Imports System.Data.OleDb

Imports C1.Data
Imports DataLibrary.DataObjects

' C1SchemaDef container class: main class of the data library.
' Contains the C1SchemaDef component managing the data schema. 
Public Class DataClass
    Inherits System.ComponentModel.Component

#Region " Component Designer generated code "

    ' C1SchemaDef component managing the data schema.
    ' Needs to be public in order to allow access to the library.
    Public WithEvents SchemaDef1 As C1.Data.C1SchemaDef

    Public Sub New(Container As System.ComponentModel.IContainer)
        MyClass.New()

        'Required for Windows.Forms Class Composition Designer support
        Container.Add(me)
    End Sub

    Public Sub New()
        MyBase.New()
        'This call is required by the Component Designer.
        InitializeComponent()
    End Sub

    'Component 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
    Friend WithEvents table_UnboundProductInfo As C1.Data.C1TableLogic
    Friend WithEvents table_UnboundCustomers As C1.Data.C1TableLogic
    Friend WithEvents table_UnboundCategories As C1.Data.C1TableLogic
    Friend WithEvents table_UnboundAuthors As C1.Data.C1TableLogic
    Friend WithEvents dataset_DataSet1 As C1.Data.C1DataSetLogic
    Friend WithEvents dataset_DataSet2 As C1.Data.C1DataSetLogic
    Friend WithEvents dataset_DataSet3 As C1.Data.C1DataSetLogic
    Friend WithEvents dataset_DataSet4 As C1.Data.C1DataSetLogic
    Friend WithEvents OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter
    Friend WithEvents OleDbSelectCommand1 As System.Data.OleDb.OleDbCommand
    Friend WithEvents OleDbConnection1 As System.Data.OleDb.OleDbConnection
    Friend WithEvents OleDbCommand1 As System.Data.OleDb.OleDbCommand
    Friend WithEvents OleDbCommand2 As System.Data.OleDb.OleDbCommand
    Friend WithEvents OleDbCommand3 As System.Data.OleDb.OleDbCommand
    Friend WithEvents AdoDataSet11 As DataLibrary.ADODataSet1

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Component Designer
    'It can be modified using the Component Designer.
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(DataClass))
        Me.SchemaDef1 = New C1.Data.C1SchemaDef
        Me.table_UnboundProductInfo = New C1.Data.C1TableLogic
        Me.table_UnboundCustomers = New C1.Data.C1TableLogic
        Me.table_UnboundCategories = New C1.Data.C1TableLogic
        Me.table_UnboundAuthors = New C1.Data.C1TableLogic
        Me.dataset_DataSet1 = New C1.Data.C1DataSetLogic
        Me.dataset_DataSet2 = New C1.Data.C1DataSetLogic
        Me.dataset_DataSet3 = New C1.Data.C1DataSetLogic
        Me.dataset_DataSet4 = New C1.Data.C1DataSetLogic
        Me.OleDbDataAdapter1 = New System.Data.OleDb.OleDbDataAdapter
        Me.OleDbCommand1 = New System.Data.OleDb.OleDbCommand
        Me.OleDbConnection1 = New System.Data.OleDb.OleDbConnection
        Me.OleDbCommand2 = New System.Data.OleDb.OleDbCommand
        Me.OleDbSelectCommand1 = New System.Data.OleDb.OleDbCommand
        Me.OleDbCommand3 = New System.Data.OleDb.OleDbCommand
        Me.AdoDataSet11 = New DataLibrary.ADODataSet1
        CType(Me.SchemaDef1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.AdoDataSet11, System.ComponentModel.ISupportInitialize).BeginInit()
        '
        'SchemaDef1
        '
        Me.SchemaDef1.DummyImage = CType(resources.GetObject("SchemaDef1.DummyImage"), System.Drawing.Bitmap)
        Me.SchemaDef1.SerializedSchemaXML = Me.SchemaDef1.GetSchemaFromResources(resources, "SchemaDef1", Me)
        Me.SchemaDef1.SerializedDesignTimeOptionsXML = Me.SchemaDef1.GetDesignTimeOptionsFromResources(resources, "SchemaDef1", Me)
        '
        'table_UnboundProductInfo
        '
        Me.table_UnboundProductInfo.SchemaComponent = Me.SchemaDef1
        Me.table_UnboundProductInfo.SchemaPersist = Me.SchemaDef1
        Me.table_UnboundProductInfo.Table = "UnboundProductInfo"
        '
        'table_UnboundCustomers
        '
        Me.table_UnboundCustomers.SchemaComponent = Me.SchemaDef1
        Me.table_UnboundCustomers.SchemaPersist = Me.SchemaDef1
        Me.table_UnboundCustomers.Table = "UnboundCustomers"
        '
        'table_UnboundCategories
        '
        Me.table_UnboundCategories.SchemaComponent = Me.SchemaDef1
        Me.table_UnboundCategories.SchemaPersist = Me.SchemaDef1
        Me.table_UnboundCategories.Table = "UnboundCategories"
        '
        'table_UnboundAuthors
        '
        Me.table_UnboundAuthors.SchemaComponent = Me.SchemaDef1
        Me.table_UnboundAuthors.SchemaPersist = Me.SchemaDef1
        Me.table_UnboundAuthors.Table = "UnboundAuthors"
        '
        'dataset_DataSet1
        '
        Me.dataset_DataSet1.DataSetDef = "DataSet1"
        Me.dataset_DataSet1.SchemaComponent = Me.SchemaDef1
        Me.dataset_DataSet1.SchemaPersist = Me.SchemaDef1
        '
        'dataset_DataSet2
        '
        Me.dataset_DataSet2.DataSetDef = "DataSet2"
        Me.dataset_DataSet2.SchemaComponent = Me.SchemaDef1
        Me.dataset_DataSet2.SchemaPersist = Me.SchemaDef1
        '
        'dataset_DataSet3
        '
        Me.dataset_DataSet3.DataSetDef = "DataSet3"
        Me.dataset_DataSet3.SchemaComponent = Me.SchemaDef1
        Me.dataset_DataSet3.SchemaPersist = Me.SchemaDef1
        '
        'dataset_DataSet4
        '
        Me.dataset_DataSet4.DataSetDef = "DataSet4"
        Me.dataset_DataSet4.SchemaComponent = Me.SchemaDef1
        Me.dataset_DataSet4.SchemaPersist = Me.SchemaDef1
        '
        'OleDbDataAdapter1
        '
        Me.OleDbDataAdapter1.DeleteCommand = Me.OleDbCommand1
        Me.OleDbDataAdapter1.InsertCommand = Me.OleDbCommand2
        Me.OleDbDataAdapter1.SelectCommand = Me.OleDbSelectCommand1
        Me.OleDbDataAdapter1.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "Customers", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("CustomerID", "CustomerID"), New System.Data.Common.DataColumnMapping("CompanyName", "CompanyName"), New System.Data.Common.DataColumnMapping("ContactName", "ContactName"), New System.Data.Common.DataColumnMapping("ContactTitle", "ContactTitle"), New System.Data.Common.DataColumnMapping("Address", "Address"), New System.Data.Common.DataColumnMapping("City", "City"), New System.Data.Common.DataColumnMapping("Country", "Country")})})
        Me.OleDbDataAdapter1.UpdateCommand = Me.OleDbCommand3
        '
        'OleDbCommand1
        '
        Me.OleDbCommand1.CommandText = "DELETE FROM Customers WHERE CustomerID=?"
        Me.OleDbCommand1.Connection = Me.OleDbConnection1
        Me.OleDbCommand1.Parameters.AddRange(New System.Data.OleDb.OleDbParameter() {New System.Data.OleDb.OleDbParameter("CustomerID", System.Data.OleDb.OleDbType.VarWChar, 5, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "CustomerID", System.Data.DataRowVersion.Original, Nothing)})
        '
        'OleDbConnection1
        '
        Me.OleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""C:\Program Files\ComponentOne Studi" & _
            "o.NET 2.0\common\NWIND.MDB"""
        '
        'OleDbCommand2
        '
        Me.OleDbCommand2.CommandText = "INSERT INTO Customers (CustomerID, CompanyName, ContactName, ContactTitle, Addres" & _
            "s, City, Country) VALUES (?, ?, ?, ?, ?, ?, ?)"
        Me.OleDbCommand2.Connection = Me.OleDbConnection1
        Me.OleDbCommand2.Parameters.AddRange(New System.Data.OleDb.OleDbParameter() {New System.Data.OleDb.OleDbParameter("CustomerID", System.Data.OleDb.OleDbType.VarWChar, 5, "CustomerID"), New System.Data.OleDb.OleDbParameter("CompanyName", System.Data.OleDb.OleDbType.VarWChar, 40, "CompanyName"), New System.Data.OleDb.OleDbParameter("ContactName", System.Data.OleDb.OleDbType.VarWChar, 30, "ContactName"), New System.Data.OleDb.OleDbParameter("ContactTitle", System.Data.OleDb.OleDbType.VarWChar, 30, "ContactTitle"), New System.Data.OleDb.OleDbParameter("Address", System.Data.OleDb.OleDbType.VarWChar, 60, "Address"), New System.Data.OleDb.OleDbParameter("City", System.Data.OleDb.OleDbType.VarWChar, 15, "City"), New System.Data.OleDb.OleDbParameter("Country", System.Data.OleDb.OleDbType.VarWChar, 15, "Country")})
        '
        'OleDbSelectCommand1
        '
        Me.OleDbSelectCommand1.CommandText = "SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Country" & _
            " FROM Customers ORDER BY CustomerID"
        Me.OleDbSelectCommand1.Connection = Me.OleDbConnection1
        '
        'OleDbCommand3
        '
        Me.OleDbCommand3.CommandText = "UPDATE Customers SET CustomerID=?, CompanyName=?, ContactName=?, ContactTitle=?, " & _
            "Address=?, City=?, Country=? WHERE CustomerID=?"
        Me.OleDbCommand3.Connection = Me.OleDbConnection1
        Me.OleDbCommand3.Parameters.AddRange(New System.Data.OleDb.OleDbParameter() {New System.Data.OleDb.OleDbParameter("CustomerID", System.Data.OleDb.OleDbType.VarWChar, 5, "CustomerID"), New System.Data.OleDb.OleDbParameter("CompanyName", System.Data.OleDb.OleDbType.VarWChar, 40, "CompanyName"), New System.Data.OleDb.OleDbParameter("ContactName", System.Data.OleDb.OleDbType.VarWChar, 30, "ContactName"), New System.Data.OleDb.OleDbParameter("ContactTitle", System.Data.OleDb.OleDbType.VarWChar, 30, "ContactTitle"), New System.Data.OleDb.OleDbParameter("Address", System.Data.OleDb.OleDbType.VarWChar, 60, "Address"), New System.Data.OleDb.OleDbParameter("City", System.Data.OleDb.OleDbType.VarWChar, 15, "City"), New System.Data.OleDb.OleDbParameter("Country", System.Data.OleDb.OleDbType.VarWChar, 15, "Country"), New System.Data.OleDb.OleDbParameter("Original_CustomerID", System.Data.OleDb.OleDbType.VarWChar, 5, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "CustomerID", System.Data.DataRowVersion.Original, Nothing)})
        '
        'AdoDataSet11
        '
        Me.AdoDataSet11.DataSetName = "ADODataSet1"
        Me.AdoDataSet11.Locale = New System.Globalization.CultureInfo("en-US")
        Me.AdoDataSet11.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema
        CType(Me.SchemaDef1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.AdoDataSet11, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub

#End Region

    Private connect1 As System.Data.OleDb.OleDbConnection = Nothing
    Private connect2 As System.Data.OleDb.OleDbConnection = Nothing
    Private dataSet4 As System.Data.DataSet = Nothing

    Private Sub dataset_DataSet1_BeforeFetch(ByVal sender As Object, ByVal e As C1.Data.FetchEventArgs) Handles dataset_DataSet1.BeforeFetch
        If e.TableView Is Nothing Then Exit Sub
        If e.TableView.Name = "UnboundProductInfo" Then
            If connect1 Is Nothing Then
                Dim connectstring As String = (CType(SchemaDef1.Schema. _
                 Connections("Connection"), C1.Data.SchemaObjects.C1OleDbConnection)).ConnectionString
                connect1 = New System.Data.OleDb.OleDbConnection(connectstring)
            End If
            connect1.Open()
            Dim command As OleDbCommand = New OleDbCommand( _
             "SELECT Products.ProductName, Categories.CategoryName, Suppliers.CompanyName, " + _
             "  Products.QuantityPerUnit, Products.UnitPrice " + _
             "FROM Products, Categories, Suppliers " + _
             "WHERE Products.SupplierID=Suppliers.SupplierID AND " + _
             "  Products.CategoryID=Categories.CategoryID", _
             connect1)
            e.Reader = command.ExecuteReader()
            e.Status = DataSetEventStatusEnum.Skip
        End If
    End Sub

    Private Sub dataset_DataSet1_AfterFetch(ByVal sender As Object, ByVal e As C1.Data.FetchEventArgs) Handles dataset_DataSet1.AfterFetch
        If e.TableView Is Nothing Then Exit Sub
        If e.TableView.Name = "UnboundProductInfo" Then
            connect1.Close()
        End If
    End Sub

    Private Sub dataset_DataSet2_BeforeFetch(ByVal sender As Object, ByVal e As C1.Data.FetchEventArgs) Handles dataset_DataSet2.BeforeFetch
        If e.TableView Is Nothing Then Exit Sub
        If e.TableView.Name = "UnboundCategories" Then
            If connect2 Is Nothing Then
                Dim connectstring As String = (CType(SchemaDef1.Schema. _
                 Connections("Connection"), C1.Data.SchemaObjects.C1OleDbConnection)).ConnectionString
                connect2 = New System.Data.OleDb.OleDbConnection(connectstring)
            End If
            connect2.Open()
            Dim command As OleDbCommand = New OleDbCommand( _
             "SELECT CategoryID, CategoryName, Description " + _
             "FROM Categories ", _
             connect2)
            e.Reader = command.ExecuteReader()
            e.Status = DataSetEventStatusEnum.Skip
        End If
    End Sub

    Private Sub dataset_DataSet2_AfterFetch(ByVal sender As Object, ByVal e As C1.Data.FetchEventArgs) Handles dataset_DataSet2.AfterFetch
        If e.TableView Is Nothing Then Exit Sub
        If e.TableView.Name = "UnboundCategories" Then
            connect2.Close()
        End If
    End Sub

    Private Sub dataset_DataSet2_BeforeUpdate(ByVal sender As Object, ByVal e As C1.Data.DataSetEventArgs) Handles dataset_DataSet2.BeforeUpdate
        If Not connect2 Is Nothing Then
            connect2.Open()
        End If
    End Sub

    Private Sub dataset_DataSet2_AfterUpdate(ByVal sender As Object, ByVal e As C1.Data.DataSetEventArgs) Handles dataset_DataSet2.AfterUpdate
        If Not connect2 Is Nothing Then
            connect2.Close()
        End If
    End Sub

    Private Sub table_UnboundCategories_BeforeUpdateRow(ByVal sender As Object, ByVal e As C1.Data.RowUpdateEventArgs) Handles table_UnboundCategories.BeforeUpdateRow
        If Not connect2 Is Nothing Then
            Select Case e.Row.RowState
                Case C1.Data.DataRowStateEnum.Deleted
                    Dim command As OleDbCommand = New OleDbCommand( _
                     "SELECT COUNT(*) As [Product Count]" + _
                     "FROM Products WHERE CategoryID=?", connect2)
                    command.Parameters.AddWithValue("CategoryID_OriginalValue", e.Row("CategoryID", _
                     DataRowVersionEnum.Original))
                    Dim ProductCount As Int32
                    Dim reader As OleDbDataReader = command.ExecuteReader()
                    Try
                        reader.Read()
                        ProductCount = CType(reader("Product Count"), Int32)
                    Finally
                        reader.Close()
                    End Try
                    If ProductCount = 0 Then
                        command.Dispose()
                        command = New OleDbCommand( _
                         "DELETE FROM Categories WHERE CategoryID=?", connect2)
                        command.Parameters.AddWithValue("CategoryID_OriginalValue", e.Row("CategoryID", _
                         DataRowVersionEnum.Original))
                        command.ExecuteNonQuery()
                    End If
                Case C1.Data.DataRowStateEnum.Added
                    Dim command As OleDbCommand = New OleDbCommand( _
                     "INSERT INTO Categories (CategoryName, Description) " + _
                     "VALUES (?, ?)", connect2)
                    command.Parameters.AddWithValue("CategoryName_NewValue", e.Row("CategoryName"))
                    command.Parameters.AddWithValue("Description_NewValue", e.Row("Description"))
                    command.ExecuteNonQuery()
                    Dim c1Row As DataLibrary.DataObjects.UnboundCategoriesRow = DataLibrary. _
                         DataObjects.UnboundCategoriesRow.Obj(e.Row)
                    Dim refreshCommand As OleDbCommand
                    If c1Row.IsDescriptionNull() Then
                        refreshCommand = New OleDbCommand( _
                         "SELECT CategoryID, CategoryName, Description " + _
                         "FROM Categories WHERE CategoryName=?", connect2)
                        refreshCommand.Parameters.AddWithValue("CategoryName_NewValue", e.Row("CategoryName"))
                    Else
                        refreshCommand = New OleDbCommand( _
                         "SELECT CategoryID, CategoryName, Description " + _
                         "FROM Categories WHERE CategoryName=? AND Description=?", connect2)
                        refreshCommand.Parameters.AddWithValue("CategoryName_NewValue", e.Row("CategoryName"))
                        refreshCommand.Parameters.AddWithValue("Description_NewValue", e.Row("Description"))
                    End If
                    Dim reader As OleDbDataReader = refreshCommand.ExecuteReader()
                    Try
                        reader.Read()
                        e.Row("CategoryID") = reader("CategoryID")
                        e.Row("CategoryName") = reader("CategoryName")
                        e.Row("Description") = reader("Description")
                    Finally
                        reader.Close()
                    End Try
                Case C1.Data.DataRowStateEnum.Modified
                    Dim command As OleDbCommand = New OleDbCommand( _
                         "UPDATE Categories " + _
                         "SET CategoryName=?, Description=? WHERE CategoryID=?", connect2)
                    command.Parameters.AddWithValue("CategoryName_NewValue", e.Row("CategoryName"))
                    command.Parameters.AddWithValue("Description_NewValue", e.Row("Description"))
                    command.Parameters.AddWithValue("CategoryID_OriginalValue", e.Row("CategoryID", _
                         DataRowVersionEnum.Original))
                    command.ExecuteNonQuery()
                    Dim refreshCommand As OleDbCommand = New OleDbCommand( _
                         "SELECT CategoryName, Description " + _
                         "FROM Categories WHERE CategoryID=?", connect2)
                    refreshCommand.Parameters.AddWithValue("CategoryID_NewValue", e.Row("CategoryID"))
                    Dim reader As OleDbDataReader = refreshCommand.ExecuteReader()
                    Try
                        reader.Read()
                        e.Row("CategoryName") = reader("CategoryName")
                        e.Row("Description") = reader("Description")
                    Finally
                        reader.Close()
                    End Try
            End Select
        End If
        e.Status = RowEventStatusEnum.SkipCurrentRow
    End Sub

    Private Sub dataset_DataSet3_BeforeFetch(ByVal sender As Object, ByVal e As C1.Data.FetchEventArgs) Handles dataset_DataSet3.BeforeFetch
        If e.TableView Is Nothing Then Exit Sub
        If e.TableView.Name = "UnboundCustomers" Then
            AdoDataSet11.Clear()

            ' Switch to C1NWind.mdb database
            Dim dbPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + _
                "\ComponentOne Samples\Common\C1NWind.mdb"
            If System.IO.File.Exists(dbPath) Then
                Dim conn As C1.Data.SchemaObjects.Connection = SchemaDef1.Schema.Connections(0)
                OleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath
            End If

            OleDbDataAdapter1.Fill(AdoDataSet11)
            Dim unbounddatareader As C1.Data.UnboundDataReader = New C1.Data. _
             UnboundDataReader(AdoDataSet11.Customers)
            e.Reader = unbounddatareader
            e.Status = DataSetEventStatusEnum.Skip
        End If
    End Sub

    Private Sub table_UnboundCustomers_BeforeUpdateRow(ByVal sender As Object, ByVal e As C1.Data.RowUpdateEventArgs) Handles table_UnboundCustomers.BeforeUpdateRow
        Select Case e.Row.RowState
            Case C1.Data.DataRowStateEnum.Deleted
                Dim adoRow As ADODataSet1.CustomersRow = AdoDataSet11.Customers. _
                 FindByCustomerID(CType(e.Row("CustomerID", DataRowVersionEnum.Original), String))
                adoRow.Delete()
            Case C1.Data.DataRowStateEnum.Added
                Dim c1Row As DataLibrary.DataObjects.UnboundCustomersRow = _
                           DataLibrary.DataObjects.UnboundCustomersRow.Obj(e.Row)
                Dim adoRow As ADODataSet1.CustomersRow = CType(AdoDataSet11.Customers.NewRow(), _
                           ADODataSet1.CustomersRow)
                adoRow.CustomerID = c1Row.CustomerID
                adoRow.CompanyName = c1Row.CompanyName
                If Not c1Row.IsContactNameNull() And c1Row.ContactName.Length > 0 Then
                    adoRow.ContactName = c1Row.ContactName
                Else
                    adoRow.SetContactNameNull()
                End If
                If Not c1Row.IsContactTitleNull() And c1Row.ContactTitle.Length > 0 Then
                    adoRow.ContactTitle = c1Row.ContactTitle
                Else
                    adoRow.SetContactTitleNull()
                End If
                If Not c1Row.IsAddressNull() And c1Row.Address.Length > 0 Then
                    adoRow.Address = c1Row.Address
                Else
                    adoRow.SetAddressNull()
                End If
                If Not c1Row.IsCityNull() And c1Row.City.Length > 0 Then
                    adoRow.City = c1Row.City
                Else
                    adoRow.SetCityNull()
                End If
                If Not c1Row.IsCountryNull() And c1Row.Country.Length > 0 Then
                    adoRow.Country = c1Row.Country
                Else
                    adoRow.SetCountryNull()
                End If
                AdoDataSet11.Customers.AddCustomersRow(adoRow)
            Case C1.Data.DataRowStateEnum.Modified
                Dim adoRow As ADODataSet1.CustomersRow = AdoDataSet11.Customers. _
                 FindByCustomerID(CType(e.Row("CustomerID", DataRowVersionEnum.Original), String))
                Dim c1Row As DataLibrary.DataObjects.UnboundCustomersRow = DataLibrary. _
                 DataObjects.UnboundCustomersRow.Obj(e.Row)
                adoRow.CustomerID = c1Row.CustomerID
                adoRow.CompanyName = c1Row.CompanyName
                If Not c1Row.IsContactNameNull() And c1Row.ContactName.Length > 0 Then
                    adoRow.ContactName = c1Row.ContactName
                Else
                    adoRow.SetContactNameNull()
                End If
                If Not c1Row.IsContactTitleNull() And c1Row.ContactTitle.Length > 0 Then
                    adoRow.ContactTitle = c1Row.ContactTitle
                Else
                    adoRow.SetContactTitleNull()
                End If
                If Not c1Row.IsAddressNull() And c1Row.Address.Length > 0 Then
                    adoRow.Address = c1Row.Address
                Else
                    adoRow.SetAddressNull()
                End If
                If Not c1Row.IsCityNull() And c1Row.City.Length > 0 Then
                    adoRow.City = c1Row.City
                Else
                    adoRow.SetCityNull()
                End If
                If Not c1Row.IsCountryNull() And c1Row.Country.Length > 0 Then
                    adoRow.Country = c1Row.Country
                Else
                    adoRow.SetCountryNull()
                End If
        End Select
        e.Status = RowEventStatusEnum.SkipCurrentRow
    End Sub

    Private Sub dataset_DataSet3_AfterUpdate(ByVal sender As Object, ByVal e As C1.Data.DataSetEventArgs) Handles dataset_DataSet3.AfterUpdate
        OleDbDataAdapter1.Update(AdoDataSet11.Customers)
        AdoDataSet11.AcceptChanges()
    End Sub

    Private Sub dataset_DataSet4_BeforeFetch(ByVal sender As Object, ByVal e As C1.Data.FetchEventArgs) Handles dataset_DataSet4.BeforeFetch
        If e.TableView Is Nothing Then Exit Sub
        If e.TableView.Name = "UnboundAuthors" Then
            dataSet4 = New System.Data.DataSet("AuthorsDataSet")
            dataSet4.ReadXml("..\..\authors.xml", System.Data.XmlReadMode.Auto)
            Dim unbounddatareader As C1.Data.UnboundDataReader = New C1.Data. _
             UnboundDataReader(dataSet4.Tables("authors"))
            e.Reader = unbounddatareader
            e.Status = DataSetEventStatusEnum.Skip
        End If
    End Sub

    Private Sub dataset_DataSet4_AfterFetch(ByVal sender As Object, ByVal e As C1.Data.FetchEventArgs) Handles dataset_DataSet4.AfterFetch
        If Not dataSet4 Is Nothing Then
            dataSet4.Dispose()
            dataSet4 = Nothing
        End If
    End Sub

    Private Sub dataset_DataSet4_BeforeUpdate(ByVal sender As Object, ByVal e As C1.Data.DataSetEventArgs) Handles dataset_DataSet4.BeforeUpdate
        dataSet4 = New System.Data.DataSet("AuthorsDataSet")
        dataSet4.ReadXml("..\..\authors.xml", System.Data.XmlReadMode.Auto)
    End Sub

    Private Sub table_UnboundAuthors_BeforeUpdateRow(ByVal sender As Object, ByVal e As C1.Data.RowUpdateEventArgs) Handles table_UnboundAuthors.BeforeUpdateRow
        Select Case e.Row.RowState
            Case C1.Data.DataRowStateEnum.Deleted
                Dim t As System.Data.DataTable = dataSet4.Tables("authors")
                Dim required_id As Int32 = CType(e.Row("ID", DataRowVersionEnum.Original), Int32)
                Dim i As Int32
                For i = 0 To t.Rows.Count - 1
                    If CType(t.Rows(i)("ID"), Int32) = required_id Then
                        t.Rows.RemoveAt(i)
                        Exit For
                    End If
                Next
            Case C1.Data.DataRowStateEnum.Added
                Dim t As System.Data.DataTable = dataSet4.Tables("authors")
                Dim i As Int32, id As Int32, test_id As Int32 = 0
                For i = 0 To t.Rows.Count - 1
                    id = CType(t.Rows(i)("ID"), Int32)
                    If id > test_id Then
                        test_id = id
                    End If
                Next
                id = test_id + 1
                Dim c1Row As DataLibrary.DataObjects.UnboundAuthorsRow = _
                 DataLibrary.DataObjects.UnboundAuthorsRow.Obj(e.Row)
                Dim r As System.Data.DataRow = t.NewRow()
                r("ID") = id
                r("FirstName") = c1Row.FirstName
                r("LastName") = c1Row.LastName
                r("Phone") = c1Row.Phone
                If Not c1Row.IsAddressNull() And c1Row.Address.Length > 0 Then
                    r("Address") = c1Row.Address
                Else
                    r("Address") = System.Convert.DBNull
                End If
                If Not c1Row.IsCityNull() And c1Row.City.Length > 0 Then
                    r("City") = c1Row.City
                Else
                    r("City") = System.Convert.DBNull
                End If
                If Not c1Row.IsStateNull() And c1Row.State.Length > 0 Then
                    r("State") = c1Row.State
                Else
                    r("State") = System.Convert.DBNull
                End If
                If Not c1Row.IsZipNull() And c1Row.Zip.Length > 0 Then
                    r("Zip") = c1Row.Zip
                Else
                    r("Zip") = System.Convert.DBNull
                End If
                t.Rows.Add(r)
            Case C1.Data.DataRowStateEnum.Modified
                Dim t As System.Data.DataTable = dataSet4.Tables("authors")
                Dim c1Row As DataLibrary.DataObjects.UnboundAuthorsRow = DataLibrary. _
                 DataObjects.UnboundAuthorsRow.Obj(e.Row)
                Dim r As System.Data.DataRow
                Dim required_id As Int32 = CType(e.Row("ID", DataRowVersionEnum.Original), Int32)
                Dim i As Int32
                For i = 0 To t.Rows.Count - 1
                    r = t.Rows(i)
                    If CType(r("ID"), Int32) = required_id Then
                        r("FirstName") = c1Row.FirstName
                        r("LastName") = c1Row.LastName
                        r("Phone") = c1Row.Phone
                        If Not c1Row.IsAddressNull() And c1Row.Address.Length > 0 Then
                            r("Address") = c1Row.Address
                        Else
                            r("Address") = System.Convert.DBNull
                        End If
                        If Not c1Row.IsCityNull() And c1Row.City.Length > 0 Then
                            r("City") = c1Row.City
                        Else
                            r("City") = System.Convert.DBNull
                        End If
                        If Not c1Row.IsStateNull() And c1Row.State.Length > 0 Then
                            r("State") = c1Row.State
                        Else
                            r("State") = System.Convert.DBNull
                        End If
                        If Not c1Row.IsZipNull() And c1Row.Zip.Length > 0 Then
                            r("Zip") = c1Row.Zip
                        Else
                            r("Zip") = System.Convert.DBNull
                        End If
                        Exit For
                    End If
                Next
        End Select
        e.Status = RowEventStatusEnum.SkipCurrentRow
    End Sub

    Private Sub dataset_DataSet4_AfterUpdate(ByVal sender As Object, ByVal e As C1.Data.DataSetEventArgs) Handles dataset_DataSet4.AfterUpdate
        If Not dataSet4 Is Nothing Then
            dataSet4.AcceptChanges()
            dataSet4.WriteXml("..\..\authors.xml", System.Data.XmlWriteMode.WriteSchema)
            dataSet4.Dispose()
            dataSet4 = Nothing
        End If
    End Sub

#Region " Switch to C1NWind.mdb database "

    Private Sub SchemaDef1_CreateSchema(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SchemaDef1.CreateSchema
        Dim dbPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + _
            "\ComponentOne Samples\Common\C1NWind.mdb"
        If System.IO.File.Exists(dbPath) Then
            Dim conn As C1.Data.SchemaObjects.Connection = SchemaDef1.Schema.Connections(0)
            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath
        End If
    End Sub

#End Region

End Class
