using System; using System.Collections.Generic; using System.Linq; using System.Text; using Ease.CoreV35.Model; using Payroll.BO; using System.Data; using System.Data.OleDb; using System.IO; using Ease.Excel; using Ease.Excel.Reader; using Ease.CoreV35.DataAccess; using Payroll.BO; using Payroll.Service.Attendence; using System.Data.SqlClient; using System.Configuration; namespace Payroll.Service { [Serializable] public class RegularDataUploadService : ServiceTemplate, IRegularDataUploadService { public RegularDataUploadService() { } #region Service Implementation public DataTable LoadData(string fileName) { return this.LoadExcelData(fileName); #region Old Code //DataTable dt = new DataTable("ImportedData"); //try //{ // using (FileStream fs = new FileStream(fileName, FileMode.Open)) // { // XlsReader reader = new XlsReader(fs, true); // dt = reader.Data.Tables[0].Copy(); // fs.Close(); // fs.Dispose(); // reader = null; // } //} //catch (Exception e) //{ // throw new ServiceException(e.Message, e); //} //return dt; #endregion } public DataTable LoadData(string fileName, string sheetName) { return this.LoadExcelData(fileName, sheetName); #region Old Code //DataTable dt = new DataTable(sheetName); //try //{ // using (FileStream fs = new FileStream(fileName, FileMode.Open)) // { // XlsReader reader = new XlsReader(fs, true); // dt = reader.Data.Tables[0].Copy(); // fs.Close(); // fs.Dispose(); // reader = null; // } //} //catch (Exception e) //{ // throw new ServiceException(e.Message, e); //} //return dt; #endregion } public DataTable LoadExcelData(string fileName, string sheetName) { string connectionString = string.Empty; if (fileName.EndsWith("xls")) //Connection string for office 2003 connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;", fileName); else //Connection string for office 2007 connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0 Xml;", fileName); DataTable dt = new DataTable(sheetName); try { OleDbConnection connection = new OleDbConnection(connectionString); connection.Open(); DataTable excelTable = connection.GetSchema("Tables"); string tableName; if (excelTable.Rows.Count > 0) { tableName = Convert.ToString(excelTable.Rows[0]["TABLE_NAME"]); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}]", sheetName + "$"), connection); //dt.TableName = sheetName; dataAdapter.Fill(dt); dataAdapter = null; } connection.Close(); connection = null; } catch (Exception ex) { throw new Exception(ex.Message); } return dt; } public DataTable LoadExcelData(string fileName) { string connectionString = string.Empty; if(fileName.EndsWith("xls")) //Connection string for office 2003 connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;", fileName); else //Connection string for office 2007 connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0 Xml;", fileName); DataTable dt = new DataTable(); try { OleDbConnection connection = new OleDbConnection(connectionString); connection.Open(); DataTable excelTable = connection.GetSchema("Tables"); string tableName; if (excelTable.Rows.Count > 0) { tableName = Convert.ToString(excelTable.Rows[0]["TABLE_NAME"]); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(string.Format("SELECT DISTINCT * FROM [{0}]", tableName), connection); dt.TableName = tableName.Substring(0, tableName.Length - 1); dataAdapter.Fill(dt); dataAdapter = null; } connection.Close(); connection = null; } catch (Exception ex) { throw new Exception(ex.Message); } return dt; } public void Save(ObjectsTemplate categorys, ObjectsTemplate grades, ObjectsTemplate departments, ObjectsTemplate locations, ObjectsTemplate religions, ObjectsTemplate designations, ObjectsTemplate banks, ObjectsTemplate branches, ObjectsTemplate employees, ObjectsTemplate oaccessCards ) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); CategoryService.SaveForUpload(tc, categorys); GradeService.SaveForUpload(tc, grades); DepartmentService.SaveForUpload(tc, departments); LocationService.SaveForUpload(tc, locations); ReligionService.SaveForUpload(tc, religions); DesignationService.SaveForUpload(tc, designations); BankService.SaveForUpload(tc, banks); BranchService.SaveForUpload(tc, branches); EmployeeService.SaveForUpload(tc, employees); AccessCardService.SaveForExcelUpload(tc, oaccessCards); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void UpdateLocation( ObjectsTemplate employees) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); ObjectsTemplate olocations = new ObjectsTemplate(); int locID =50; Location op = Location.Get(ID.FromInteger(5)); olocations.Add(op); foreach (Employee emp in employees) { //oemployee = oemployees.Find(delegate(Employee emp) { return emp.EmployeeNo == dr["Employee No"].ToString(); }); Location onewloc= olocations.Find(delegate (Location item) {return item.Name == emp.Location.Name ;}); if (onewloc == null) { locID = locID + 1; emp.Location.SetObjectID(locID); olocations.Add(emp.Location); } else { emp.Location.SetObjectID(onewloc.ID.Integer); } } LocationService.SaveForUpload(tc, olocations); //foreach (Employee emp in employees) //{ // MiscellaneousDA.updateLocation(tc, emp.ID, emp.Location.ID); //} tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public DataSet LoadViewData() { SqlConnection MyConn = null; SqlCommand com = null; string mySQL = ""; SqlDataAdapter da = null; DataTable dt = null; string sConnectionString = ConfigurationSettings.AppSettings["ViewConnection"]; //string sViews = @"vw_General,vw_SpouseWorkingBrac,vw_Academic,vw_Experience,vw_Training,vw_PublicationAward,vw_PromotionTransferHistory,vw_Punishment,vw_Organogram,vw_OrganogramPosting,vw_ChangeNodePosition"; string sViews = ConfigurationSettings.AppSettings["Views"]; DataSet ds = new DataSet(); if (sViews != "") { string[] values = sViews.Split(','); foreach (string ss in values) { if (ss == "vw_Organogram") mySQL = "SELECT * from " + ss + " Order by Parent_Position_No,Position_No"; else mySQL = "SELECT * from " + ss; MyConn = new SqlConnection(sConnectionString); com = new SqlCommand(mySQL, MyConn); da = new SqlDataAdapter(com); dt = new DataTable(ss); try { da.Fill(dt); ds.Tables.Add(dt); } catch (Exception exp) { throw new Exception("Could not read views. " + exp.Message); } } } return ds; } #endregion protected override T CreateObject(Ease.CoreV35.DataAccess.DataReader dr) { throw new NotImplementedException(); } } }