Формат строки инициализации не соответствует спецификации, начинающейся с индекса 0. В MVC 5 я получаю эту ошибку, пожалуйста, помогите мне в этом.
public JsonResult UploadExcel(DepartmentVM deptVM, 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("~/Docs/"); FileUpload.SaveAs(targetpath + filename); string pathToExcelFile = targetpath + filename; var connectionString = ""; if (filename.EndsWith(".xls")) { connectionString = string.Format(@"C:\src\RedirectApplication\RedirectApplication\301s.xlsx", pathToExcelFile); // public static string path = @"C:\src\RedirectApplication\RedirectApplication\301s.xlsx"; // public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;"; } else if (filename.EndsWith(".xlsx")) { connectionString = string.Format("Provider= Microsoft.ACE.OLEDB.12.0; Data Source = " + pathToExcelFile + "; Extended Properties = Excel 12.0;HDR=Yes", pathToExcelFile); } var adapter = new OleDbDataAdapter("SELECT * FROM [DepartmentVMs$]", connectionString); // Error on this line var ds = new DataSet(); adapter.Fill(ds, "DepartmentDetails"); DataTable dtable = ds.Tables["DepartmentDetails"]; string sheetName = "DepartmentVMs"; var excelFile = new ExcelQueryFactory(pathToExcelFile); var artistAlbums = from a in excelFile.Worksheet<DepartmentVM>(sheetName) select a; foreach (var a in artistAlbums) { try { if (a.deptCode != "" && a.deptName != "" && a.deptAdditionalInfo!="" && a.deptIsActive!= true) { DepartmentVM dvm = new DepartmentVM(); dvm.deptCode = a.deptCode; dvm.deptName = a.deptName; dvm.deptAdditionalInfo = a.deptAdditionalInfo; dvm.deptIsActive = a.deptIsActive; db.DepartmentVMs.Add(dvm); db.SaveChanges(); } else { data.Add("<ul>"); if (a.deptCode == "" || a.deptCode == null) data.Add("<li> Code is required</li>"); if (a.deptName == "" || a.deptName == null) data.Add("<li> Name is required</li>"); if (a.deptAdditionalInfo == "" || a.deptAdditionalInfo == null) data.Add("<li> Additional Info is required</li>"); if (a.deptIsActive == true || a.deptIsActive == false) data.Add("<li> Is Active status 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); } }
Что я уже пробовал:
[HttpPost] public JsonResult UploadExcel(DepartmentVM deptVM, 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("~/Docs/"); FileUpload.SaveAs(targetpath + filename); string pathToExcelFile = targetpath + filename; var connectionString = ""; if (filename.EndsWith(".xls")) { connectionString = string.Format(@"C:\src\RedirectApplication\RedirectApplication\301s.xlsx", pathToExcelFile); // public static string path = @"C:\src\RedirectApplication\RedirectApplication\301s.xlsx"; // public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;"; } else if (filename.EndsWith(".xlsx")) { connectionString = string.Format("Provider= Microsoft.ACE.OLEDB.12.0; Data Source = " + pathToExcelFile + "; Extended Properties = Excel 12.0;HDR=Yes", pathToExcelFile); } var adapter = new OleDbDataAdapter("SELECT * FROM [DepartmentVMs$]", connectionString); // Error on this line var ds = new DataSet(); adapter.Fill(ds, "DepartmentDetails"); DataTable dtable = ds.Tables["DepartmentDetails"]; string sheetName = "DepartmentVMs"; var excelFile = new ExcelQueryFactory(pathToExcelFile); var artistAlbums = from a in excelFile.Worksheet<DepartmentVM>(sheetName) select a; foreach (var a in artistAlbums) { try { if (a.deptCode != "" && a.deptName != "" && a.deptAdditionalInfo!="" && a.deptIsActive!= true) { DepartmentVM dvm = new DepartmentVM(); dvm.deptCode = a.deptCode; dvm.deptName = a.deptName; dvm.deptAdditionalInfo = a.deptAdditionalInfo; dvm.deptIsActive = a.deptIsActive; db.DepartmentVMs.Add(dvm); db.SaveChanges(); } else { data.Add("<ul>"); if (a.deptCode == "" || a.deptCode == null) data.Add("<li> Code is required</li>"); if (a.deptName == "" || a.deptName == null) data.Add("<li> Name is required</li>"); if (a.deptAdditionalInfo == "" || a.deptAdditionalInfo == null) data.Add("<li> Additional Info is required</li>"); if (a.deptIsActive == true || a.deptIsActive == false) data.Add("<li> Is Active status 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); } }
NotPolitcallyCorrect
Просто выгрузка вашего кода в публикацию не делает его вопросом. Узнайте, как использовать отладчик и указать допустимую строку подключения.
Member 12224368
Спасибо за ваш комментарий, Я новичок в mvc, я проверил через отладчик, я не мог найти, как исправить эту ошибку. поэтому, пожалуйста, дайте мне знать, как дать действительную строку подключения, это очень срочно.
Большое спасибо.
Member 12224368
Спасибо я нашел решение