I am writing an application that imports from excel and save in database using asp.net mvc and wcf
Controller
[HttpPost]
public JsonResult UploadExcel(FIRS firs, HttpPostedFileBase FileUpload)
{
List<string> data = new List<string>();
if (FileUpload != null)
{
// tdata.ExecuteCommand("truncate table OtherCompanyAssets");
if (FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
string filename = FileUpload.FileName;
string targetpath = Server.MapPath("~/FileUpload/");
FileUpload.SaveAs(targetpath + filename);
string pathToExcelFile = targetpath + filename;
var connectionString = "";
if (filename.EndsWith(".xls"))
{
connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", pathToExcelFile);
}
else if (filename.EndsWith(".xlsx"))
{
connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", pathToExcelFile);
}
var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
var ds = new DataSet();
adapter.Fill(ds, "ExcelTable");
DataTable dtable = ds.Tables["ExcelTable"];
string sheetName = "Sheet1";
var excelFile = new ExcelQueryFactory(pathToExcelFile);
var firsRecords = from a in excelFile.Worksheet<FIRS>(sheetName) select a;
foreach (var a in firsRecords)
{
try
{
if (a.RC_NUMBER != "" && a.TIN_NUMBER != "" && a.COMPANY_NAME != "" && a.TCC_NUMBER != "")
{
FIRS TU = new FIRS();
var helper = new FirsServiceClient();
TU.RC_NUMBER = a.RC_NUMBER;
TU.TIN_NUMBER = a.TIN_NUMBER;
TU.COMPANY_NAME = a.COMPANY_NAME;
TU.TCC_NUMBER = a.TCC_NUMBER;
//db.FIRS.Add(TU);
//_firsService.AddFirs(TU);
//svc.SaveFirsImportData(TU);
//helper.SaveFirsImportData(TU);
TU.ACTION_STATUS = 1;
TU.CREATED_DATE = DateTime.Now;
_firsService.AddFirs(firs);
}
else
{
data.Add("<ul>");
if (a.RC_NUMBER == "" || a.RC_NUMBER == null) data.Add("<li> RC Number is required</li>");
if (a.TIN_NUMBER == "" || a.TIN_NUMBER == null) data.Add("<li>RC Number is required</li>");
if (a.COMPANY_NAME == "" || a.COMPANY_NAME == null) data.Add("<li>Company Name is required</li>");
if (a.TCC_NUMBER == "" || a.TCC_NUMBER == null) data.Add("<li> TCC Number is required</li>");
data.Add("</ul>");
data.ToArray();
return Json(data, JsonRequestBehavior.AllowGet);
}
}
catch (DbEntityValidationException ex)
{
foreach (var entityValidationErrors in ex.EntityValidationErrors)
{
foreach (var validationError in entityValidationErrors.ValidationErrors)
{
Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
}
}
}
}
//deleting excel file from folder
if ((System.IO.File.Exists(pathToExcelFile)))
{
System.IO.File.Delete(pathToExcelFile);
}
return Json("success", JsonRequestBehavior.AllowGet);
}
else
{
//alert message for invalid file format
data.Add("<ul>");
data.Add("<li>Only Excel file format is allowed</li>");
data.Add("</ul>");
data.ToArray();
return Json(data, JsonRequestBehavior.AllowGet);
}
}
else
{
data.Add("<ul>");
if (FileUpload == null) data.Add("<li>Please choose Excel file</li>");
data.Add("</ul>");
data.ToArray();
return Json(data, JsonRequestBehavior.AllowGet);
}
}
Service.svc
public void AddFirs(FIRS firs)
{
_firsManager.AddFirs(firs);
}
When upload excel is click from view, it calls the controller ** public JsonResult UploadExcel(FIRS firs, HttpPostedFileBase FileUpload)** Then, the controller reference the service AddFirs(FIRS firs) to save data to the database.
Why am I getting this error, and how can I resolve it.