public static ReportViewModel ImportExcel(Stream file, string user) { using (CFMMCDEntities db = new CFMMCDEntities()) { ReportViewModel error = new ReportViewModel(); XLWorkbook workBook; try { workBook = new XLWorkbook(file); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); error.Result = false; error.Message = "File format not supported"; error.ErrorLevel = 3; return(error); } IXLWorksheet workSheet = workBook.Worksheet(1); var MIMRowList = new List <CSHMIMP0>(); bool IsFirstRow = true; int succesfulRows = 0; int blankCounter = 0; IXLRow FirstRow = workSheet.Rows().ElementAt(0); if (FirstRow == null || FirstRow.CellCount() <= 0) { error.Result = false; error.ErrorLevel = 3; error.Message = "File has incorrect or unsupported format"; return(error); } int index = 1; foreach (IXLRow row in workSheet.Rows()) { if (row == null) { break; } if (IsFirstRow) { FirstRow = row; IsFirstRow = false; } else { if (row.Cells() == null || row.CellCount() <= 0) { break; } INVVEMP0 VRow = new INVVEMP0(); int errorLevel = 0; for (int i = 1; i < row.CellCount(); i++) { System.Diagnostics.Debug.WriteLine("Cell count: " + i); System.Diagnostics.Debug.WriteLine("Cell header: " + FirstRow.Cell(i).Value.ToString().ToUpper()); System.Diagnostics.Debug.WriteLine("Cell data: " + row.Cell(i).Value.ToString()); System.Diagnostics.Debug.WriteLine("Row: " + index); if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMVEN") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("VENDOR NUMBER") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("VENDOR #")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMVEN = int.Parse(row.Cell(i).Value.ToString()); } else { error.Message += "Vendor number [" + row.Cell(i).Value.ToString() + "] at {Row " + index + "} not in the correct format. | "; if (error.ErrorLevel != 3) { errorLevel = 2; } error.Result = false; break; } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMDS1") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("DESCRIPTION 1")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMDS1 = row.Cell(i).Value.ToString(); } else { error.Result = false; error.Message += "Description 1 [" + row.Cell(i).Value.ToString() + "] at {Row " + index + "} not in the correct format. | "; if (error.ErrorLevel != 3) { errorLevel = 2; } } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMDS2") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("DESCRIPTION 2")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMDS2 = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMWSI") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("INTERNATIONAL WSI-NUMBER")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMWSI = int.Parse(row.Cell(i).Value.ToString()); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMCCD") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("COUNTRY CODE")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMCCD = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMZIP") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("ZIP CODE")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMZIP = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMCTY") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("CITY")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMCTY = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMSTR") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("STREET")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMSTR = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMTEL") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("TELEPHONE NUMBER")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMTEL = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMSTN") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("SHORT TELEPHONE NUMBER")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMSTN = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMLOC") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("LOCAL VENDOR")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMLOC = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMDAY") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("MINIMUM STOCK BUFFER")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMDAY = double.Parse(row.Cell(i).Value.ToString()); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMTID") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("TELEPHONE ID")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMTID = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMSTA") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("STATUS")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMSTA = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMDAT") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("CREATION/CHANGE DATE")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMDAT = DateTime.Parse(row.Cell(i).Value.ToString()); } else { VRow.VEMDAT = DateTime.Now; } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMUSR") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("USER")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMUSR = row.Cell(i).Value.ToString(); } else { VRow.VEMUSR = user.ToUpper().Substring(0, 3); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMADE") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("AUTO DELIVERY")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMADE = row.Cell(i).Value.ToString(); } ; } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("VEMDEL") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("SUPPLY CHAIN VENDOR")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.VEMDEL = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("REGION")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.Region = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("PROVINCE")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.Province = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("CITY")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.City = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("STORE")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { VRow.Store = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value == null || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("")) { blankCounter++; if (blankCounter > 20) { break; } else { continue; } } } if (VRow.VEMVEN == 0 || VRow.VEMDS1 == null) { error.Result = false; error.Message += "{Row " + index + "} has incorrect format | "; errorLevel = 2; continue; } if (db.INVVEMP0.Where(o => o.VEMVEN == VRow.VEMVEN).Any()) { error.Result = false; error.Message += "Vendor [" + VRow.VEMVEN + "] at {Row " + index + "} is already defined | "; errorLevel = 2; } if (VRow.VEMLOC != null && !VRow.VEMLOC.Equals("") && !db.LOCATIONs.Where(o => o.Id.ToString().Equals(VRow.VEMLOC)).Any()) { error.Result = false; error.Message += "Location with Id [" + VRow.VEMLOC + "] at {Row " + index + "} not available | "; errorLevel = 2; } if (VRow.Store != null && !VRow.Store.Equals("") && !db.Store_Profile.Where(o => o.STORE_NO.ToString().Equals(VRow.Store)).Any()) { error.Result = false; error.Message += "Store with Id [" + VRow.VEMLOC + "] at {Row " + index + "} not available | "; errorLevel = 2; } if (errorLevel >= 2) { error.Message += "{Row " + index + "} not inserted | "; } if (errorLevel < 2) { db.INVVEMP0.Add(VRow); try { db.SaveChanges(); // Special case for logging import succesfulRows++; new AuditLogManager().Audit(user, DateTime.Now, "Vendor Master", "Import", VRow.VEMVEN.ToString(), VRow.VEMDS1); System.Diagnostics.Debug.WriteLine(VRow.VEMVEN); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Source); System.Diagnostics.Debug.WriteLine(e.Message); System.Diagnostics.Debug.WriteLine(e.StackTrace); System.Diagnostics.Debug.WriteLine(e.InnerException); Exception f = e.InnerException; while (f != null) { System.Diagnostics.Debug.WriteLine("INNER:"); System.Diagnostics.Debug.WriteLine(f.Message); System.Diagnostics.Debug.WriteLine(f.Source); f = f.InnerException; } System.Diagnostics.Debug.WriteLine(e.Data); error.Result = false; error.Message += "{Row " + index + "} failed to insert. | \n"; errorLevel = 2; } } error.ErrorLevel = errorLevel; index++; } } if (succesfulRows <= 0) { error.Result = false; error.Message += "No rows imported | "; error.ErrorLevel = 3; } else if (succesfulRows >= index) { error.ErrorLevel = 0; error.Result = true; } error.Message += "Imported " + index + " rows. "; return(error); } }
public static ReportViewModel ImportExcel(Stream file, string user) { using (CFMMCDEntities db = new CFMMCDEntities()) { ReportViewModel error = new ReportViewModel(); XLWorkbook workBook; try { workBook = new XLWorkbook(file); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); error.Result = false; error.Message = "File format not supported"; error.ErrorLevel = 3; return(error); } IXLWorksheet workSheet = workBook.Worksheet(1); var MIMRowList = new List <CSHMIMP0>(); bool IsFirstRow = true; int succesfulRows = 0; int blankCounter = 0; IXLRow FirstRow = workSheet.Rows().ElementAt(0); if (FirstRow == null || FirstRow.CellCount() <= 0) { error.Result = false; error.ErrorLevel = 3; error.Message = "File has incorrect or unsupported format"; return(error); } int index = 1; foreach (IXLRow row in workSheet.Rows()) { if (row == null) { break; } if (IsFirstRow) { FirstRow = row; IsFirstRow = false; } else { if (row.Cells() == null || row.CellCount() <= 0) { break; } Store_Profile SPRow = new Store_Profile(); int errorLevel = 0; for (int i = 1; i < row.CellCount(); i++) { System.Diagnostics.Debug.WriteLine("Cell count: " + i); System.Diagnostics.Debug.WriteLine("Cell header: " + FirstRow.Cell(i).Value.ToString().ToUpper()); System.Diagnostics.Debug.WriteLine("Cell data: " + row.Cell(i).Value.ToString()); System.Diagnostics.Debug.WriteLine("Row: " + index); if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("STORE_NO") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("STORE NUMBER") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("STORE #")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.STORE_NO = int.Parse(row.Cell(i).Value.ToString()); } else { error.Message += "Store number [" + row.Cell(i).Value.ToString() + "] at {Row " + index + "} not in the correct format. | "; if (error.ErrorLevel != 3) { errorLevel = 2; } error.Result = false; break; } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("STORE_NAME") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("STORE NAME")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.STORE_NAME = row.Cell(i).Value.ToString(); } else { error.Result = false; error.Message += "Store name [" + row.Cell(i).Value.ToString() + "] at {Row " + index + "} not in the correct format. | "; if (error.ErrorLevel != 3) { errorLevel = 2; } } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("OWNERSHIP") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("OWNERSHIP")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.OWNERSHIP = int.Parse(row.Cell(i).Value.ToString()); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("PROFIT_CENTER") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("PROFIT CENTER")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.PROFIT_CENTER = int.Parse(row.Cell(i).Value.ToString()); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("BREAKFAST_PRICE_TIER") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("BREAKFAST PRICE TIER")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.BREAKFAST_PRICE_TIER = int.Parse(row.Cell(i).Value.ToString()); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("REGULAR_PRICE_TIER") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("REGULAR PRICE TIER")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.REGULAR_PRICE_TIER = int.Parse(row.Cell(i).Value.ToString()); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("DC_PRICE_TIER") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("DC PRICE TIER")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.DC_PRICE_TIER = int.Parse(row.Cell(i).Value.ToString()); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("MDS_PRICE_TIER") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("MDS PRICE TIER")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.MDS_PRICE_TIER = int.Parse(row.Cell(i).Value.ToString()); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("MCCAFE_LEVEL2_PRICE_TIER") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("MCCAFE LEVEL 2 PRICE TIER")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.MCCAFE_LEVEL_2_PRICE_TIER = int.Parse(row.Cell(i).Value.ToString()); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("MCCAFE_LEVEL3_PRICE_TIER") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("MCCAFE LEVEL 3 PRICE TIER")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.MCCAFE_LEVEL_3_PRICE_TIER = int.Parse(row.Cell(i).Value.ToString()); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("MCCAFE_BISTRO_PRICE_TIER") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("MCCAFE BISTRO PRICE TIER")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.MCCAFE_BISTRO_PRICE_TIER = int.Parse(row.Cell(i).Value.ToString()); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("PROJECT_GOLD_PRICE_TIER") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("PROJECT GOLD PRICE TIER")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.PROJECT_GOLD_PRICE_TIER = int.Parse(row.Cell(i).Value.ToString()); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("BET") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("BUSINESS EXTENSION")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.BET = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("REGION") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("REGION")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.REGION = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("PROVINCE") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("PROVINCE")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.PROVINCE = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("LOCATION") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("LOCATION")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.LOCATION = int.Parse(row.Cell(i).Value.ToString()); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("ADDRESS") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("ADDRESS")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.ADDRESS = row.Cell(i).Value.ToString(); } ; } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("CITY") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("CITY")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.CITY = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("FRESH_OR_FROZEN") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("FRESH OR FROZEN")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.FRESH_OR_FROZEN = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("PAPER_OR_PLASTIC") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("PAPER OR PLASTIC")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.PAPER_OR_PLASTIC = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("SOFT_SERVE_OR_VANILLA_POWDER_MIX") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("SOFT SERVE OR VANILLA POWDER MIX")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.SOFT_SERVE_OR_VANILLA_POWDER_MIX = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("SIMPLOT_OR_MCCAIN") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("SIMPLOT OR MCCAIN")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.SIMPLOT_OR_MCCAIN = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("MCCORMICK_OR_GSF") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("MCCORMICK OR GSF")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.MCCORMICK_OR_GSF = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value.ToString().ToUpper().Equals("FRESHB_OR_FROZENB") || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("FRESH BUNS OR FROZEN BUNS")) { if (row.Cell(i).Value != null && !row.Cell(i).Value.ToString().Equals("")) { SPRow.SOFT_SERVE_OR_VANILLA_POWDER_MIX = row.Cell(i).Value.ToString(); } } else if (FirstRow.Cell(i).Value == null || FirstRow.Cell(i).Value.ToString().ToUpper().Contains("")) { blankCounter++; if (blankCounter > 20) { break; } else { continue; } } } if (SPRow.STORE_NO == 0 || SPRow.STORE_NAME == null) { error.Result = false; error.Message += "{Row " + index + "} has incorrect format | "; errorLevel = 2; break; } if (db.Store_Profile.Where(o => o.STORE_NO == SPRow.STORE_NO).Any()) { error.Result = false; error.Message += "Store profile [" + SPRow.STORE_NO + "] at {Row " + index + "} is already defined | "; errorLevel = 2; } if (SPRow.OWNERSHIP != null && !SPRow.OWNERSHIP.Equals("") && !db.OWNERSHIPs.Where(o => o.Id == SPRow.OWNERSHIP).Any()) { error.Result = false; error.Message += "Ownership with Id [" + SPRow.OWNERSHIP + "] at {Row " + index + "} not available | "; errorLevel = 2; } if (SPRow.PROFIT_CENTER != null && !SPRow.PROFIT_CENTER.Equals("") && !db.PROFIT_CEN.Where(o => o.Id == SPRow.PROFIT_CENTER).Any()) { error.Result = false; error.Message += "Ownership with Id [" + SPRow.PROFIT_CENTER + "] at {Row " + index + "} not available | "; errorLevel = 2; } if (SPRow.LOCATION != null && !SPRow.PROFIT_CENTER.Equals("") && !db.LOCATIONs.Where(o => o.Id == SPRow.LOCATION).Any()) { error.Result = false; error.Message += "Location with Id [" + SPRow.LOCATION + "] at {Row " + index + "} not available | "; errorLevel = 2; } if (errorLevel >= 2) { error.Message += "{Row " + index + "} not inserted | "; } if (errorLevel < 2) { db.Store_Profile.Add(SPRow); try { db.SaveChanges(); // Special case for logging import succesfulRows++; new AuditLogManager().Audit(user, DateTime.Now, "Store profile", "Import", SPRow.STORE_NO.ToString(), SPRow.STORE_NAME); System.Diagnostics.Debug.WriteLine(SPRow.STORE_NO); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Source); System.Diagnostics.Debug.WriteLine(e.Message); System.Diagnostics.Debug.WriteLine(e.StackTrace); System.Diagnostics.Debug.WriteLine(e.InnerException); Exception f = e.InnerException; while (f != null) { System.Diagnostics.Debug.WriteLine("INNER:"); System.Diagnostics.Debug.WriteLine(f.Message); System.Diagnostics.Debug.WriteLine(f.Source); f = f.InnerException; } System.Diagnostics.Debug.WriteLine(e.Data); error.Result = false; error.Message += "{Row " + index + "} failed to insert. | \n"; errorLevel = 2; } } error.ErrorLevel = errorLevel; index++; } } if (succesfulRows <= 0) { error.Result = false; error.Message += "No rows imported | "; error.ErrorLevel = 3; } else if (succesfulRows >= index) { error.ErrorLevel = 0; error.Result = true; } error.Message += "Imported " + index + " rows. "; return(error); } }