using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using C1.Data;
using DataLibrary.DataObjects;
using System.Data;
using System.Data.OleDb;

namespace DataLibrary
{
    /// <summary>
    /// C1SchemaDef container class: main class of the data library.
    /// Contains the C1SchemaDef component managing the data schema. 
    /// </summary>
    public class DataClass : System.ComponentModel.Component
    {
        public C1.Data.C1SchemaDef schemaDef1;

        public DataClass(System.ComponentModel.IContainer container)
        {
            /// <summary>
            /// Required for Windows.Forms Class Composition Designer support
            /// </summary>
            container.Add(this);
            InitializeComponent();
        }

        public DataClass()
        {
            /// <summary>
            /// Required for Windows.Forms Class Composition Designer support
            /// </summary>
            InitializeComponent();
        }

        #region Component Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DataClass));
            this.schemaDef1 = new C1.Data.C1SchemaDef();
            this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
            this.oleDbCommand1 = new System.Data.OleDb.OleDbCommand();
            this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
            this.oleDbCommand2 = new System.Data.OleDb.OleDbCommand();
            this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
            this.oleDbCommand3 = new System.Data.OleDb.OleDbCommand();
            this.dataSet11 = new DataLibrary.DataSet1();
            this.table_UnboundProductInfo = new C1.Data.C1TableLogic();
            this.table_UnboundCustomers = new C1.Data.C1TableLogic();
            this.table_UnboundCategories = new C1.Data.C1TableLogic();
            this.dataset_DataSet1 = new C1.Data.C1DataSetLogic();
            this.dataset_DataSet2 = new C1.Data.C1DataSetLogic();
            this.dataset_DataSet3 = new C1.Data.C1DataSetLogic();
            this.table_UnboundAuthors = new C1.Data.C1TableLogic();
            this.dataset_DataSet4 = new C1.Data.C1DataSetLogic();
            ((System.ComponentModel.ISupportInitialize)(this.schemaDef1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
            // 
            // schemaDef1
            // 
            this.schemaDef1.DummyImage = ((System.Drawing.Bitmap)(resources.GetObject("schemaDef1.DummyImage")));
            this.schemaDef1.CreateSchema += new System.EventHandler(this.schemaDef1_CreateSchema);
            this.schemaDef1.SerializedSchemaXML = this.schemaDef1.GetSchemaFromResources(resources, "schemaDef1", this);
            this.schemaDef1.SerializedDesignTimeOptionsXML = this.schemaDef1.GetDesignTimeOptionsFromResources(resources, "schemaDef1", this);
            // 
            // oleDbDataAdapter1
            // 
            this.oleDbDataAdapter1.DeleteCommand = this.oleDbCommand1;
            this.oleDbDataAdapter1.InsertCommand = this.oleDbCommand2;
            this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;
            this.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")})});
            this.oleDbDataAdapter1.UpdateCommand = this.oleDbCommand3;
            // 
            // oleDbCommand1
            // 
            this.oleDbCommand1.CommandText = "DELETE FROM Customers WHERE CustomerID=?";
            this.oleDbCommand1.Connection = this.oleDbConnection1;
            this.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, ((byte)(0)), ((byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null)});
            // 
            // oleDbConnection1
            // 
            this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"C:\\Program Files\\ComponentOne Studi" +
                "o.NET 2.0\\common\\NWIND.MDB\"";
            // 
            // oleDbCommand2
            // 
            this.oleDbCommand2.CommandText = "INSERT INTO Customers (CustomerID, CompanyName, ContactName, ContactTitle, Addres" +
                "s, City, Country) VALUES (?, ?, ?, ?, ?, ?, ?)";
            this.oleDbCommand2.Connection = this.oleDbConnection1;
            this.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
            // 
            this.oleDbSelectCommand1.CommandText = "SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Country" +
                " FROM Customers ORDER BY CustomerID";
            this.oleDbSelectCommand1.Connection = this.oleDbConnection1;
            // 
            // oleDbCommand3
            // 
            this.oleDbCommand3.CommandText = "UPDATE Customers SET CustomerID=?, CompanyName=?, ContactName=?, ContactTitle=?, " +
                "Address=?, City=?, Country=? WHERE CustomerID=?";
            this.oleDbCommand3.Connection = this.oleDbConnection1;
            this.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, ((byte)(0)), ((byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null)});
            // 
            // dataSet11
            // 
            this.dataSet11.DataSetName = "DataSet1";
            this.dataSet11.Locale = new System.Globalization.CultureInfo("en-US");
            this.dataSet11.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
            // 
            // table_UnboundProductInfo
            // 
            this.table_UnboundProductInfo.SchemaComponent = this.schemaDef1;
            this.table_UnboundProductInfo.SchemaPersist = this.schemaDef1;
            this.table_UnboundProductInfo.Table = "UnboundProductInfo";
            // 
            // table_UnboundCustomers
            // 
            this.table_UnboundCustomers.SchemaComponent = this.schemaDef1;
            this.table_UnboundCustomers.SchemaPersist = this.schemaDef1;
            this.table_UnboundCustomers.Table = "UnboundCustomers";
            this.table_UnboundCustomers.BeforeUpdateRow += new C1.Data.RowUpdateEventHandler(this.table_UnboundCustomers_BeforeUpdateRow);
            // 
            // table_UnboundCategories
            // 
            this.table_UnboundCategories.SchemaComponent = this.schemaDef1;
            this.table_UnboundCategories.SchemaPersist = this.schemaDef1;
            this.table_UnboundCategories.Table = "UnboundCategories";
            this.table_UnboundCategories.BeforeUpdateRow += new C1.Data.RowUpdateEventHandler(this.table_UnboundCategories_BeforeUpdateRow);
            // 
            // dataset_DataSet1
            // 
            this.dataset_DataSet1.DataSetDef = "DataSet1";
            this.dataset_DataSet1.SchemaComponent = this.schemaDef1;
            this.dataset_DataSet1.SchemaPersist = this.schemaDef1;
            this.dataset_DataSet1.AfterFetch += new C1.Data.FetchEventHandler(this.dataset_DataSet1_AfterFetch);
            this.dataset_DataSet1.BeforeFetch += new C1.Data.FetchEventHandler(this.dataset_DataSet1_BeforeFetch);
            // 
            // dataset_DataSet2
            // 
            this.dataset_DataSet2.DataSetDef = "DataSet2";
            this.dataset_DataSet2.SchemaComponent = this.schemaDef1;
            this.dataset_DataSet2.SchemaPersist = this.schemaDef1;
            this.dataset_DataSet2.AfterFetch += new C1.Data.FetchEventHandler(this.dataset_DataSet2_AfterFetch);
            this.dataset_DataSet2.AfterUpdate += new C1.Data.DataSetEventHandler(this.dataset_DataSet2_AfterUpdate);
            this.dataset_DataSet2.BeforeFetch += new C1.Data.FetchEventHandler(this.dataset_DataSet2_BeforeFetch);
            this.dataset_DataSet2.BeforeUpdate += new C1.Data.DataSetEventHandler(this.dataset_DataSet2_BeforeUpdate);
            // 
            // dataset_DataSet3
            // 
            this.dataset_DataSet3.DataSetDef = "DataSet3";
            this.dataset_DataSet3.SchemaComponent = this.schemaDef1;
            this.dataset_DataSet3.SchemaPersist = this.schemaDef1;
            this.dataset_DataSet3.AfterUpdate += new C1.Data.DataSetEventHandler(this.dataset_DataSet3_AfterUpdate);
            this.dataset_DataSet3.BeforeFetch += new C1.Data.FetchEventHandler(this.dataset_DataSet3_BeforeFetch);
            // 
            // table_UnboundAuthors
            // 
            this.table_UnboundAuthors.SchemaComponent = this.schemaDef1;
            this.table_UnboundAuthors.SchemaPersist = this.schemaDef1;
            this.table_UnboundAuthors.Table = "UnboundAuthors";
            this.table_UnboundAuthors.BeforeUpdateRow += new C1.Data.RowUpdateEventHandler(this.table_UnboundAuthors_BeforeUpdateRow);
            // 
            // dataset_DataSet4
            // 
            this.dataset_DataSet4.DataSetDef = "DataSet4";
            this.dataset_DataSet4.SchemaComponent = this.schemaDef1;
            this.dataset_DataSet4.SchemaPersist = this.schemaDef1;
            this.dataset_DataSet4.AfterFetch += new C1.Data.FetchEventHandler(this.dataset_DataSet4_AfterFetch);
            this.dataset_DataSet4.AfterUpdate += new C1.Data.DataSetEventHandler(this.dataset_DataSet4_AfterUpdate);
            this.dataset_DataSet4.BeforeFetch += new C1.Data.FetchEventHandler(this.dataset_DataSet4_BeforeFetch);
            this.dataset_DataSet4.BeforeUpdate += new C1.Data.DataSetEventHandler(this.dataset_DataSet4_BeforeUpdate);
            ((System.ComponentModel.ISupportInitialize)(this.schemaDef1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();

        }
        #endregion

        private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
        private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
        private System.Data.OleDb.OleDbConnection oleDbConnection1;
        private System.Data.OleDb.OleDbCommand oleDbCommand1;
        private DataLibrary.DataSet1 dataSet11;
        private C1.Data.C1TableLogic table_UnboundProductInfo;
        private C1.Data.C1TableLogic table_UnboundCustomers;
        private C1.Data.C1TableLogic table_UnboundCategories;
        private C1.Data.C1DataSetLogic dataset_DataSet1;
        private C1.Data.C1DataSetLogic dataset_DataSet2;
        private C1.Data.C1DataSetLogic dataset_DataSet3;
        private System.Data.OleDb.OleDbCommand oleDbCommand2;
        private System.Data.OleDb.OleDbCommand oleDbCommand3;
        private C1.Data.C1TableLogic table_UnboundAuthors;
        private C1.Data.C1DataSetLogic dataset_DataSet4;

        private System.Data.OleDb.OleDbConnection connect1 = null;
        private System.Data.OleDb.OleDbConnection connect2 = null;
        private System.Data.DataSet dataSet4 = null;

        private void dataset_DataSet1_BeforeFetch(object sender, C1.Data.FetchEventArgs e)
        {
            if (e.TableView == null)
                return;
            if (e.TableView.Name == "UnboundProductInfo")
            {
                if (connect1 == null)
                {
                    string connectstring = ((C1.Data.SchemaObjects.C1OleDbConnection)schemaDef1.Schema.
                        Connections["Connection"]).ConnectionString;
                    connect1 = new System.Data.OleDb.OleDbConnection(connectstring);
                }
                connect1.Open();
                OleDbCommand command = 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;
            }
        }

        private void dataset_DataSet1_AfterFetch(object sender, C1.Data.FetchEventArgs e)
        {
            if (e.TableView == null)
                return;
            if (e.TableView.Name == "UnboundProductInfo")
                connect1.Close();
        }

        private void dataset_DataSet2_BeforeFetch(object sender, C1.Data.FetchEventArgs e)
        {
            if (e.TableView == null)
                return;
            if (e.TableView.Name == "UnboundCategories")
            {
                if (connect2 == null)
                {
                    string connectstring = ((C1.Data.SchemaObjects.C1OleDbConnection)schemaDef1.Schema.
                        Connections["Connection"]).ConnectionString;
                    connect2 = new System.Data.OleDb.OleDbConnection(connectstring);
                }
                connect2.Open();
                OleDbCommand command = new OleDbCommand(
                    "SELECT CategoryID, CategoryName, Description " +
                    "FROM Categories ",
                    connect2);
                e.Reader = command.ExecuteReader();
                e.Status = DataSetEventStatusEnum.Skip;
            }
        }

        private void dataset_DataSet2_AfterFetch(object sender, C1.Data.FetchEventArgs e)
        {
            if (e.TableView == null)
                return;
            if (e.TableView.Name == "UnboundCategories")
                connect2.Close();
        }

        private void dataset_DataSet2_BeforeUpdate(object sender, C1.Data.DataSetEventArgs e)
        {
            if (connect2 != null)
                connect2.Open();
        }

        private void dataset_DataSet2_AfterUpdate(object sender, C1.Data.DataSetEventArgs e)
        {
            if (connect2 != null)
                connect2.Close();
        }

        private void table_UnboundCategories_BeforeUpdateRow(object sender, C1.Data.RowUpdateEventArgs e)
        {
            if (connect2 != null)
                switch (e.Row.RowState)
                {
                    case C1.Data.DataRowStateEnum.Deleted:
                        {
                            OleDbCommand command = new OleDbCommand(
                                "SELECT COUNT(*) As [Product Count]" +
                                "FROM Products WHERE CategoryID=?", connect2);
                            command.Parameters.AddWithValue("CategoryID_OriginalValue", e.Row["CategoryID",
                                DataRowVersionEnum.Original]);
                            int ProductCount;
                            OleDbDataReader reader = command.ExecuteReader();
                            try
                            {
                                reader.Read();
                                ProductCount = (int)reader["Product Count"];
                            }
                            finally
                            {
                                reader.Close();
                            }
                            if (ProductCount == 0)
                            {
                                command.Dispose();
                                command = new OleDbCommand(
                                    "DELETE FROM Categories WHERE CategoryID=?", connect2);
                                command.Parameters.AddWithValue("CategoryID_OriginalValue", e.Row["CategoryID",
                                    DataRowVersionEnum.Original]);
                                command.ExecuteNonQuery();
                            }
                            break;
                        }
                    case C1.Data.DataRowStateEnum.Added:
                        {
                            OleDbCommand command = 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();
                            DataLibrary.DataObjects.UnboundCategoriesRow c1Row = DataLibrary.
                                DataObjects.UnboundCategoriesRow.Obj(e.Row);
                            OleDbCommand refreshCommand;
                            if (c1Row.IsDescriptionNull())
                            {
                                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"]);
                            }
                            OleDbDataReader reader = refreshCommand.ExecuteReader();
                            try
                            {
                                reader.Read();
                                e.Row["CategoryID"] = reader["CategoryID"];
                                e.Row["CategoryName"] = reader["CategoryName"];
                                e.Row["Description"] = reader["Description"];
                            }
                            finally
                            {
                                reader.Close();
                            }
                            break;
                        }
                    case C1.Data.DataRowStateEnum.Modified:
                        {
                            OleDbCommand command = 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();
                            OleDbCommand refreshCommand = new OleDbCommand(
                                "SELECT CategoryName, Description " +
                                "FROM Categories WHERE CategoryID=?", connect2);
                            refreshCommand.Parameters.AddWithValue("CategoryID_NewValue", e.Row["CategoryID"]);
                            OleDbDataReader reader = refreshCommand.ExecuteReader();
                            try
                            {
                                reader.Read();
                                e.Row["CategoryName"] = reader["CategoryName"];
                                e.Row["Description"] = reader["Description"];
                            }
                            finally
                            {
                                reader.Close();
                            }
                            break;
                        }
                }
            e.Status = RowEventStatusEnum.SkipCurrentRow;
        }

        private void dataset_DataSet3_BeforeFetch(object sender, C1.Data.FetchEventArgs e)
        {
            if (e.TableView == null)
                return;
            if (e.TableView.Name == "UnboundCustomers")
            {
                dataSet11.Clear();

                #region Switch to C1NWind.mdb database

                string dbPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) +
                    "\\ComponentOne Samples\\Common\\C1NWind.mdb";
                if (System.IO.File.Exists(dbPath))
                {
                    C1.Data.SchemaObjects.Connection conn = schemaDef1.Schema.Connections[0];
                    oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath;
                }

                #endregion

                oleDbDataAdapter1.Fill(dataSet11);
                C1.Data.UnboundDataReader unbounddatareader = new C1.Data.
                    UnboundDataReader(dataSet11.Customers);
                e.Reader = unbounddatareader;
                e.Status = DataSetEventStatusEnum.Skip;
            }
        }

        private void table_UnboundCustomers_BeforeUpdateRow(object sender, C1.Data.RowUpdateEventArgs e)
        {
            switch (e.Row.RowState)
            {
                case C1.Data.DataRowStateEnum.Deleted:
                    {
                        DataSet1.CustomersRow adoRow = dataSet11.Customers.
                            FindByCustomerID((string)e.Row["CustomerID", DataRowVersionEnum.Original]);
                        adoRow.Delete();
                        break;
                    }
                case C1.Data.DataRowStateEnum.Added:
                    {
                        DataLibrary.DataObjects.UnboundCustomersRow c1Row =
                            DataLibrary.DataObjects.UnboundCustomersRow.Obj(e.Row);
                        DataSet1.CustomersRow adoRow = ((DataSet1.
                            CustomersRow)(dataSet11.Customers.NewRow()));
                        adoRow.CustomerID = c1Row.CustomerID;
                        adoRow.CompanyName = c1Row.CompanyName;
                        if (!c1Row.IsContactNameNull() && c1Row.ContactName.Length > 0)
                            adoRow.ContactName = c1Row.ContactName;
                        else
                            adoRow.SetContactNameNull();
                        if (!c1Row.IsContactTitleNull() && c1Row.ContactTitle.Length > 0)
                            adoRow.ContactTitle = c1Row.ContactTitle;
                        else
                            adoRow.SetContactTitleNull();
                        if (!c1Row.IsAddressNull() && c1Row.Address.Length > 0)
                            adoRow.Address = c1Row.Address;
                        else
                            adoRow.SetAddressNull();
                        if (!c1Row.IsCityNull() && c1Row.City.Length > 0)
                            adoRow.City = c1Row.City;
                        else
                            adoRow.SetCityNull();
                        if (!c1Row.IsCountryNull() && c1Row.Country.Length > 0)
                            adoRow.Country = c1Row.Country;
                        else
                            adoRow.SetCountryNull();
                        dataSet11.Customers.AddCustomersRow(adoRow);
                        break;
                    }
                case C1.Data.DataRowStateEnum.Modified:
                    {
                        DataSet1.CustomersRow adoRow = dataSet11.Customers.
                            FindByCustomerID((string)e.Row["CustomerID", DataRowVersionEnum.Original]);
                        DataLibrary.DataObjects.UnboundCustomersRow c1Row = DataLibrary.
                            DataObjects.UnboundCustomersRow.Obj(e.Row);
                        adoRow.CustomerID = c1Row.CustomerID;
                        adoRow.CompanyName = c1Row.CompanyName;
                        if (!c1Row.IsContactNameNull() && c1Row.ContactName.Length > 0)
                            adoRow.ContactName = c1Row.ContactName;
                        else
                            adoRow.SetContactNameNull();
                        if (!c1Row.IsContactTitleNull() && c1Row.ContactTitle.Length > 0)
                            adoRow.ContactTitle = c1Row.ContactTitle;
                        else
                            adoRow.SetContactTitleNull();
                        if (!c1Row.IsAddressNull() && c1Row.Address.Length > 0)
                            adoRow.Address = c1Row.Address;
                        else
                            adoRow.SetAddressNull();
                        if (!c1Row.IsCityNull() && c1Row.City.Length > 0)
                            adoRow.City = c1Row.City;
                        else
                            adoRow.SetCityNull();
                        if (!c1Row.IsCountryNull() && c1Row.Country.Length > 0)
                            adoRow.Country = c1Row.Country;
                        else
                            adoRow.SetCountryNull();
                        break;
                    }
            }
            e.Status = RowEventStatusEnum.SkipCurrentRow;
        }

        private void dataset_DataSet3_AfterUpdate(object sender, C1.Data.DataSetEventArgs e)
        {
            oleDbDataAdapter1.Update(dataSet11.Customers);
            dataSet11.AcceptChanges();
        }

        private void dataset_DataSet4_BeforeFetch(object sender, C1.Data.FetchEventArgs e)
        {
            if (e.TableView == null)
                return;
            if (e.TableView.Name == "UnboundAuthors")
            {
                dataSet4 = new System.Data.DataSet("AuthorsDataSet");
                dataSet4.ReadXml(@"..\..\authors.xml", System.Data.XmlReadMode.Auto);
                C1.Data.UnboundDataReader unbounddatareader = new C1.Data.
                    UnboundDataReader(dataSet4.Tables["authors"]);
                e.Reader = unbounddatareader;
                e.Status = DataSetEventStatusEnum.Skip;
            }
        }

        private void dataset_DataSet4_AfterFetch(object sender, C1.Data.FetchEventArgs e)
        {
            if (dataSet4 != null)
            {
                dataSet4.Dispose();
                dataSet4 = null;
            }
        }

        private void dataset_DataSet4_BeforeUpdate(object sender, C1.Data.DataSetEventArgs e)
        {
            dataSet4 = new System.Data.DataSet("AuthorsDataSet");
            dataSet4.ReadXml(@"..\..\authors.xml", System.Data.XmlReadMode.Auto);
        }

        private void table_UnboundAuthors_BeforeUpdateRow(object sender, C1.Data.RowUpdateEventArgs e)
        {
            switch (e.Row.RowState)
            {
                case C1.Data.DataRowStateEnum.Deleted:
                    {
                        System.Data.DataTable t = dataSet4.Tables["authors"];
                        int required_id = (int)e.Row["ID", DataRowVersionEnum.Original];
                        for (int i = 0; i < t.Rows.Count; i++)
                            if ((int)t.Rows[i]["ID"] == required_id)
                            {
                                t.Rows.RemoveAt(i);
                                break;
                            }
                        break;
                    }
                case C1.Data.DataRowStateEnum.Added:
                    {
                        System.Data.DataTable t = dataSet4.Tables["authors"];
                        int id, test_id = 0;
                        for (int i = 0; i < t.Rows.Count; i++)
                        {
                            id = (int)t.Rows[i]["ID"];
                            if (id > test_id)
                                test_id = id;
                        }
                        id = test_id + 1;
                        DataLibrary.DataObjects.UnboundAuthorsRow c1Row =
                            DataLibrary.DataObjects.UnboundAuthorsRow.Obj(e.Row);
                        c1Row.ID = id;
                        t.Rows.Add(new object[] {
						c1Row.ID,
						c1Row.FirstName,
						c1Row.LastName,
						c1Row.Phone,
						(c1Row.IsAddressNull() || c1Row.Address.Length == 0 ?
												System.Convert.DBNull :
												c1Row.Address),
						(c1Row.IsCityNull() || c1Row.City.Length == 0 ?
												System.Convert.DBNull :
												c1Row.City),
						(c1Row.IsStateNull() || c1Row.State.Length == 0 ?
												System.Convert.DBNull :
												c1Row.State),
						(c1Row.IsZipNull() || c1Row.Zip.Length == 0 ?
												System.Convert.DBNull :
												c1Row.Zip)
					});
                        break;
                    }
                case C1.Data.DataRowStateEnum.Modified:
                    {
                        System.Data.DataTable t = dataSet4.Tables["authors"];
                        DataLibrary.DataObjects.UnboundAuthorsRow c1Row = DataLibrary.
                            DataObjects.UnboundAuthorsRow.Obj(e.Row);
                        System.Data.DataRow r;
                        int required_id = (int)e.Row["ID", DataRowVersionEnum.Original];
                        for (int i = 0; i < t.Rows.Count; i++)
                        {
                            r = t.Rows[i];
                            if ((int)r["ID"] == required_id)
                            {
                                r["FirstName"] = c1Row.FirstName;
                                r["LastName"] = c1Row.LastName;
                                r["Phone"] = c1Row.Phone;
                                if (!c1Row.IsAddressNull() && c1Row.Address.Length > 0)
                                    r["Address"] = c1Row.Address;
                                else
                                    r["Address"] = System.Convert.DBNull;
                                if (!c1Row.IsCityNull() && c1Row.City.Length > 0)
                                    r["City"] = c1Row.City;
                                else
                                    r["City"] = System.Convert.DBNull;
                                if (!c1Row.IsStateNull() && c1Row.State.Length > 0)
                                    r["State"] = c1Row.State;
                                else
                                    r["State"] = System.Convert.DBNull;
                                if (!c1Row.IsZipNull() && c1Row.Zip.Length > 0)
                                    r["Zip"] = c1Row.Zip;
                                else
                                    r["Zip"] = System.Convert.DBNull;
                                break;
                            }
                        }
                        break;
                    }
            }
            e.Status = RowEventStatusEnum.SkipCurrentRow;
        }

        private void dataset_DataSet4_AfterUpdate(object sender, C1.Data.DataSetEventArgs e)
        {
            if (dataSet4 != null)
            {
                dataSet4.AcceptChanges();
                dataSet4.WriteXml(@"..\..\authors.xml", System.Data.XmlWriteMode.WriteSchema);
                dataSet4.Dispose();
                dataSet4 = null;
            }
        }

        #region Switch to C1NWind.mdb database

        private void schemaDef1_CreateSchema(object sender, EventArgs e)
        {
            string dbPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) +
                "\\ComponentOne Samples\\Common\\C1NWind.mdb";
            if (System.IO.File.Exists(dbPath))
            {
                C1.Data.SchemaObjects.Connection conn = schemaDef1.Schema.Connections[0];
                conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath;
            }
        }

        #endregion
    }
}
