public void ProcessOrders()
        {
            int batchSize = int.Parse(ConfigurationManager.AppSettings["BatchSize"].ToString());

            Console.Write(string.Format("Batch Size: {0}", batchSize));
            Console.ReadLine();
            Console.WriteLine();

            EConnectCreateOrderResponse response = null;
            List<RFIDPassMediaReissueStage> orders = GetRFIDPassMediaReissueStageByStatus(Enums.OrderStatus.Pending);
            List<RFIDPassMediaReissueDetailStage> orderDetails = null;

            foreach (RFIDPassMediaReissueStage order in orders)
            {
                orderDetails = GetRFIDPassMediaReissueDetailStageByPrimaryIPCode(order.PrimaryIPCode);

                //Process request
                try
                {
                    response = ProcessRequest(orderDetails);
                }
                catch(Exception ex)
                {
                    response = new EConnectCreateOrderResponse();
                    response.ResponseCode = 1;
                    response.ResponseMessage = ex.Message;
                }

                //Update DB
                try
                {
                    using (var context = new RTPContext())
                    {
                        order.StatusID = response.ResponseCode == 0 ? Enums.OrderStatus.Processed.GetHashCode() : Enums.OrderStatus.Error.GetHashCode();

                        //Parent record is success but there was an error with at least one of the household users
                        if (order.StatusID == Enums.OrderStatus.Processed.GetHashCode() && response.EConnectFulfillOrderResponse.EConnectFulfillOrderDetailResponses.Where(d => d.ResponseCode != 0).Count() > 0)
                        {
                            order.StatusID = Enums.OrderStatus.PartialError.GetHashCode();
                        }

                        order.UpdateDate = DateTime.Now;
                        order.ErrorMessage = response.ResponseMessage;
                        if (response.SupplierOrderID > 0)
                        {
                            order.OrderID = response.SupplierOrderID;
                        }

                        context.RFIDPassMediaReissueStages.Attach(order);
                        context.Entry(order).State = EntityState.Modified;

                        //If null then the order was never created so don't update RFIDPassMediaReissueDetailStage table
                        if (response.EConnectFulfillOrderResponse != null)
                        {
                            foreach (RFIDPassMediaReissueDetailStage detail in orderDetails)
                            {
                                EConnectFulfillOrderDetailResponse responseDetail = response.EConnectFulfillOrderResponse.EConnectFulfillOrderDetailResponses.Where(d => d.IPCode == detail.IPCode).FirstOrDefault();

                                detail.ErrorMessage = responseDetail.ResponseMessage;
                                detail.StatusID = responseDetail.ResponseCode == 0 ? Enums.OrderStatus.Processed.GetHashCode() : Enums.OrderStatus.Error.GetHashCode();

                                context.RFIDPassMediaReissueDetailStages.Attach(detail);
                                context.Entry(detail).State = EntityState.Modified;
                            }
                        }

                        context.SaveChanges();

                        if (order.StatusID == Enums.OrderStatus.Processed.GetHashCode())
                        {
                            Console.WriteLine(string.Format("SUCCESS for PrimaryIPCode: {0}", order.PrimaryIPCode));
                        }
                        else
                        {
                            Console.WriteLine(string.Format("ERROR for PrimaryIPCode: {0}", order.PrimaryIPCode));
                        }
                        Console.WriteLine();
                    }
                }
                catch(Exception ex)
                {
                    string message = ex.Message + "\r\n" + ex.StackTrace + "\r\n DistributorOrderID: " + (string.IsNullOrEmpty(response.DistributorOrderID) ? "" : response.DistributorOrderID);
                    message += "\r\n PrimaryIPCode:" + order.PrimaryIPCode.ToString();

                  //  System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\RFID.Order.Console_ERROR.txt", true);
                   // file.WriteLine(message);
                    //file.Close();

                    //throw ex;

                    Console.WriteLine("Exception during SQL save: " + message);
                    Console.ReadLine();
                    //Console.WriteLine();
                }
            }
        }
        private EConnectCreateOrderResponse ExecuteConnectCreateOrder(string requestXML)
        {
            EConnectCreateOrderResponse response = new EConnectCreateOrderResponse();
            ConnectService.SupplierServiceSoapClient myConnect = new ConnectService.SupplierServiceSoapClient();

            Console.WriteLine(string.Format("BEGIN Invoke Connect Service to create order"));
            Console.WriteLine();

            string responseXML = myConnect.ProcessMessage(requestXML);

            Console.WriteLine(string.Format("END Invoke Connect Service to create order"));
            Console.WriteLine();

            XmlDocument xml = new XmlDocument();
            xml.LoadXml(responseXML);

            XmlNodeList xnList = xml.SelectNodes("/RTPSA_CreateOrderRS");

            foreach (XmlNode xn in xnList)
            {
                XmlNode nodeCode = xn.SelectSingleNode("ResponseCode");
                XmlNode nodeMessage = xn.SelectSingleNode("ResponseMessage");
                XmlNode nodeOrderID = xn.SelectSingleNode("SupplierOrderId");
                XmlNode nodeOrder = xn.SelectSingleNode("Order");

                if (nodeCode != null)
                {
                    response.ResponseCode = int.Parse(nodeCode.InnerText);
                }
                if (nodeMessage != null)
                {
                    response.ResponseMessage = nodeMessage.InnerText;
                }
                if (nodeOrderID != null)
                {
                    response.SupplierOrderID = int.Parse(nodeOrderID.InnerText);
                }
                if (nodeOrder != null && nodeOrder.Attributes["DistributorOrderId"] != null)
                {
                    response.DistributorOrderID = nodeOrder.Attributes["DistributorOrderId"].Value;
                }
            }

            return response;
        }