protected void rgIndustries_OnNeedDataSource(object sender, GridNeedDataSourceEventArgs e)
 {
     using (var hrsgEntities = new HRSG_DatabaseEntities())
     {
         rgIndustries.DataSource = hrsgEntities.Industries.Where(a => a.Active).ToList();
     }
 }
        public User CheckLogin(string username, string password)
        {
            using (var hrsgEntites = new HRSG_DatabaseEntities()) {
                //TODO: convert the password to HASHED or encrypted version

                return hrsgEntites.Users.FirstOrDefault(a => a.Username == username && a.Password == password && a.Active);
            }
        }
        protected void Page_Load(object sender, EventArgs e) {
            if (IsPostBack) return;

            using (var hrsgEntities = new HRSG_DatabaseEntities()) {
                var section = hrsgEntities.Sections.FirstOrDefault(a => a.ID == SectionID && a.Active);

                if (section != null) {
                    txtbxSectionName.Text = section.Description;
                    txtbxSectionValue.Text = section.Value;
                }
            }
        }
 private Globals()
 {
     if (!_initialized)
     {
         using (var hrsgEntities = new HRSG_DatabaseEntities())
         {
             //store all the activate industries
             Industries = hrsgEntities.Industries.Where(i => i.Active).ToList();
             EmployeeRanges = hrsgEntities.EmployeeRanges.Where(e => e.Active).ToList();
         }
     }
 }
        protected void btnSave_OnClick(object sender, EventArgs e) {
            using (var hrsgEntities = new HRSG_DatabaseEntities()) {
                var industry = hrsgEntities.Industries.Create();

                industry.Created = DateTime.Now;
                industry.Modified = DateTime.Now;
                industry.Active = true;
                industry.Name = txtbxIndustryName.Text;

                hrsgEntities.Industries.Add(industry);
                hrsgEntities.SaveChanges();

                ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndSave();", true);
            }
        }
        protected void btnSave_OnClick(object sender, EventArgs e) {
            using (var hrsgEntities = new HRSG_DatabaseEntities()) {
                var section = hrsgEntities.Sections.FirstOrDefault(a => a.ID == SectionID && a.Active);

                if (section != null) {
                    section.Modified = DateTime.Now;
                    section.Description = txtbxSectionName.Text;
                    section.Value = txtbxSectionValue.Text;

                    hrsgEntities.SaveChanges();
                }

                ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndSave();", true);
            }
        }
        protected void btnSave_OnClick(object sender, EventArgs e) {
            using (var hrsgEntities = new HRSG_DatabaseEntities()) {
                var section = hrsgEntities.Sections.Create();

                section.Created = DateTime.Now;
                section.Modified = DateTime.Now;
                section.Active = true;
                section.Description = txtbxSectionName.Text;
                section.Value = txtbxSectionValue.Text;

                hrsgEntities.Sections.Add(section);
                hrsgEntities.SaveChanges();

                ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndSave();", true);
            }
        }
        protected void rgIndustries_OnItemCommand(object sender, GridCommandEventArgs e)
        {
            using (var hrsgEntities = new HRSG_DatabaseEntities()) {
                switch (e.CommandName) {
                    case "Edit":

                        break;

                    case "Delete":
                        var id = Convert.ToInt32(e.CommandArgument);    //LINQ does not understand Convert.ToInt(), convert it before passing to the expression
                        var industry = hrsgEntities.Industries.FirstOrDefault(a => a.ID == id && a.Active);

                        if (industry != null) {
                            industry.Active = false;
                            hrsgEntities.SaveChanges();
                        }
                        break;
                }
            }
        }
        protected void rgClients_OnNeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            using (var hrsgEntities = new HRSG_DatabaseEntities())
            {
                var results = hrsgEntities.Clients.Where(c => c.Active).ToList();
                
                //convert the results into a custom listing object
                var clientListing = results.Select(a => new ClientListingItem()
                {
                    ID = a.ID,
                    Modified = a.Modified,
                    Created = a.Created,
                    EmployeeRange = Globals.Instance.EmployeeRanges.FirstOrDefault(b => b.ID == a.EmployeeRange).Description,
                    IndustryID = a.IndustryID,
                    IndustryName = hrsgEntities.Industries.FirstOrDefault(b => b.ID == a.IndustryID).Name,
                    Name = a.Name
                }).ToList();

                rgClients.DataSource = clientListing;
            }
        }
        protected void rgSubsections_OnItemCommand(object sender, GridCommandEventArgs e) {
            

            using (var hrsgEntities = new HRSG_DatabaseEntities()) {
                switch (e.CommandName) {
                    case "Edit":
                        var item = e.Item as GridDataItem;
                        var itemID = item["ID"].Text;

                        ClientScript.RegisterStartupScript(Page.GetType(), "mykey", $"OpenEditSubSection({itemID});", true);
                        break;

                    case "Delete":
                        var id = Convert.ToInt32(e.CommandArgument);    //LINQ does not understand Convert.ToInt(), convert it before passing to the expression
                        var subsection = hrsgEntities.SubSections.FirstOrDefault(a => a.ID == id && a.Active);

                        if (subsection != null) {
                            subsection.Active = false;
                            hrsgEntities.SaveChanges();
                        }
                        break;
                }
            }
        }
 protected void rgSubsections_OnNeedDataSource(object sender, GridNeedDataSourceEventArgs e) {
     using (var hrsgEntities = new HRSG_DatabaseEntities()) {
         rgSubsections.DataSource = hrsgEntities.SubSections.Where(c => c.Active).ToList();
     }
 }