public ActionResult ImportCsvFileToContactDataTable() { objResponse Response = new objResponse(); session = new SessionHelper(); BAL.Clients.ClientManager objClinetManager = new BAL.Clients.ClientManager(); DataTable dt = new DataTable(); // string Result=""; try { string fname; if (Request.Files.Count > 0) { HttpFileCollectionBase files = Request.Files; for (int i = 0; i < files.Count; i++) { HttpPostedFileBase file = files[i]; // Checking for Internet Explorer if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER") { string[] testfiles = file.FileName.Split(new char[] { '\\' }); fname = testfiles[testfiles.Length - 1]; } else { fname = file.FileName; } string filePath = Server.MapPath(file.FileName); string newFileName = "IMP_Csv_" + session.UserSession.UserId + "_" + fname; string newFilePath = Server.MapPath(ConfigurationManager.AppSettings["Import_Csv_Dir"]) + newFileName; file.SaveAs(newFilePath); dt = FileImporter.CsvPackageExtension.GetDataTableFromCSVFile(newFilePath); int count = 0; foreach (DataRow dr in dt.Rows) { Project.Entity.Clients objClient = new Entity.Clients(); objClient.Name = dr[0].ToString(); objClient.CompanyName = dr[1].ToString(); objClient.Email = dr[2].ToString(); objClient.Alternate_Email = dr[3].ToString(); objClient.ContactNo = dr[4].ToString(); objClient.SkypeNo = dr[5].ToString(); objClient.AddressLine1 = dr[6].ToString(); objClient.AddressLine2 = dr[7].ToString(); objClient.City = dr[8].ToString(); objClient.State = dr[9].ToString(); objClient.Country = dr[10].ToString(); objClient.ZipCode = dr[11].ToString(); objClient.Source = dr[12].ToString(); objClient.Model = dr[13].ToString();; Response = objClinetManager.ImportClient(objClient, session.UserSession.Username, Convert.ToInt64(session.UserSession.PIN)); if (Response.ErrorCode == 0) { if (Response.ErrorMessage != "Client with same Email Already Exists") { count++; } } else { return(Json("fail", JsonRequestBehavior.AllowGet)); } } if (System.IO.File.Exists(newFilePath)) { System.IO.File.Delete(newFilePath); } return(Json("Success," + count.ToString(), JsonRequestBehavior.AllowGet)); } return(Json("fail", JsonRequestBehavior.AllowGet)); } else { return(Json("fail", JsonRequestBehavior.AllowGet)); } } catch (Exception ex) { BAL.Common.LogManager.LogError("ImportCsvFileToContactDataTable", 1, Convert.ToString(ex.Source), Convert.ToString(ex.Message), Convert.ToString(ex.StackTrace)); return(Json("fail", JsonRequestBehavior.AllowGet)); } }
public ActionResult ImportExcelFileToContactDataTable() { objResponse Response = new objResponse(); session = new SessionHelper(); BAL.Clients.ClientManager objClientManager = new BAL.Clients.ClientManager(); DataTable dt = new DataTable(); // string Result=""; try { string fname; if (Request.Files.Count > 0) { HttpFileCollectionBase files = Request.Files; for (int i = 0; i < files.Count; i++) { HttpPostedFileBase file = files[i]; // Checking for Internet Explorer if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER") { string[] testfiles = file.FileName.Split(new char[] { '\\' }); fname = testfiles[testfiles.Length - 1]; } else { fname = file.FileName; } if ((file != null) && (file.ContentLength != 0) && !string.IsNullOrEmpty(file.FileName)) { string fileName = file.FileName; string fileContentType = file.ContentType; byte[] fileBytes = new byte[file.ContentLength]; var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength)); } var excel = new ExcelPackage(file.InputStream); dt = ExcelPackageExtensions.ToDataTable(excel); int count = 0; foreach (DataRow dr in dt.Rows) { Project.Entity.Clients objClient = new Entity.Clients(); objClient.Name = dr[0].ToString(); objClient.CompanyName = dr[1].ToString(); objClient.Email = dr[2].ToString(); objClient.Alternate_Email = dr[3].ToString(); objClient.ContactNo = dr[4].ToString(); objClient.SkypeNo = dr[5].ToString(); objClient.AddressLine1 = dr[6].ToString(); objClient.AddressLine2 = dr[7].ToString(); objClient.City = dr[8].ToString(); objClient.State = dr[9].ToString(); objClient.Country = dr[10].ToString(); objClient.ZipCode = dr[11].ToString(); objClient.Source = dr[12].ToString(); objClient.Model = dr[13].ToString();; Response = objClientManager.ImportClient(objClient, session.UserSession.Username, Convert.ToInt64(session.UserSession.PIN)); if (Response.ErrorCode == 0) { if (Response.ErrorMessage != "Client with same Email Already Exists") { count++; } } else { return(Json("fail", JsonRequestBehavior.AllowGet)); } } return(Json("Success," + count.ToString(), JsonRequestBehavior.AllowGet)); } return(Json("fail", JsonRequestBehavior.AllowGet)); } else { return(Json("fail", JsonRequestBehavior.AllowGet)); } } catch (Exception ex) { BAL.Common.LogManager.LogError("ImportExcelFileToContactDataTable", 1, Convert.ToString(ex.Source), Convert.ToString(ex.Message), Convert.ToString(ex.StackTrace)); return(Json("fail", JsonRequestBehavior.AllowGet)); } }