title: ASP.NET MVC - How to Treat Duplicate in Excel Upload
body: <p>I am uploading Excel file and save into my database using ASP.NET MVC. I succeeded in:</p>
  
  <ol>
  <li><p>Importing the Excel file</p></li>
  <li><p>Save in to database</p></li>
  </ol>
  
  <p>See my Controller below</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>        public ActionResult ImportCountriesExcel(HttpPostedFileBase FileUpload)
      {
          string data = "";
          var notif = new UINotificationViewModel()
          {
              notif_message = "Record Saved successfully",
              notif_type = NotificationType.SUCCESS,
          };
          var bodsList = new List&lt;COUNTRIES&gt;();
          if (FileUpload != null)
          {
              // tdata.ExecuteCommand("truncate table OtherCompanyAssets");  
              HttpPostedFileBase file = Request.Files["FileUpload"];
              if (true)//FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
              {
                  try { 
                  string message = "";
                  using (var package = new ExcelPackage(FileUpload.InputStream))
                  {
                      var currentSheet = package.Workbook.Worksheets;
                      // if(currentSheet.Count)
                      var workSheet = currentSheet.First();
                      var noOfCol = workSheet.Dimension.End.Column;
                      var noOfRow = workSheet.Dimension.End.Row;
                      for (int rowIterator = 2; rowIterator &lt;= noOfRow; rowIterator++)
                      {
                              var bod = new COUNTRIES();
                              bod.COUNTRY_CODE = Convert.ToString(workSheet.Cells[rowIterator, 1].Value);
                              if (bod.COUNTRY_CODE == "")
                                  message += "&lt;li&gt; Country Code is empty at row " + rowIterator + "&lt;/li&gt;";
                              bod.COUNTRY_NAME = Convert.ToString(workSheet.Cells[rowIterator, 2].Value);
                              if (bod.COUNTRY_NAME == "")
                                  message += "&lt;li&gt; Country Name is empty at row " + rowIterator + "&lt;/li&gt;";
                              if (bod.COUNTRY_DESC == "")
                                  message += "&lt;li&gt; Country Desc is empty at row " + rowIterator + "&lt;/li&gt;";
                              bodsList.Add(bod);
                      }
                  }
                  foreach (var item in bodsList)
                  {
                      _countriesService.AddCountry(item);
                  }
                  return RedirectToAction("Index");
              }
                  catch (Exception e)
                  {
                      notif.notif_message = "Application error, Contact the Administrator";
                      notif.notif_type = NotificationType.ERROR;
                  }
                  TempData["notif"] = notif;
                  return RedirectToAction("Index");
              }
              else
              {
                  notif.notif_type = NotificationType.ERROR;
                  //alert message for invalid file format  
                  data += "&lt;ul&gt;";
                  data += "&lt;li&gt;Only Excel file format is allowed&lt;/li&gt;";
                  data += "&lt;/ul&gt;";
                  notif.notif_message = data;
                  TempData["notif"] = notif;
                  return RedirectToAction("Index");
                  //return Json(data, JsonRequestBehavior.AllowGet);
              }
          }
          else
          {
              data += "&lt;ul&gt;";
              if (FileUpload == null) data += "&lt;li&gt;Please choose Excel file&lt;/li&gt;";
              data += "&lt;/ul&gt;";
              notif.notif_message = data;
              TempData["notif"] = notif;
              return RedirectToAction("Index");
              //return Json(data, JsonRequestBehavior.AllowGet);
          }
      }
  </code></pre>
  
  <p>The issue I have is this</p>
  
  <ol>
  <li>Some of the import have white spaces</li>
  <li>It allows duplicate</li>
  </ol>
  
  <p><strong>Please how do I:</strong></p>
  
  <ol>
  <li>Remove white spaces using Trim</li>
  <li>Whenever there is duplicate (If the record already exist), it should be filtered based on COUNTRY_CODE and COUNTRY_NAME. Then the system should should the list of records that already exist. It will ask the user the ones he wants to update or allow to duplicate. </li>
  </ol>