Click here to Skip to main content
15,896,912 members
Articles / Programming Languages / C#

Custom DataBindable BusinessObjects and The Typed DataSet

Rate me:
Please Sign up or sign in to vote.
4.80/5 (36 votes)
3 Jan 200716 min read 150K   2K   147  
An article about Custom Businessobjects in conjunction with the DataSet to Retrieve Data
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DCAF.DataLayer.CustomerOrderTDSTableAdapters;

namespace DCAF.DataLayer
{
    public class CustomerOrderService
    {
        #region "Storage"

        private CustomersTableAdapter m_customersAdapter = null;
        protected CustomersTableAdapter CustomersAdapter
        {
            get
            {
                if (m_customersAdapter == null)
                {
                    m_customersAdapter = new CustomersTableAdapter();
                }
                return m_customersAdapter;
            }
        }

        private OrdersTableAdapter m_ordersAdapter = null;
        protected OrdersTableAdapter OrdersAdapter
        {
            get
            {
                if (m_ordersAdapter == null)
                {
                    m_ordersAdapter = new OrdersTableAdapter();
                }
                return m_ordersAdapter;
            }
        }
        
        #endregion "Storage"

        #region "Public Interface

        public CustomerOrderTDS.CustomersDataTable GetCustomers()
        {
            
            return CustomersAdapter.GetData();
        }

        public CustomerOrderTDS.OrdersDataTable GetOrders()
        {
            return OrdersAdapter.GetData();
        }

        public CustomerOrderTDS GetCustomerOrders()
        {
            
            return CustomerOrderTDS.GetCustomerOrders();
        }

        public CustomerOrderTDS.CustomersDataTable GetCustomerRow(string p_customerID)
        {
            m_customersAdapter = null;
            return CustomersAdapter.GetDataByCustomerID(p_customerID);
        }

        public CustomerOrderTDS.OrdersDataTable GetOrderRow(int p_orderID)
        {
            m_ordersAdapter = null;
            return OrdersAdapter.GetDataByOrderID(p_orderID);
        }
        


        public int SaveWithTransaction(CustomerOrderTDS p_updateContainer, bool p_dbConcurrency)
        {

            using (CustomersAdapter.Connection)
            {
                //--- Open the Connection before starting the transaction
                CustomersAdapter.Connection.Open();

                //--- Propagate the open connection to the OrdersAdapter
                OrdersAdapter.Connection = CustomersAdapter.Connection;
                
                //--- Create the Transaction
                SqlTransaction trans = CustomersAdapter.Connection.BeginTransaction(IsolationLevel.ReadCommitted);

                trans = CustomersAdapter.BeginTransaction(CustomersAdapter.Connection, trans);
                trans = OrdersAdapter.BeginTransaction(OrdersAdapter.Connection, trans);

                try
                {
                    int numUpdated = 0;

                    if (!p_dbConcurrency)
                    {
                        //--- Step1 : Delete Child Rows (Orders)
                        numUpdated += OrdersAdapter.Update(p_updateContainer.Orders.Select("", "", DataViewRowState.Deleted));

                        //--- Step2 : Delete Parent Rows (Customers)
                        numUpdated += CustomersAdapter.Update(p_updateContainer.Customers.Select("", "", DataViewRowState.Deleted));

                        //--- Step3 : Insert/Update Parent Rows (Customers)
                        numUpdated += CustomersAdapter.Update(p_updateContainer.Customers.Select("", "", DataViewRowState.CurrentRows));

                        //--- Step4 : Insert/Update Child Rows (Orders)
                        numUpdated += OrdersAdapter.Update(p_updateContainer.Orders.Select("", "", DataViewRowState.CurrentRows));
                    }
                    else
                    {
                        //--- Update after Concurrency Error

                        //--- Step1 : Update Customer Data

                        CustomerOrderTDS.CustomersRow[] customerRows = 
                            (CustomerOrderTDS.CustomersRow[])p_updateContainer.Customers.Select("", "", DataViewRowState.ModifiedCurrent);

                        if (customerRows != null)
                        {
                            foreach (CustomerOrderTDS.CustomersRow customer in customerRows)
                            {

                                if (customer.IsRegionIDNull())
                                    customer.RegionID = 0;

                                
                                numUpdated += CustomersAdapter.UpdateCustomerAfterDbConcurrency(
                                    customer.CustomerID,
                                    customer.CompanyName,
                                    customer.ContactName,
                                    customer.ContactTitle,
                                    customer.Address,
                                    customer.City,
                                    customer.Region,
                                    customer.PostalCode,
                                    customer.Country,
                                    customer.Phone,
                                    customer.Fax,
                                    customer.RegionID,
                                    customer.CustomerID);
                            }
                        }

                        //--- Step2 : Update Order Data

                        CustomerOrderTDS.OrdersRow[] orderRows =
                            (CustomerOrderTDS.OrdersRow[])p_updateContainer.Orders.Select("", "", DataViewRowState.ModifiedCurrent);

                        if (orderRows != null)
                        {
                            foreach (CustomerOrderTDS.OrdersRow order in orderRows)
                            {
                                if (order.IsEmployeeIDNull())
                                    order.EmployeeID = 0;
                                if (order.IsFreightNull())
                                    order.Freight = 0;
                                if (order.IsOrderDateNull()) 
                                    order.OrderDate = DateTime.Now;
                                if (order.IsRequiredDateNull())
                                    order.RequiredDate = DateTime.Now;
                                if (order.IsShippedDateNull())
                                    order.ShippedDate = DateTime.Now;
                                if (order.IsShipViaNull())
                                    order.ShipVia = 0;


                                numUpdated += OrdersAdapter.UpdateOrderAfterDbConcurrency(
                                    order.CustomerID, order.EmployeeID, order.OrderDate, order.RequiredDate, order.ShippedDate, order.ShipVia,
                                    order.Freight, order.ShipName, order.ShipAddress, order.ShipCity, order.ShipRegion, order.ShipPostalCode,
                                    order.ShipCountry, order.OrderID, order.OrderID);

                            }
                        }


                    }

                    //--- Try to Commit the transactional update ...
                    trans.Commit();

                    //--- Return the Affected Rows
                    return numUpdated;


                }
                catch (Exception ex)
                {
                    //--- In case the update failed, RollBack the Transaction, which puts the data back in it's original state
                    trans.Rollback();

                    //--- Throw the exception
                    throw ex;
                }
                finally
                { 
                    //--- Clean Up Code
                    if (CustomersAdapter.Connection.State == ConnectionState.Open)
                    {
                        m_customersAdapter.Connection.Close();
                        m_customersAdapter.Connection = null;
                        m_ordersAdapter.Connection = null;
                    }

                    
                }
            }

        }

        #endregion "Public Interface"
    }
    
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Belgium Belgium
Working in the IT-Branch for more then 10 years now. Starting as a programmer in WinDev, moved to Progress and actualy working in .NET since 2003. At the moment i'm employed as a .NET architect and teamleader at BERCO N.V. at Ronse (Belgium). In my spare time, i'm a die hard mountainbiker and together with my son Jarne, we're climbing the hills in the "Flemish Ardens" and the wonderfull "Pays des Collines". I also enjoy "a p'tit Jack" (Jack Daniels Whiskey) or a "Duvel" (beer) for "l'après VTT !".

Comments and Discussions