private async void ImportFileIntoDatabase(object fileName) { FileStream fs = null; StreamReader sr = null; try { fs = File.Open((string)fileName, FileMode.Open); sr = new StreamReader(fs); string contents = sr.ReadToEnd(); string[] contentLines = contents.Split(new char[] { '\n' }); MedicineContext context = new MedicineContext(); if (ValidateFile(contentLines[0])) { for (int i = 1; i < contentLines.Length; i++) { string[] medicineInfos = contentLines[i].Split(','); Medicine obj = new Entities.Medicine { Id = medicineInfos[0], Name = medicineInfos[1], ManufacturingDate = DateTime.Parse(medicineInfos[2]), ExpirationDate = DateTime.Parse(medicineInfos[3]) }; MedicineList.Add(obj); await context.Medicines.InsertOneAsync(obj); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { fs.Close(); sr.Close(); } }