public static ShippingHeader SaveOrUpdateShippingHeader(string areaCode, string shipNumber, string shipTo, DateTime shipDate, string dealerCode, int itemCount, string createBy)
        {
            IDao<ShippingHeader, long> dao;
            List<ShippingHeader> list;
            ShippingHeader SH;

            dao = DaoFactory.GetDao<ShippingHeader, long>();
               dao.SetCriteria(new ICriterion[] { Expression.Eq("Shippingnumber", shipNumber.Trim()) });

            // check shippingNumber for existing;
            list = dao.GetAll();

            var db =DCFactory.GetDataContext<VehicleDataContext>();

                var query = (from sh in db.ShippingHeaders
                            where sh.ShippingNumber.Equals(shipNumber.Trim())
                            select new ShippingHeader
                                       {
                                           Areacode = sh.AreaCode,
                                           Createdby = sh.CreatedBy,
                                           Createddate = sh.CreatedDate,
                                           Dealercode = sh.DealerCode,
                                           Id = sh.ShippingId,
                                           Itemcount = sh.ItemCount,
                                           Shippingdate = sh.ShippingDate,
                                           Shippingnumber = sh.ShippingTo
                                       }).ToList();
                //if (query.Count() > 0) SH = (ShippingHeader)list[0];
                if (query.Count() > 0) SH = (ShippingHeader)query[0];
            else
            {
                SH = new ShippingHeader();
                SH.Createddate = DateTime.Now;
                SH.Createdby = createBy;
            }

            SH.Shippingnumber = shipNumber;
            SH.Shippingto = shipTo;
            SH.Shippingdate = shipDate;
            SH.Dealercode = dealerCode;
            SH.Areacode = areaCode;
            if (SH.Itemcount < itemCount) SH.Itemcount = itemCount;

            try
            {
                return dao.SaveOrUpdate(SH);
            }
            catch
            {
                return null;
            }
        }
    private ShippingHeader GetShippingHeader(string shipNum, out bool onTipTop)
    {
        string shipNumber = shipNum.ToUpper().Trim();

        // get shipheader from VDMS
        ShippingHeader SH = VDMS_ShippingHeader(shipNumber);
        VDMS.I.Entity.IShippingHeader drSH = TipTop_ShippingHeader(shipNumber);

        if (SH == null)  // not found in VDMS
        {
            onTipTop = drSH != null;
            if (!onTipTop) return null;

            // copy drSH to SH ....
            SH = new ShippingHeader();
            SH.Shippingnumber = drSH.IssueNumber;   //drSH["ShippingNumber"].ToString();
            SH.Shippingdate = drSH.ShipDate;        // Convert.ToDateTime(drSH["ShippingDate"].ToString());
            SH.Itemcount = 0;  // Convert.ToInt32(drSH["ItemCount"].ToString());
            SH.Shippingto = drSH.BranchCode;// drSH["ShippingTo"].ToString();
            ShippingAddress = drSH.ShippingAddress; //["ShippingAddress"].ToString();
            SH.Dealercode = drSH.DealerCode;        // drSH["DealerCode"].ToString();
            ViewState[VS_BranchCode] = drSH.BranchCode;// drSH["BranchCode"].ToString();
        }
        else    // already exist in VDMS
        {
            onTipTop = false;
            if (drSH != null)
            {
                ViewState[VS_BranchCode] = drSH.BranchCode; // drSH["BranchCode"].ToString();
            }
        }

        // after all: save infos and return
        ViewState[VS_ShippingNumber] = SH.Shippingnumber;
        ViewState[VS_ShipID] = SH.Id;
        ViewState[VS_ExportDate] = drSH.ShipDate;   // drSH["ShippingDate"];
        ViewState[VS_BaseShipDate] = drSH.ShipDate; //drSH["ShippingDate"];

        if ((DateTime)ViewState[VS_BaseShipDate] > DateTime.Now)
            Validator.SetDateRange(rvImportDate, DateTime.Now, DateTime.Now, true);
        else
            Validator.SetDateRange(rvImportDate, (DateTime)ViewState[VS_BaseShipDate], DateTime.Now, true);

        return SH;
    }