using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using Ease.CoreV35.Model; namespace Payroll.BO { [Serializable] public class DataUploadValidation { #region Properties public DataUploadValidation() { _errorOrSuccList = new List(); } #region Error List For New Regular Data Upload private List _errorOrSuccList; public List ErrorOrSuccessList { get { return _errorOrSuccList; } set { _errorOrSuccList = value; } } #endregion #endregion #region Functions #region Validate Input public void ValidateInput(DataUploadColumnDefinition columnDefinitions, DataTable dTable) { //this.uploadStatus = uploadStatus; int index = -1; // ExcelColumnNameValidation(columnDefinitions, dTable); if (columnDefinitions.ColumnName.Count != dTable.Columns.Count) throw new Exception("No of excel column is not like the excel column definition"); else { foreach (DataRow row in dTable.Rows) { index++; //uploadStatus.Text = string.Format("Validating Fields... {0}/{1}", index, dTable.Rows.Count); //uploadStatus.Refresh(); object[] obj = row.ItemArray; for (int i = 0; i < obj.Length; i++) { if (columnDefinitions.IsAllowNull(dTable.Columns[i].ColumnName)==false) { if (!ExcelNullValueValidation(obj[i])) { this.GenerateErrorList(dTable.Columns[i].ColumnName, index, " Value can't be empty/blank."); continue; } } if (ExcelNullValueValidation(obj[i])==true ) { if (!ExcelDataTypeValidation(dTable.Columns[i].ColumnName, obj[i].ToString(),columnDefinitions)) this.GenerateErrorList(dTable.Columns[i].ColumnName, index, "'" + obj[i].ToString() + "' is not valid " + columnDefinitions.GetColumnDataType(dTable.Columns[i].ColumnName)); } } } } } #endregion #region Generate Error List private void GenerateErrorList(string columnName, int counter, string errMsg) { UploadErrorOrSuccess oItem = new UploadErrorOrSuccess(); oItem.RowNo = counter + 1; oItem.ColumName = columnName; oItem.Message ="Row:" + oItem.RowNo.ToString() + " Column:" + columnName + " " + errMsg; _errorOrSuccList.Add(oItem); } #endregion #region Excel Column Name Validation public void ColumnNameValidation(DataUploadColumnDefinition columnDefinitions, DataTable uploadData) { bool isExist=false; foreach (string item in columnDefinitions.ColumnName) { isExist=false; foreach(DataColumn col in uploadData.Columns) { if (col.ColumnName.ToUpper().Trim() == item.ToUpper().Trim()) { col.ColumnName = item; isExist = true; break; } } if (isExist==false) GenerateErrorList(item, 1, " column not found in the excel data"); } } #endregion #region Excel Null Value Validation private bool ExcelNullValueValidation(object columnValue) { if (columnValue == null || columnValue.ToString() == string.Empty) return false; else return true; } #endregion #region Excel Column Data Type Validation private bool ExcelDataTypeValidation(string columnName, string columnValue, DataUploadColumnDefinition columnDefinitions) { int index= columnDefinitions.getColumnIndex(columnName); bool IsValid = true; switch ((EnumColumnDataType)Enum.Parse(typeof(EnumColumnDataType), columnDefinitions.DataType[index])) { case EnumColumnDataType.Int: { int number = 0; if (!int.TryParse(columnValue, out number)) IsValid = false; break; } case EnumColumnDataType.String: { //if (columnValue == null || Convert.ToString(columnValue) == string.Empty) // IsValid = false; break; } case EnumColumnDataType.Double: { double number = 0.0; if (!double.TryParse(columnValue, out number)) IsValid = false; break; } case EnumColumnDataType.Date: { DateTime dt = new DateTime(); if (!DateTime.TryParse(columnValue, out dt)) IsValid = false; break; } case EnumColumnDataType.Boolean: { bool value = false; if (!bool.TryParse(columnValue, out value)) IsValid = false; break; } } return IsValid; } #endregion #endregion } }