public ActionResult ImportSupplierDetails(HttpPostedFileBase fileUploaded) { try { //Check if there is a file is sent to the controller if (fileUploaded != null && fileUploaded.ContentLength > 0) { // Check if the file ends with csv extension if (fileUploaded.FileName.EndsWith(".csv")) { // Read the file as a stream StreamReader streamCsv = new StreamReader(fileUploaded.InputStream); string csvDataLine = ""; int CurrentLine = 0; string[] LineData = null; // Delete all data everytime a new file is uploaded. purchaseService.ClearSupplierDetailsData(); // Looping to read the File stream and Add Data to the database line by line while ((csvDataLine = streamCsv.ReadLine()) != null) { // Ignore the first line of the file, for column names. if (CurrentLine != 0) { // Add the returned data to an array LineData = csvDataLine.Split(','); //Pass LineData array values var newSupplierDetail = new SupplierDetailsEF() { SupplierDetailsId = int.Parse(LineData[0]), SupplierCode = LineData[1], ItemCode = LineData[2], UnitPrice = double.Parse(LineData[3]), SupplierRank = int.Parse(LineData[4]) }; //Add Data to The Database purchaseService.AddSupplierDetail(newSupplierDetail); } CurrentLine += 1; } Debug.Print("Loaded: " + (CurrentLine - 1) + " items to supplier details table."); } else { TempData["MessageError"] = "File Format is not Supported"; } } else { TempData["MessageError"] = "Please Upload A File"; } // Back to the first view return(RedirectToAction("Index", "ManageSupplier")); } catch (Exception ex) { TempData["MessageError"] = "Error:" + ex.Message; return(RedirectToAction("Index", "ManageSupplier")); } }