public static bool InsertOrUpdateOneTransaction(EbayTransactionType trans)
        {
            if (trans.IsValid() == false)
            {
                return(false);
            }

            bool result = true;

            EbayTransactionType transLoc = EbayTransactionDAL.GetOneTransaction(trans.OrderLineItemId);

            if (transLoc != null)
            {
                result = UpdateOneTransaction(trans);
            }
            else
            {
                result = InsertOneTransaction(trans);
            }

            return(true);
        }
        // Private to prevent misuse, use public InsertOrUpdateOneTransaction instead!!!!!!!
        private static bool InsertOneTransaction(EbayTransactionType trans)
        {
            if (trans.IsValid() == false)
                return false;

            EbayTransactionType transLoc = EbayTransactionDAL.GetOneTransaction(trans.OrderLineItemId);
            if (transLoc != null)
            {
                Logger.WriteSystemLog(string.Format("Transaction already existed in database: userId={0}, itemTitle={1}",
                   trans.BuyerId, trans.ItemTitle));
                return false;
            }

            IDbCommand cmd = DataFactory.CreateCommand(null);
            cmd.CommandText = @"insert into [Transaction] (SellerName, OrderId, OrderLineItemId, EbayTransactionId, EbayRecordId, BuyerId, BuyerRating,"
                    + "BuyerCountryEbayCode, BuyerCountry4PXCode,"
                    + "BuyerCountry, BuyerCompanyName, BuyerName, BuyerStateOrProvince, BuyerCity,"
                    + "BuyerTel, BuyerMail, BuyerPostalCode, BuyerAddress, BuyerAddressCompact, BuyerAddressLine1, BuyerAddressLine2,"
                    + "BuyerPayPal,ItemId, ItemTitle, ItemSKU, ItemPrice, SaleQuantity, SalePrice, TotalPrice, CurrencyId,"
                    + "SaleDate, SaleDateCN, IsPaid, PaidDate, IsShipped, ShippedDate, ShippingServiceCode, ShippingService, ShippingTrackingNo, ShippingCost, FinalValueFee, PayPalFee,"
                    + "IsReceived, IsBuyerLeftFeedback, IsSellerLeftFeedback, IsNeedAttention, MessageStatus, IsContactedBuyer,"
                    + "LastContactedBuyerDate, IsResendReplacement, UserComment) values ("
                    + "@SellerName, @OrderId, @OrderLineItemId, @EbayTransactionId, @EbayRecordId, @BuyerId, @BuyerRating,"
                    + "@BuyerCountryEbayCode, @BuyerCountry4PXCode,"
                    + "@BuyerCountry, @BuyerCompanyName, @BuyerName, @BuyerStateOrProvince, @BuyerCity,"
                    + "@BuyerTel, @BuyerMail, @BuyerPostalCode, @BuyerAddress, @BuyerAddressCompact, @BuyerAddressLine1, @BuyerAddressLine2,"
                    + "@BuyerPayPal, @ItemId, @ItemTitle, @ItemSKU, @ItemPrice, @SaleQuantity, @SalePrice, @TotalPrice, @CurrencyId,"
                    + "@SaleDate, @SaleDateCN, @IsPaid, @PaidDate, @IsShipped, @ShippedDate, @ShippingServiceCode, @ShippingService, @ShippingTrackingNo, @ShippingCost, @FinalValueFee, @PayPalFee,"
                    + "@IsReceived, @IsBuyerLeftFeedback, @IsSellerLeftFeedback, @IsNeedAttention, @MessageStatus, @IsContactedBuyer,"
                    + "@LastContactedBuyerDate, @IsResendReplacement, @UserComment)";

            DataFactory.AddCommandParam(cmd, "@SellerName", DbType.String, trans.SellerName);
            DataFactory.AddCommandParam(cmd, "@OrderId", DbType.String, trans.OrderId);
            DataFactory.AddCommandParam(cmd, "@OrderLineItemId", DbType.String, trans.OrderLineItemId);
            DataFactory.AddCommandParam(cmd, "@EbayTransactionId", DbType.String, trans.EbayTransactionId);
            DataFactory.AddCommandParam(cmd, "@EbayRecordId", DbType.Int32, trans.EbayRecordId);
            DataFactory.AddCommandParam(cmd, "@BuyerId", DbType.String, trans.BuyerId);
            DataFactory.AddCommandParam(cmd, "@BuyerRating", DbType.Int32, trans.BuyerRating);

            DataFactory.AddCommandParam(cmd, "@BuyerCountryEbayCode", DbType.String, StringUtil.GetSafeString(trans.BuyerCountryEbayCode));
            DataFactory.AddCommandParam(cmd, "@BuyerCountry4PXCode", DbType.String, StringUtil.GetSafeString(trans.BuyerCountry4PXCode));
            DataFactory.AddCommandParam(cmd, "@BuyerCountry", DbType.String, StringUtil.GetSafeString(trans.BuyerCountry));
            DataFactory.AddCommandParam(cmd, "@BuyerCompanyName", DbType.String, StringUtil.GetSafeString(trans.BuyerCompanyName));
            DataFactory.AddCommandParam(cmd, "@BuyerName", DbType.String, trans.BuyerName);

            DataFactory.AddCommandParam(cmd, "@BuyerStateOrProvince", DbType.String, trans.BuyerStateOrProvince);
            DataFactory.AddCommandParam(cmd, "@BuyerCity", DbType.String, trans.BuyerCity);
            DataFactory.AddCommandParam(cmd, "@BuyerTel", DbType.String, StringUtil.GetSafeString(trans.BuyerTel));
            DataFactory.AddCommandParam(cmd, "@BuyerMail", DbType.String, StringUtil.GetSafeString(trans.BuyerMail));
            DataFactory.AddCommandParam(cmd, "@BuyerPostalCode", DbType.String, StringUtil.GetSafeString(trans.BuyerPostalCode));

            DataFactory.AddCommandParam(cmd, "@BuyerAddress", DbType.String, StringUtil.GetSafeString(trans.BuyerAddress));
            DataFactory.AddCommandParam(cmd, "@BuyerAddressCompact", DbType.String, StringUtil.GetSafeString(trans.BuyerAddressCompact));
            DataFactory.AddCommandParam(cmd, "@BuyerAddressLine1", DbType.String, StringUtil.GetSafeString(trans.BuyerAddressLine1));
            DataFactory.AddCommandParam(cmd, "@BuyerAddressLine2", DbType.String, StringUtil.GetSafeString(trans.BuyerAddressLine2));
            DataFactory.AddCommandParam(cmd, "@BuyerPayPal", DbType.String, StringUtil.GetSafeString(trans.BuyerPayPal));
            DataFactory.AddCommandParam(cmd, "@ItemId", DbType.String, StringUtil.GetSafeString(trans.ItemId));
            DataFactory.AddCommandParam(cmd, "@ItemTitle", DbType.String, StringUtil.GetSafeString(trans.ItemTitle));

            DataFactory.AddCommandParam(cmd, "@ItemSKU", DbType.String, StringUtil.GetSafeString(trans.ItemSKU));
            DataFactory.AddCommandParam(cmd, "@ItemPrice", DbType.Double, trans.ItemPrice);
            DataFactory.AddCommandParam(cmd, "@SaleQuantity", DbType.Int32, trans.SaleQuantity);
            DataFactory.AddCommandParam(cmd, "@SalePrice", DbType.Double, trans.SalePrice);
            DataFactory.AddCommandParam(cmd, "@TotalPrice", DbType.Double, trans.TotalPrice);

            DataFactory.AddCommandParam(cmd, "@CurrencyId", DbType.String, StringUtil.GetSafeString(trans.CurrencyId));
            DataFactory.AddCommandParam(cmd, "@SaleDate", DbType.Date, StringUtil.GetSafeDateTime(trans.SaleDate).ToString());
            DataFactory.AddCommandParam(cmd, "@SaleDateCN", DbType.Date, StringUtil.GetSafeDateTime(trans.SaleDateCN).ToString());
            DataFactory.AddCommandParam(cmd, "@IsPaid", DbType.Boolean, trans.IsPaid);
            DataFactory.AddCommandParam(cmd, "@PaidDate", DbType.Date, StringUtil.GetSafeDateTime(trans.PaidDate).ToString());
            DataFactory.AddCommandParam(cmd, "@IsShipped", DbType.Boolean, trans.IsShipped);

            DataFactory.AddCommandParam(cmd, "@ShippedDate", DbType.Date, StringUtil.GetSafeDateTime(trans.ShippedDate).ToString());
            DataFactory.AddCommandParam(cmd, "@ShippingServiceCode", DbType.String, trans.ShippingServiceCode);
            DataFactory.AddCommandParam(cmd, "@ShippingService", DbType.String, trans.ShippingService);
            DataFactory.AddCommandParam(cmd, "@ShippingTrackingNo", DbType.String, trans.ShippingTrackingNo);

            DataFactory.AddCommandParam(cmd, "@ShippingCost", DbType.Double, trans.ShippingCost);
            DataFactory.AddCommandParam(cmd, "@FinalValueFee", DbType.Double, trans.FinalValueFee);
            DataFactory.AddCommandParam(cmd, "@PayPalFee", DbType.Double, trans.PayPalFee);

            DataFactory.AddCommandParam(cmd, "@IsReceived", DbType.Boolean, trans.IsReceived);
            DataFactory.AddCommandParam(cmd, "@IsBuyerLeftFeedback", DbType.Boolean, trans.IsBuyerLeftFeedback);
            DataFactory.AddCommandParam(cmd, "@IsSellerLeftFeedback", DbType.Boolean, trans.IsSellerLeftFeedback);
            DataFactory.AddCommandParam(cmd, "@IsNeedAttention", DbType.Boolean, trans.IsNeedAttention);
            DataFactory.AddCommandParam(cmd, "@MessageStatus", DbType.Int32, trans.MessageStatus);
            DataFactory.AddCommandParam(cmd, "@IsContactedBuyer", DbType.Boolean, trans.IsContactedBuyer);

            DataFactory.AddCommandParam(cmd, "@LastContactedBuyerDate", DbType.Date, StringUtil.GetSafeDateTime(trans.LastContactedBuyerDate).ToString());
            DataFactory.AddCommandParam(cmd, "@IsResendReplacement", DbType.Boolean, trans.IsResendReplacement);
            DataFactory.AddCommandParam(cmd, "@UserComment", DbType.String, StringUtil.GetSafeString(trans.UserComment));

            bool result = false;
            int newItemId = 0;

            try
            {
                if (DataFactory.DbConnection.State == ConnectionState.Closed)
                    DataFactory.DbConnection.Open();
                cmd.ExecuteNonQuery();

                IDbCommand cmdNewID = DataFactory.CreateCommand("SELECT @@IDENTITY");

                // Retrieve the Autonumber and store it in the CategoryID column.
                object obj = cmdNewID.ExecuteScalar();
                Int32.TryParse(obj.ToString(), out newItemId);
                result = newItemId > 0;
            }
            catch (DataException ex)
            {
                // Write to log here.
                Logger.WriteSystemLog(string.Format("Error : {0}", ex.Message));
                trans.dump();
                result = false;
            }
            finally
            {
                if (DataFactory.DbConnection.State == ConnectionState.Open)
                    DataFactory.DbConnection.Close();
            }

            return result;
        }
        public static bool UpdateOneTransaction(EbayTransactionType trans)
        {
            if (trans.IsValid() == false)
                return false;

            EbayTransactionType transLoc = EbayTransactionDAL.GetOneTransaction(trans.OrderLineItemId);
            if (transLoc == null)
            {
                Logger.WriteSystemLog(string.Format("No transaction with EbayTransactionId={0}", trans.EbayTransactionId));
                return false;
            }

            bool needUpdateIsPaid = false;
            if (trans.IsPaid != transLoc.IsPaid)
            {
                Logger.WriteSystemLog(string.Format("Update one transaction with id={0}, set isPaid={1}", trans.EbayTransactionId, trans.IsPaid));
                needUpdateIsPaid = true;
            }

            bool needUpdateShipped = false;
            if (trans.IsShipped != transLoc.IsShipped)
            {
                Logger.WriteSystemLog(string.Format("Update one transaction with id={0}, set IsShipped={1}", trans.EbayTransactionId, trans.IsShipped));
                needUpdateShipped = true;
            }

            bool needUpdateBuyerFeedbackLeft = false;
            if (trans.IsBuyerLeftFeedback != transLoc.IsBuyerLeftFeedback)
            {
                Logger.WriteSystemLog(string.Format("Update one transaction with id={0}, set BuyerFeedbackLeft={1}", trans.EbayTransactionId, trans.IsBuyerLeftFeedback));
                needUpdateBuyerFeedbackLeft = true;
            }

            bool needUpdateSellerFeedbackLeft = false;
            if (trans.IsSellerLeftFeedback != transLoc.IsSellerLeftFeedback)
            {
                Logger.WriteSystemLog(string.Format("Update one transaction with id={0}, set SellerFeedbackLeft={1}", trans.EbayTransactionId, trans.IsSellerLeftFeedback));
                needUpdateSellerFeedbackLeft = true;
            }

            bool needUpdateSellerName = false;
            if (trans.SellerName == "")
                needUpdateSellerName = true;

            bool needUpdateTotalPrice = false;
            if (trans.TotalPrice != transLoc.TotalPrice)
                needUpdateTotalPrice = true;

            bool needUpdateSaleRecordId = false;
            if (trans.EbayRecordId != -1 && trans.EbayRecordId != transLoc.EbayRecordId)
                needUpdateSaleRecordId = true;

            bool needUpdateSaleDateCN = false;
            if (trans.SaleDateCN != null && 0 != trans.SaleDateCN.CompareTo(transLoc.SaleDateCN))
                needUpdateSaleDateCN = true;

            bool needUpdateTrackingNumber = false;
            if (trans.ShippingTrackingNo != null && trans.ShippingTrackingNo != transLoc.ShippingTrackingNo)
                needUpdateTrackingNumber = true;

            bool needUpdateShippingDate = false;
            if (trans.ShippedDate != transLoc.ShippedDate)
                needUpdateShipped = true;

            if (!needUpdateIsPaid && !needUpdateShipped && !needUpdateBuyerFeedbackLeft &&
                !needUpdateSellerFeedbackLeft && !needUpdateSellerName && !needUpdateTotalPrice &&
                !needUpdateSaleRecordId && !needUpdateSaleDateCN && !needUpdateTrackingNumber && !needUpdateShippingDate)
                return true;

            if (transLoc.TransactionId <= 0)
            {
                Logger.WriteSystemLog("Invalid transaction id.");
                return false;
            }

            // CAUTION: we need to retain some column values in the old entry,
            //  such as the message status we calculated last time.
            if (transLoc.ItemSKU != null && transLoc.ItemSKU != "")
                trans.ItemSKU = transLoc.ItemSKU;
            if (transLoc.ShippingService != null && transLoc.ShippingService != "")
                trans.ShippingService = transLoc.ShippingService;
            if (transLoc.ShippingServiceCode != null && transLoc.ShippingServiceCode != "")
                trans.ShippingServiceCode = transLoc.ShippingServiceCode;
            trans.MessageStatus = transLoc.MessageStatus;

            bool result = UpdateOneTransactionInternal(transLoc.TransactionId, trans);
            return result;
        }
        public static bool InsertOrUpdateOneTransaction(EbayTransactionType trans)
        {
            if (trans.IsValid() == false)
                return false;

            bool result = true;

            EbayTransactionType transLoc = EbayTransactionDAL.GetOneTransaction(trans.OrderLineItemId);
            if (transLoc != null)
            {
                result = UpdateOneTransaction(trans);
            }
            else
            {
                result = InsertOneTransaction(trans);
            }

            return true;
        }
        // Private to prevent misuse, use public InsertOrUpdateOneTransaction instead!!!!!!!
        private static bool InsertOneTransaction(EbayTransactionType trans)
        {
            if (trans.IsValid() == false)
            {
                return(false);
            }

            EbayTransactionType transLoc = EbayTransactionDAL.GetOneTransaction(trans.OrderLineItemId);

            if (transLoc != null)
            {
                Logger.WriteSystemLog(string.Format("Transaction already existed in database: userId={0}, itemTitle={1}",
                                                    trans.BuyerId, trans.ItemTitle));
                return(false);
            }

            IDbCommand cmd = DataFactory.CreateCommand(null);

            cmd.CommandText = @"insert into [Transaction] (SellerName, OrderId, OrderLineItemId, EbayTransactionId, EbayRecordId, BuyerId, BuyerRating,"
                              + "BuyerCountryEbayCode, BuyerCountry4PXCode,"
                              + "BuyerCountry, BuyerCompanyName, BuyerName, BuyerStateOrProvince, BuyerCity,"
                              + "BuyerTel, BuyerMail, BuyerPostalCode, BuyerAddress, BuyerAddressCompact, BuyerAddressLine1, BuyerAddressLine2,"
                              + "BuyerPayPal,ItemId, ItemTitle, ItemSKU, ItemPrice, SaleQuantity, SalePrice, TotalPrice, CurrencyId,"
                              + "SaleDate, SaleDateCN, IsPaid, PaidDate, IsShipped, ShippedDate, ShippingServiceCode, ShippingService, ShippingTrackingNo, ShippingCost, FinalValueFee, PayPalFee,"
                              + "IsReceived, IsBuyerLeftFeedback, IsSellerLeftFeedback, IsNeedAttention, MessageStatus, IsContactedBuyer,"
                              + "LastContactedBuyerDate, IsResendReplacement, UserComment) values ("
                              + "@SellerName, @OrderId, @OrderLineItemId, @EbayTransactionId, @EbayRecordId, @BuyerId, @BuyerRating,"
                              + "@BuyerCountryEbayCode, @BuyerCountry4PXCode,"
                              + "@BuyerCountry, @BuyerCompanyName, @BuyerName, @BuyerStateOrProvince, @BuyerCity,"
                              + "@BuyerTel, @BuyerMail, @BuyerPostalCode, @BuyerAddress, @BuyerAddressCompact, @BuyerAddressLine1, @BuyerAddressLine2,"
                              + "@BuyerPayPal, @ItemId, @ItemTitle, @ItemSKU, @ItemPrice, @SaleQuantity, @SalePrice, @TotalPrice, @CurrencyId,"
                              + "@SaleDate, @SaleDateCN, @IsPaid, @PaidDate, @IsShipped, @ShippedDate, @ShippingServiceCode, @ShippingService, @ShippingTrackingNo, @ShippingCost, @FinalValueFee, @PayPalFee,"
                              + "@IsReceived, @IsBuyerLeftFeedback, @IsSellerLeftFeedback, @IsNeedAttention, @MessageStatus, @IsContactedBuyer,"
                              + "@LastContactedBuyerDate, @IsResendReplacement, @UserComment)";

            DataFactory.AddCommandParam(cmd, "@SellerName", DbType.String, trans.SellerName);
            DataFactory.AddCommandParam(cmd, "@OrderId", DbType.String, trans.OrderId);
            DataFactory.AddCommandParam(cmd, "@OrderLineItemId", DbType.String, trans.OrderLineItemId);
            DataFactory.AddCommandParam(cmd, "@EbayTransactionId", DbType.String, trans.EbayTransactionId);
            DataFactory.AddCommandParam(cmd, "@EbayRecordId", DbType.Int32, trans.EbayRecordId);
            DataFactory.AddCommandParam(cmd, "@BuyerId", DbType.String, trans.BuyerId);
            DataFactory.AddCommandParam(cmd, "@BuyerRating", DbType.Int32, trans.BuyerRating);

            DataFactory.AddCommandParam(cmd, "@BuyerCountryEbayCode", DbType.String, StringUtil.GetSafeString(trans.BuyerCountryEbayCode));
            DataFactory.AddCommandParam(cmd, "@BuyerCountry4PXCode", DbType.String, StringUtil.GetSafeString(trans.BuyerCountry4PXCode));
            DataFactory.AddCommandParam(cmd, "@BuyerCountry", DbType.String, StringUtil.GetSafeString(trans.BuyerCountry));
            DataFactory.AddCommandParam(cmd, "@BuyerCompanyName", DbType.String, StringUtil.GetSafeString(trans.BuyerCompanyName));
            DataFactory.AddCommandParam(cmd, "@BuyerName", DbType.String, trans.BuyerName);

            DataFactory.AddCommandParam(cmd, "@BuyerStateOrProvince", DbType.String, trans.BuyerStateOrProvince);
            DataFactory.AddCommandParam(cmd, "@BuyerCity", DbType.String, trans.BuyerCity);
            DataFactory.AddCommandParam(cmd, "@BuyerTel", DbType.String, StringUtil.GetSafeString(trans.BuyerTel));
            DataFactory.AddCommandParam(cmd, "@BuyerMail", DbType.String, StringUtil.GetSafeString(trans.BuyerMail));
            DataFactory.AddCommandParam(cmd, "@BuyerPostalCode", DbType.String, StringUtil.GetSafeString(trans.BuyerPostalCode));

            DataFactory.AddCommandParam(cmd, "@BuyerAddress", DbType.String, StringUtil.GetSafeString(trans.BuyerAddress));
            DataFactory.AddCommandParam(cmd, "@BuyerAddressCompact", DbType.String, StringUtil.GetSafeString(trans.BuyerAddressCompact));
            DataFactory.AddCommandParam(cmd, "@BuyerAddressLine1", DbType.String, StringUtil.GetSafeString(trans.BuyerAddressLine1));
            DataFactory.AddCommandParam(cmd, "@BuyerAddressLine2", DbType.String, StringUtil.GetSafeString(trans.BuyerAddressLine2));
            DataFactory.AddCommandParam(cmd, "@BuyerPayPal", DbType.String, StringUtil.GetSafeString(trans.BuyerPayPal));
            DataFactory.AddCommandParam(cmd, "@ItemId", DbType.String, StringUtil.GetSafeString(trans.ItemId));
            DataFactory.AddCommandParam(cmd, "@ItemTitle", DbType.String, StringUtil.GetSafeString(trans.ItemTitle));

            DataFactory.AddCommandParam(cmd, "@ItemSKU", DbType.String, StringUtil.GetSafeString(trans.ItemSKU));
            DataFactory.AddCommandParam(cmd, "@ItemPrice", DbType.Double, trans.ItemPrice);
            DataFactory.AddCommandParam(cmd, "@SaleQuantity", DbType.Int32, trans.SaleQuantity);
            DataFactory.AddCommandParam(cmd, "@SalePrice", DbType.Double, trans.SalePrice);
            DataFactory.AddCommandParam(cmd, "@TotalPrice", DbType.Double, trans.TotalPrice);

            DataFactory.AddCommandParam(cmd, "@CurrencyId", DbType.String, StringUtil.GetSafeString(trans.CurrencyId));
            DataFactory.AddCommandParam(cmd, "@SaleDate", DbType.Date, StringUtil.GetSafeDateTime(trans.SaleDate).ToString());
            DataFactory.AddCommandParam(cmd, "@SaleDateCN", DbType.Date, StringUtil.GetSafeDateTime(trans.SaleDateCN).ToString());
            DataFactory.AddCommandParam(cmd, "@IsPaid", DbType.Boolean, trans.IsPaid);
            DataFactory.AddCommandParam(cmd, "@PaidDate", DbType.Date, StringUtil.GetSafeDateTime(trans.PaidDate).ToString());
            DataFactory.AddCommandParam(cmd, "@IsShipped", DbType.Boolean, trans.IsShipped);

            DataFactory.AddCommandParam(cmd, "@ShippedDate", DbType.Date, StringUtil.GetSafeDateTime(trans.ShippedDate).ToString());
            DataFactory.AddCommandParam(cmd, "@ShippingServiceCode", DbType.String, trans.ShippingServiceCode);
            DataFactory.AddCommandParam(cmd, "@ShippingService", DbType.String, trans.ShippingService);
            DataFactory.AddCommandParam(cmd, "@ShippingTrackingNo", DbType.String, trans.ShippingTrackingNo);

            DataFactory.AddCommandParam(cmd, "@ShippingCost", DbType.Double, trans.ShippingCost);
            DataFactory.AddCommandParam(cmd, "@FinalValueFee", DbType.Double, trans.FinalValueFee);
            DataFactory.AddCommandParam(cmd, "@PayPalFee", DbType.Double, trans.PayPalFee);

            DataFactory.AddCommandParam(cmd, "@IsReceived", DbType.Boolean, trans.IsReceived);
            DataFactory.AddCommandParam(cmd, "@IsBuyerLeftFeedback", DbType.Boolean, trans.IsBuyerLeftFeedback);
            DataFactory.AddCommandParam(cmd, "@IsSellerLeftFeedback", DbType.Boolean, trans.IsSellerLeftFeedback);
            DataFactory.AddCommandParam(cmd, "@IsNeedAttention", DbType.Boolean, trans.IsNeedAttention);
            DataFactory.AddCommandParam(cmd, "@MessageStatus", DbType.Int32, trans.MessageStatus);
            DataFactory.AddCommandParam(cmd, "@IsContactedBuyer", DbType.Boolean, trans.IsContactedBuyer);

            DataFactory.AddCommandParam(cmd, "@LastContactedBuyerDate", DbType.Date, StringUtil.GetSafeDateTime(trans.LastContactedBuyerDate).ToString());
            DataFactory.AddCommandParam(cmd, "@IsResendReplacement", DbType.Boolean, trans.IsResendReplacement);
            DataFactory.AddCommandParam(cmd, "@UserComment", DbType.String, StringUtil.GetSafeString(trans.UserComment));

            bool result    = false;
            int  newItemId = 0;

            try
            {
                if (DataFactory.DbConnection.State == ConnectionState.Closed)
                {
                    DataFactory.DbConnection.Open();
                }
                cmd.ExecuteNonQuery();

                IDbCommand cmdNewID = DataFactory.CreateCommand("SELECT @@IDENTITY");

                // Retrieve the Autonumber and store it in the CategoryID column.
                object obj = cmdNewID.ExecuteScalar();
                Int32.TryParse(obj.ToString(), out newItemId);
                result = newItemId > 0;
            }
            catch (DataException ex)
            {
                // Write to log here.
                Logger.WriteSystemLog(string.Format("Error : {0}", ex.Message));
                trans.dump();
                result = false;
            }
            finally
            {
                if (DataFactory.DbConnection.State == ConnectionState.Open)
                {
                    DataFactory.DbConnection.Close();
                }
            }

            return(result);
        }   // InsertOneTransaction
        public static bool UpdateOneTransaction(EbayTransactionType trans)
        {
            if (trans.IsValid() == false)
            {
                return(false);
            }

            EbayTransactionType transLoc = EbayTransactionDAL.GetOneTransaction(trans.OrderLineItemId);

            if (transLoc == null)
            {
                Logger.WriteSystemLog(string.Format("No transaction with EbayTransactionId={0}", trans.EbayTransactionId));
                return(false);
            }

            bool needUpdateIsPaid = false;

            if (trans.IsPaid != transLoc.IsPaid)
            {
                Logger.WriteSystemLog(string.Format("Update one transaction with id={0}, set isPaid={1}", trans.EbayTransactionId, trans.IsPaid));
                needUpdateIsPaid = true;
            }

            bool needUpdateShipped = false;

            if (trans.IsShipped != transLoc.IsShipped)
            {
                Logger.WriteSystemLog(string.Format("Update one transaction with id={0}, set IsShipped={1}", trans.EbayTransactionId, trans.IsShipped));
                needUpdateShipped = true;
            }

            bool needUpdateBuyerFeedbackLeft = false;

            if (trans.IsBuyerLeftFeedback != transLoc.IsBuyerLeftFeedback)
            {
                Logger.WriteSystemLog(string.Format("Update one transaction with id={0}, set BuyerFeedbackLeft={1}", trans.EbayTransactionId, trans.IsBuyerLeftFeedback));
                needUpdateBuyerFeedbackLeft = true;
            }

            bool needUpdateSellerFeedbackLeft = false;

            if (trans.IsSellerLeftFeedback != transLoc.IsSellerLeftFeedback)
            {
                Logger.WriteSystemLog(string.Format("Update one transaction with id={0}, set SellerFeedbackLeft={1}", trans.EbayTransactionId, trans.IsSellerLeftFeedback));
                needUpdateSellerFeedbackLeft = true;
            }

            bool needUpdateSellerName = false;

            if (trans.SellerName == "")
            {
                needUpdateSellerName = true;
            }

            bool needUpdateTotalPrice = false;

            if (trans.TotalPrice != transLoc.TotalPrice)
            {
                needUpdateTotalPrice = true;
            }

            bool needUpdateSaleRecordId = false;

            if (trans.EbayRecordId != -1 && trans.EbayRecordId != transLoc.EbayRecordId)
            {
                needUpdateSaleRecordId = true;
            }

            bool needUpdateSaleDateCN = false;

            if (trans.SaleDateCN != null && 0 != trans.SaleDateCN.CompareTo(transLoc.SaleDateCN))
            {
                needUpdateSaleDateCN = true;
            }

            bool needUpdateTrackingNumber = false;

            if (trans.ShippingTrackingNo != null && trans.ShippingTrackingNo != transLoc.ShippingTrackingNo)
            {
                needUpdateTrackingNumber = true;
            }

            bool needUpdateShippingDate = false;

            if (trans.ShippedDate != transLoc.ShippedDate)
            {
                needUpdateShipped = true;
            }

            if (!needUpdateIsPaid && !needUpdateShipped && !needUpdateBuyerFeedbackLeft &&
                !needUpdateSellerFeedbackLeft && !needUpdateSellerName && !needUpdateTotalPrice &&
                !needUpdateSaleRecordId && !needUpdateSaleDateCN && !needUpdateTrackingNumber && !needUpdateShippingDate)
            {
                return(true);
            }

            if (transLoc.TransactionId <= 0)
            {
                Logger.WriteSystemLog("Invalid transaction id.");
                return(false);
            }

            // CAUTION: we need to retain some column values in the old entry,
            //  such as the message status we calculated last time.
            if (transLoc.ItemSKU != null && transLoc.ItemSKU != "")
            {
                trans.ItemSKU = transLoc.ItemSKU;
            }
            if (transLoc.ShippingService != null && transLoc.ShippingService != "")
            {
                trans.ShippingService = transLoc.ShippingService;
            }
            if (transLoc.ShippingServiceCode != null && transLoc.ShippingServiceCode != "")
            {
                trans.ShippingServiceCode = transLoc.ShippingServiceCode;
            }
            trans.MessageStatus = transLoc.MessageStatus;

            bool result = UpdateOneTransactionInternal(transLoc.TransactionId, trans);

            return(result);
        }