// GET: /Timesheet/Delete/<id>
        public ActionResult Delete(
            Int32?TimesheetID
            )
        {
            if (
                TimesheetID == null
                )
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            dtEmployee = Timesheet_EmployeeData.SelectAll();
            dtProject  = Timesheet_ProjectData.SelectAll();

            Timesheet Timesheet = new Timesheet();

            Timesheet.TimesheetID = System.Convert.ToInt32(TimesheetID);
            Timesheet             = TimesheetData.Select_Record(Timesheet);
            Timesheet.Employee    = new Employee()
            {
                EmployeeID  = (Int32)Timesheet.EmployeeID
                , FirstName = (from DataRow rowEmployee in dtEmployee.Rows
                               where Timesheet.EmployeeID == (int)rowEmployee["EmployeeID"]
                               select(String) rowEmployee["FirstName"]).FirstOrDefault()
            };
            Timesheet.Project = new Project()
            {
                ProjectID     = (Int32)Timesheet.ProjectID
                , ProjectName = (from DataRow rowProject in dtProject.Rows
                                 where Timesheet.ProjectID == (int)rowProject["ProjectID"]
                                 select(String) rowProject["ProjectName"]).FirstOrDefault()
            };

            if (Timesheet == null)
            {
                return(HttpNotFound());
            }
            return(View(Timesheet));
        }
        // GET: /Timesheet/
        public ActionResult Index(string sortOrder,
                                  String SearchField,
                                  String SearchCondition,
                                  String SearchText,
                                  String Export,
                                  int?PageSize,
                                  int?page,
                                  string command)
        {
            if (command == "Show All")
            {
                SearchField                = null;
                SearchCondition            = null;
                SearchText                 = null;
                Session["SearchField"]     = null;
                Session["SearchCondition"] = null;
                Session["SearchText"]      = null;
            }
            else if (command == "Add New Record")
            {
                return(RedirectToAction("Create"));
            }
            else if (command == "Export")
            {
                Session["Export"] = Export;
            }
            else if (command == "Search" | command == "Page Size")
            {
                if (!string.IsNullOrEmpty(SearchText))
                {
                    Session["SearchField"]     = SearchField;
                    Session["SearchCondition"] = SearchCondition;
                    Session["SearchText"]      = SearchText;
                }
            }
            if (command == "Page Size")
            {
                Session["PageSize"] = PageSize;
            }

            ViewData["SearchFields"]     = GetFields((Session["SearchField"] == null ? "Timesheet I D" : Convert.ToString(Session["SearchField"])));
            ViewData["SearchConditions"] = Library.GetConditions((Session["SearchCondition"] == null ? "Contains" : Convert.ToString(Session["SearchCondition"])));
            ViewData["SearchText"]       = Session["SearchText"];
            ViewData["Exports"]          = Library.GetExports((Session["Export"] == null ? "Pdf" : Convert.ToString(Session["Export"])));
            ViewData["PageSizes"]        = Library.GetPageSizes();

            ViewData["CurrentSort"]         = sortOrder;
            ViewData["TimesheetIDSortParm"] = sortOrder == "TimesheetID_asc" ? "TimesheetID_desc" : "TimesheetID_asc";
            ViewData["EmployeeIDSortParm"]  = sortOrder == "EmployeeID_asc" ? "EmployeeID_desc" : "EmployeeID_asc";
            ViewData["ProjectIDSortParm"]   = sortOrder == "ProjectID_asc" ? "ProjectID_desc" : "ProjectID_asc";
            ViewData["EntryDateSortParm"]   = sortOrder == "EntryDate_asc" ? "EntryDate_desc" : "EntryDate_asc";
            ViewData["StartTimeSortParm"]   = sortOrder == "StartTime_asc" ? "StartTime_desc" : "StartTime_asc";
            ViewData["EndTimeSortParm"]     = sortOrder == "EndTime_asc" ? "EndTime_desc" : "EndTime_asc";
            ViewData["TotTimeSortParm"]     = sortOrder == "TotTime_asc" ? "TotTime_desc" : "TotTime_asc";
            ViewData["RemarksSortParm"]     = sortOrder == "Remarks_asc" ? "Remarks_desc" : "Remarks_asc";

            dtTimesheet = TimesheetData.SelectAll();
            dtEmployee  = Timesheet_EmployeeData.SelectAll();
            dtProject   = Timesheet_ProjectData.SelectAll();

            try
            {
                if (!string.IsNullOrEmpty(Convert.ToString(Session["SearchField"])) & !string.IsNullOrEmpty(Convert.ToString(Session["SearchCondition"])) & !string.IsNullOrEmpty(Convert.ToString(Session["SearchText"])))
                {
                    dtTimesheet = TimesheetData.Search(Convert.ToString(Session["SearchField"]), Convert.ToString(Session["SearchCondition"]), Convert.ToString(Session["SearchText"]));
                }
            }
            catch { }

            var Query = from rowTimesheet in dtTimesheet.AsEnumerable()
                        join rowEmployee in dtEmployee.AsEnumerable() on rowTimesheet.Field <Int32>("EmployeeID") equals rowEmployee.Field <Int32>("EmployeeID")
                        join rowProject in dtProject.AsEnumerable() on rowTimesheet.Field <Int32>("ProjectID") equals rowProject.Field <Int32>("ProjectID")
                        select new Timesheet()
            {
                TimesheetID = rowTimesheet.Field <Int32>("TimesheetID")
                ,
                Employee = new Employee()
                {
                    EmployeeID  = rowEmployee.Field <Int32>("EmployeeID")
                    , FirstName = rowEmployee.Field <String>("FirstName")
                }
                ,
                Project = new Project()
                {
                    ProjectID     = rowProject.Field <Int32>("ProjectID")
                    , ProjectName = rowProject.Field <String>("ProjectName")
                }
                , EntryDate = rowTimesheet.Field <DateTime>("EntryDate")
                , StartTime = rowTimesheet.Field <DateTime>("StartTime")
                , EndTime   = rowTimesheet.Field <DateTime>("EndTime")
                , TotTime   = rowTimesheet.Field <String>("TotTime")
                , Remarks   = rowTimesheet.Field <String>("Remarks")
            };

            switch (sortOrder)
            {
            case "TimesheetID_desc":
                Query = Query.OrderByDescending(s => s.TimesheetID);
                break;

            case "TimesheetID_asc":
                Query = Query.OrderBy(s => s.TimesheetID);
                break;

            case "EmployeeID_desc":
                Query = Query.OrderByDescending(s => s.Employee.FirstName);
                break;

            case "EmployeeID_asc":
                Query = Query.OrderBy(s => s.Employee.FirstName);
                break;

            case "ProjectID_desc":
                Query = Query.OrderByDescending(s => s.Project.ProjectName);
                break;

            case "ProjectID_asc":
                Query = Query.OrderBy(s => s.Project.ProjectName);
                break;

            case "EntryDate_desc":
                Query = Query.OrderByDescending(s => s.EntryDate);
                break;

            case "EntryDate_asc":
                Query = Query.OrderBy(s => s.EntryDate);
                break;

            case "StartTime_desc":
                Query = Query.OrderByDescending(s => s.StartTime);
                break;

            case "StartTime_asc":
                Query = Query.OrderBy(s => s.StartTime);
                break;

            case "EndTime_desc":
                Query = Query.OrderByDescending(s => s.EndTime);
                break;

            case "EndTime_asc":
                Query = Query.OrderBy(s => s.EndTime);
                break;

            case "TotTime_desc":
                Query = Query.OrderByDescending(s => s.TotTime);
                break;

            case "TotTime_asc":
                Query = Query.OrderBy(s => s.TotTime);
                break;

            case "Remarks_desc":
                Query = Query.OrderByDescending(s => s.Remarks);
                break;

            case "Remarks_asc":
                Query = Query.OrderBy(s => s.Remarks);
                break;

            default:      // Name ascending
                Query = Query.OrderBy(s => s.TimesheetID);
                break;
            }

            if (command == "Export")
            {
                GridView  gv = new GridView();
                DataTable dt = new DataTable();
                dt.Columns.Add("Timesheet I D", typeof(string));
                dt.Columns.Add("Employee I D", typeof(string));
                dt.Columns.Add("Project I D", typeof(string));
                dt.Columns.Add("Entry Date", typeof(string));
                dt.Columns.Add("Start Time", typeof(string));
                dt.Columns.Add("End Time", typeof(string));
                dt.Columns.Add("Tot Time", typeof(string));
                dt.Columns.Add("Remarks", typeof(string));
                foreach (var item in Query)
                {
                    dt.Rows.Add(
                        item.TimesheetID
                        , item.Employee.FirstName
                        , item.Project.ProjectName
                        , item.EntryDate
                        , item.StartTime
                        , item.EndTime
                        , item.TotTime
                        , item.Remarks
                        );
                }
                gv.DataSource = dt;
                gv.DataBind();
                ExportData(Export, gv, dt);
            }

            int pageNumber = (page ?? 1);
            int?pageSZ     = (Convert.ToInt32(Session["PageSize"]) == 0 ? 5 : Convert.ToInt32(Session["PageSize"]));

            return(View(Query.ToPagedList(pageNumber, (pageSZ ?? 5))));
        }