private void ImportButton_Click(object sender, RoutedEventArgs e) { var screen = new OpenFileDialog(); screen.Filter = "Excel files|*.xls;*.xlsx"; if (screen.ShowDialog() == true) { var workbook = new Workbook(screen.FileName); var sheet = workbook.Worksheets[0]; var col = "A"; var row = 2; var cell = sheet.Cells[$"{col}{row}"]; while (cell.Value != null) { var categoryid = sheet.Cells[$"C{row}"].StringValue; var name = sheet.Cells[$"B{row}"].StringValue; bool has = MainWindow.db.Categories.ToList().Any(cus => cus.ID == categoryid); if (has == true) { var price = sheet.Cells[$"D{row}"].DoubleValue; var weight = sheet.Cells[$"E{row}"].DoubleValue; var imageName = sheet.Cells[$"F{row}"].StringValue; var imageSourceInfo = new FileInfo(screen.FileName); var imageSourceFullPath = $"{imageSourceInfo.DirectoryName}\\images\\{imageName}"; var imageSourceFileInfo = new FileInfo(imageSourceFullPath); var uniqueName = $"{Guid.NewGuid()}.{imageSourceFileInfo.Extension}"; var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; var destinationPath = $"{baseDirectory}image\\{uniqueName}"; File.Copy(imageSourceFullPath, destinationPath); var newProduct = new Product() { ID = IDGenerator.createID("P"), Name = name, //CategoryID = (from category in MainWindow.db.Categories where category.Name == categoryid select category.ID).Single(), CategoryID = categoryid, Price = price, Weight = weight, isDeleted = false, Picture = uniqueName, }; MainWindow.db.Products.Add(newProduct); MainWindow.db.SaveChanges(); row++; cell = sheet.Cells[$"{col}{row}"]; } else { string message = "The category with ID \"" + categoryid + "\" in product \"" + name + "\" does not exist.\nDo you want to add a new category? You can skip importing this product by clicking \"No\"."; if (MessageBox.Show(message, "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes) { var newCategory = new AddCategoryWindow(); if (newCategory.ShowDialog() == true) { if (newCategory.CT_Name != null) { var category = new Category() { ID = categoryid, Name = newCategory.CT_Name, isDeleted = false, }; MainWindow.db.Categories.Add(category); MainWindow.db.SaveChanges(); CategoryControl.category_notification.CategoryChange = true; var price = sheet.Cells[$"D{row}"].DoubleValue; var weight = sheet.Cells[$"E{row}"].DoubleValue; var imageName = sheet.Cells[$"F{row}"].StringValue; var imageSourceInfo = new FileInfo(screen.FileName); var imageSourceFullPath = $"{imageSourceInfo.DirectoryName}\\images\\{imageName}"; var imageSourceFileInfo = new FileInfo(imageSourceFullPath); var uniqueName = $"{Guid.NewGuid()}.{imageSourceFileInfo.Extension}"; var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; var destinationPath = $"{baseDirectory}image\\{uniqueName}"; File.Copy(imageSourceFullPath, destinationPath); var newProduct = new Product() { ID = IDGenerator.createID("P"), Name = name, CategoryID = categoryid, Price = price, Weight = weight, isDeleted = false, Picture = uniqueName, }; MainWindow.db.Products.Add(newProduct); MainWindow.db.SaveChanges(); } } row++; cell = sheet.Cells[$"{col}{row}"]; } else { row++; cell = sheet.Cells[$"{col}{row}"]; } } } MessageBox.Show("Import completed!", "Message"); productDataGrid.ItemsSource = MainWindow.db.Products.ToList(); } }