/// <summary>
        /// Get Headcount by Id
        /// </summary>
        /// <param name="r"></param>
        /// <returns></returns>
        public HeadCountDTO Get(HeadCountList r)
        {
            HeadCount headcount = null;
            List <UploadFileDetail> uploadedFiles = null;

            using (var dbCmd = Db.CreateCommand())
            {
                dbCmd.CommandType    = CommandType.StoredProcedure;
                dbCmd.CommandTimeout = 300;
                dbCmd.Parameters.Add(new SqlParameter("@Id", r.Id));
                dbCmd.CommandText = "sp_GetHeadcountDetailById";
                using (var reader = dbCmd.ExecuteReader())
                {
                    headcount = reader.ConvertTo <HeadCount>();
                }
            }

            var result = new HeadCountDTO();

            result.HeadCountItem = headcount == null ? new HeadCount() : headcount;

            //Get File Name with Id
            using (var dbCmd = Db.CreateCommand())
            {
                dbCmd.CommandType    = CommandType.StoredProcedure;
                dbCmd.CommandTimeout = 300;
                dbCmd.Parameters.Add(new SqlParameter("@Id", r.Id));
                dbCmd.CommandText = "sp_GetUploadedFilesByHeadcountId";
                using (var reader = dbCmd.ExecuteReader())
                {
                    uploadedFiles = reader.ConvertToList <UploadFileDetail>();
                }
            }

            if (uploadedFiles.Count > 0)
            {
                result.UploadFileDetails = uploadedFiles;
            }
            else
            {
                result.UploadFileDetails = new List <UploadFileDetail>();
            }

            return(result);
        }
        public HeadCountDTO Post(HeadCountSave p)
        {
            using (System.Data.IDbTransaction dbTrans = Db.BeginTransaction())
            {
                try
                {
                    HeadCount HeadCountItem = p.Headcount;
                    //If inserting
                    if (HeadCountItem.Id == 0)
                    {
                        HeadCountItem.CreatedBy   = HeadCountItem.UpdatedBy = WindowsHelper.WindowsUserName;
                        HeadCountItem.CreatedDate = HeadCountItem.UpdatedDate = DateTime.Now;
                        HeadCountItem.Id          = Convert.ToInt32(Db.GetLastInsertId());
                    }
                    // Else updating
                    else
                    {
                        HeadCountItem.UpdatedBy   = WindowsHelper.WindowsUserName;
                        HeadCountItem.UpdatedDate = DateTime.Now;
                    }

                    // Save and Commit all changes to the database
                    Db.Save <HeadCount>(HeadCountItem);


                    if (HeadCountItem.Id == 0)
                    {
                        HeadCountItem.Id = Convert.ToInt32(Db.GetLastInsertId());
                    }

                    List <UploadFileDetail> UploadFileDetailsList = p.UploadFileDetailList;
                    Db.Delete <UploadFileDetail>(x => x.HeadCountID == HeadCountItem.Id);



                    foreach (UploadFileDetail item in UploadFileDetailsList)
                    {
                        Db.Insert(new UploadFileDetail
                        {
                            HeadCountID = HeadCountItem.Id,
                            ContentType = item.ContentType,
                            DisplayName = item.DisplayName,
                            Extension   = item.Extension,
                            FileData    = item.FileData,
                            FileSize    = item.FileSize,
                            Name_File   = item.Name_File,
                            UploadType  = item.UploadType,
                            CreatedBy   = WindowsHelper.WindowsUserName,
                            UpdatedBy   = WindowsHelper.WindowsUserName,
                            CreatedDate = DateTime.Now,
                            UpdatedDate = DateTime.Now
                        });
                    }

                    dbTrans.Commit();

                    HeadCountList r = new HeadCountList();
                    r.Id = HeadCountItem.Id;

                    return(Get(r));
                }
                catch (Exception ex)
                {
                    dbTrans.Rollback();
                    ErrorLogger.InsertError("Could not update HeadCount", ex);
                    throw new Exception("Could not update HeadCount", ex);
                }
            }
        }