i put a break point at the beginning of import method and the code always enter the first condition even if i import an excel file and this cause the exception, the exception say that An exception of type 'System.NullReferenceException' occurred in TestImportFromExcel.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
public ActionResult Import(HttpPostedFileBase excelFile)
{
if (excelFile.ContentLength == 0 || excelFile == null)
{
ViewBag.Error = "select excel file <br/>";
return View("Index");
}
else
{
//if file is not null
if (excelFile.FileName.EndsWith("xls") ||
excelFile.FileName.EndsWith("xlsx"))
{
string path = Server.MapPath("~/Content" +
excelFile.FileName);
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
excelFile.SaveAs(path);
Excel.Application application = new Excel.Application();
Excel.Workbook workBook =
application.Workbooks.Open(path);
Excel.Worksheet worksheet = workBook.ActiveSheet;
Excel.Range range = worksheet.UsedRange;
List<ItemDetails> itemDetails = new List<ItemDetails>();
for (int x = 1; x < range.Rows.Count; x++)
{
ItemDetails i = new ItemDetails();
i.Id = ((Excel.Range)range.Cells[x, 1]).Text;
i.Factory = ((Excel.Range)range.Cells[x, 2]).Text;
i.ItemCode = ((Excel.Range)range.Cells[x, 3]).Text;
i.Description = ((Excel.Range)range.Cells[x,4]).Text;
i.UnitMeasure = ((Excel.Range)range.Cells[x,5]).Text;
i.Weight = ((Excel.Range)range.Cells[x, 6]).Text;
itemDetails.Add(i);
}
ViewBag.itemDetails = itemDetails;
return View("Success");
}
else
{
ViewBag.Error = "the file type is not correct<br/>";
return View("Index");
}
}
}