//reads the batch order file and returns //an array list of orders private ArrayList processFile(string fileName, out List <CustomError> alWarnings) { alWarnings = new List <CustomError>(); bool bValid = true; ArrayList alOrders = null; string csvFileName = null; //no conversion needed if .csv if (fileName.Contains(".csv")) { csvFileName = fileName; } else { //convert to .csv ApplicationClass ExcelApp = null; try { ExcelApp = new ApplicationClass(); Workbook uploadedWorkBook = ExcelApp.Workbooks.Open(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); csvFileName = fileName + ".csv"; File.Delete(csvFileName); uploadedWorkBook.SaveAs(csvFileName, XlFileFormat.xlCSV, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); uploadedWorkBook.Close(false, csvFileName, false); } catch (Exception e) { bValid = false; Validators.Add(new CustomError("Error processing .xls file. Contact customer support. Error code 1")); EventLog woeLog = new EventLog("WebOrderEntry"); woeLog.Source = "WebOrderEntryApp"; string errorMessage = "Message\r\n" + e.Message.ToString() + "\r\n\r\n"; errorMessage += "Source\r\n" + e.Source + "\r\n\r\n"; errorMessage += "Target site\r\n" + e.TargetSite.ToString() + "\r\n\r\n"; errorMessage += "Stack trace\r\n" + e.StackTrace + "\r\n\r\n"; errorMessage += "ToString()\r\n\r\n" + e.ToString(); woeLog.WriteEntry(errorMessage, EventLogEntryType.Error, 1); return(null); } finally { if (ExcelApp != null) { ExcelApp.Workbooks.Close(); ExcelApp.Quit(); } } } alOrders = new ArrayList(); warehouse_shipping_ordersWarehouse_shipping_order eachOrder = null; OrderValidation orderValidation = null; int iCurrentRow = 1; string sCurrentRow = "1"; // open the file which is a CSV file with no headers using (CsvReader csv = new CsvReader(new StreamReader(csvFileName), false)) { int fieldCount = csv.FieldCount; //string[] headers = csv.GetFieldHeaders(); while (csv.ReadNextRecord()) { string sEachSegment = csv[0].Trim(); switch (sEachSegment) { case "HDR": #region validateHeader // New Order if (orderValidation != null) { eachOrder = orderValidation.getOrder(sCurrentRow); if (eachOrder != null) { alOrders.Add(eachOrder); } else { bValid = false; //Validators.Add(new CustomError("Invalid " + HDR + " field count=" + dr.FieldCount.ToString() + " should be " + HDR_FIELDCOUNT.ToString() + " row " + iCurrentRow + " column A.")); break; } } orderValidation = new OrderValidation(Validators); //set account id from session if (Session["AccountId"] != null) { orderValidation.AccountId = (string)Session["AccountId"]; } //process header record //check field count if (csv.FieldCount < HDR_FIELDCOUNT) { bValid = false; Validators.Add(new CustomError("Invalid " + HDR + " field count=" + csv.FieldCount.ToString() + " should be " + HDR_FIELDCOUNT.ToString() + " row " + iCurrentRow + " column A.")); break; } List <CustomError> eachOrderWarningList = orderValidation.validateHeader(csv, sCurrentRow, out bValid); if (eachOrderWarningList.Count > 0) { foreach (CustomError warning in eachOrderWarningList) { alWarnings.Add(warning); } } if (bValid == false) { break; } #endregion break; case "LOC": #region validateLoc //process address record //check field count if (csv.FieldCount < LOC_FIELDCOUNT) { bValid = false; Validators.Add(new CustomError("Invalid " + LOC + " field count=" + csv.FieldCount.ToString() + " should be " + LOC_FIELDCOUNT.ToString() + " row " + iCurrentRow + " column A.")); break; } if (!orderValidation.validateLoc(csv, sCurrentRow)) { bValid = false; break; } #endregion break; case "DET": #region validateDetail //process detail record //check field count if (csv.FieldCount < DET_FIELDCOUNT) { bValid = false; Validators.Add(new CustomError("Invalid " + DET + " field count=" + csv.FieldCount.ToString() + " should be " + DET_FIELDCOUNT.ToString() + " row " + iCurrentRow + " column A.")); break; } if (!orderValidation.validateDetail(csv, sCurrentRow)) { bValid = false; break; } #endregion break; case "ASN": #region validateASN //process asn record //check field count if (csv.FieldCount < ASN_FIELDCOUNT) { bValid = false; Validators.Add(new CustomError("Invalid " + ASN + " field count=" + csv.FieldCount.ToString() + " should be " + ASN_FIELDCOUNT.ToString() + " row " + iCurrentRow + " column A.")); break; } if (!orderValidation.validateAsn(csv, sCurrentRow)) { bValid = false; break; } #endregion break; case "REF": #region validateRefNbr //process header reference record //check field count if (csv.FieldCount < REF_FIELDCOUNT) { bValid = false; Validators.Add(new CustomError("Invalid " + REF + " field count=" + csv.FieldCount.ToString() + " should be " + REF_FIELDCOUNT.ToString() + " row " + iCurrentRow + " column A.")); break; } if (!orderValidation.validateRef(csv, sCurrentRow)) { bValid = false; break; } #endregion break; case "NTE": #region validateRefNbr //process note record //check field count if (csv.FieldCount < NTE_FIELDCOUNT) { bValid = false; Validators.Add(new CustomError("Invalid " + NTE + " field count=" + csv.FieldCount.ToString() + " should be " + NTE_FIELDCOUNT.ToString() + " row " + iCurrentRow + " column A.")); break; } if (!orderValidation.validateNote(csv, sCurrentRow)) { bValid = false; break; } #endregion break; default: bValid = false; Validators.Add(new CustomError("Invalid Segment Name" + sEachSegment + " - row " + iCurrentRow + " column A.")); break; } iCurrentRow++; sCurrentRow = Convert.ToString(iCurrentRow); } } //done with csv file, delete it File.Delete(csvFileName); //} //check/add the last order if (bValid) { eachOrder = orderValidation.getOrder(sCurrentRow); if (eachOrder != null) { alOrders.Add(eachOrder); } else { bValid = false; } } //return the validated orders arraylist if (bValid) { return(alOrders); } else { return(null); } }