protected void btnCodeSave_Click(object sender, ImageClickEventArgs e)
        {
            var  rptr   = rptrCodes;
            int  i      = 0;
            bool errors = false;

            foreach (RepeaterItem item in rptr.Items)
            {
                i++;
                try
                {
                    var CID         = int.Parse(((TextBox)item.FindControl("CID")).Text);
                    var CTID        = int.Parse(((TextBox)item.FindControl("CTID")).Text);
                    var Code        = ((TextBox)item.FindControl("Code")).Text;
                    var Description = ((TextBox)item.FindControl("Description")).Text;

                    var obj = new DAL.Codes();
                    obj.Fetch(CID);
                    obj.Code        = Code;
                    obj.Description = Description;

                    if (obj.IsValid(BusinessRulesValidationMode.UPDATE))
                    {
                        obj.Update();


                        var masterPage = (IControlRoomMaster)Master;
                        masterPage.PageMessage = SRPResources.SaveAllOK;
                    }
                    else
                    {
                        var    masterPage = (IControlRoomMaster)Master;
                        string message    = String.Format("On Row {1}: " + SRPResources.ApplicationError1, "<ul>", i);
                        foreach (BusinessRulesValidationMessage m in obj.ErrorCodes)
                        {
                            message = string.Format(String.Format("{0}<li>{{0}}</li>", message), m.ErrorMessage);
                        }
                        message = string.Format("{0}</ul>", message);
                        masterPage.PageError = message;
                        errors = true;
                    }
                }
                catch (Exception ex)
                {
                    var masterPage = (IControlRoomMaster)Master;
                    masterPage.PageError = String.Format("On Row {1}: " + SRPResources.ApplicationError1, ex.Message, i);
                    errors = true;
                }
            }

            if (!errors)
            {
                ShowDD();
            }
        }
        protected void rptrCodes_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DeleteRecord")
            {
                var key = int.Parse(e.CommandArgument.ToString());
                try
                {
                    var obj = new DAL.Codes();
                    if (obj.IsValid(BusinessRulesValidationMode.DELETE))
                    {
                        DAL.Codes.FetchObject(key).Delete();
                        ShowDD();

                        var masterPage = (IControlRoomMaster)Master;
                        if (masterPage != null)
                        {
                            masterPage.PageMessage = SRPResources.DeleteOK;
                        }
                    }
                    else
                    {
                        var    masterPage = (IControlRoomMaster)Master;
                        string message    = String.Format(SRPResources.ApplicationError1, "<ul>");
                        foreach (BusinessRulesValidationMessage m in obj.ErrorCodes)
                        {
                            message = string.Format(String.Format("{0}<li>{{0}}</li>", message), m.ErrorMessage);
                        }
                        message = string.Format("{0}</ul>", message);
                        if (masterPage != null)
                        {
                            masterPage.PageError = message;
                        }
                    }
                }
                catch (Exception ex)
                {
                    var masterPage = (IControlRoomMaster)Master;
                    if (masterPage != null)
                    {
                        masterPage.PageError = String.Format(SRPResources.ApplicationError1, ex.Message);
                    }
                }
            }
        }
        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                var obj = new DAL.Codes();
                obj.CTID        = int.Parse(ddlCodeTypes.SelectedValue.ToString());
                obj.Code        = CodeAdd.Text;
                obj.Description = DescriptionAdd.Text;


                if (obj.IsValid(BusinessRulesValidationMode.INSERT))
                {
                    obj.Insert();
                    LoadData();
                    ddlCodeTypes.SelectedValue = obj.CTID.ToString();
                    ShowDD();

                    var masterPage = (IControlRoomMaster)Master;
                    masterPage.PageMessage = SRPResources.AddedOK;
                }
                else
                {
                    var    masterPage = (IControlRoomMaster)Master;
                    string message    = String.Format(SRPResources.ApplicationError1, "<ul>");
                    foreach (BusinessRulesValidationMessage m in obj.ErrorCodes)
                    {
                        message = string.Format(String.Format("{0}<li>{{0}}</li>", message), m.ErrorMessage);
                    }
                    message = string.Format("{0}</ul>", message);
                    masterPage.PageError = message;
                }
            }
            catch (Exception ex)
            {
                var masterPage = (IControlRoomMaster)Master;
                masterPage.PageError = String.Format(SRPResources.ApplicationError1, ex.Message);
            }
        }
        protected void btnCodeSave_Click(object sender, ImageClickEventArgs e)
        {
            var rptr = rptrCodes;
            int i = 0;
            bool errors = false;
            foreach (RepeaterItem item in rptr.Items)
            {

                i++;
                try
                {
                    var CID = int.Parse(((TextBox)item.FindControl("CID")).Text);
                    var CTID = int.Parse(((TextBox)item.FindControl("CTID")).Text);
                    var Code = ((TextBox)item.FindControl("Code")).Text;
                    var Description = ((TextBox)item.FindControl("Description")).Text;

                    var obj = new DAL.Codes();
                    obj.Fetch(CID);
                    obj.Code = Code;
                    obj.Description = Description;

                    if (obj.IsValid(BusinessRulesValidationMode.UPDATE))
                    {
                        obj.Update();
                        

                        var masterPage = (IControlRoomMaster)Master;
                        masterPage.PageMessage = SRPResources.SaveAllOK;
                    }
                    else
                    {
                        var masterPage = (IControlRoomMaster)Master;
                        string message = String.Format("On Row {1}: " + SRPResources.ApplicationError1, "<ul>", i);
                        foreach (BusinessRulesValidationMessage m in obj.ErrorCodes)
                        {
                            message = string.Format(String.Format("{0}<li>{{0}}</li>", message), m.ErrorMessage);
                        }
                        message = string.Format("{0}</ul>", message);
                        masterPage.PageError = message;
                        errors = true;
                    }


                }
                catch (Exception ex)
                {
                    var masterPage = (IControlRoomMaster)Master;
                    masterPage.PageError = String.Format("On Row {1}: " + SRPResources.ApplicationError1, ex.Message, i);
                    errors = true;
                }

            }

            if (!errors) ShowDD();
        }
        protected void rptrCodes_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DeleteRecord")
            {

                var key = int.Parse(e.CommandArgument.ToString());
                try
                {
                    var obj = new DAL.Codes();
                    if (obj.IsValid(BusinessRulesValidationMode.DELETE))
                    {
                        DAL.Codes.FetchObject(key).Delete();
                        ShowDD();

                        var masterPage = (IControlRoomMaster)Master;
                        if (masterPage != null) masterPage.PageMessage = SRPResources.DeleteOK;
                    }
                    else
                    {
                        var masterPage = (IControlRoomMaster)Master;
                        string message = String.Format(SRPResources.ApplicationError1, "<ul>");
                        foreach (BusinessRulesValidationMessage m in obj.ErrorCodes)
                        {
                            message = string.Format(String.Format("{0}<li>{{0}}</li>", message), m.ErrorMessage);
                        }
                        message = string.Format("{0}</ul>", message);
                        if (masterPage != null) masterPage.PageError = message;
                    }
                }
                catch (Exception ex)
                {
                    var masterPage = (IControlRoomMaster)Master;
                    if (masterPage != null)
                        masterPage.PageError = String.Format(SRPResources.ApplicationError1, ex.Message);
                }


            }
        }
        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                var obj = new DAL.Codes();
                obj.CTID = int.Parse(ddlCodeTypes.SelectedValue.ToString());
                obj.Code = CodeAdd.Text;
                obj.Description = DescriptionAdd.Text;

              
                if (obj.IsValid(BusinessRulesValidationMode.INSERT))
                {
                    obj.Insert();                    
                    LoadData();
                    ddlCodeTypes.SelectedValue = obj.CTID.ToString();
                    ShowDD();

                    var masterPage = (IControlRoomMaster)Master;
                    masterPage.PageMessage = SRPResources.AddedOK;
                }
                else
                {
                    var masterPage = (IControlRoomMaster)Master;
                    string message = String.Format(SRPResources.ApplicationError1, "<ul>");
                    foreach (BusinessRulesValidationMessage m in obj.ErrorCodes)
                    {
                        message = string.Format(String.Format("{0}<li>{{0}}</li>", message), m.ErrorMessage);
                    }
                    message = string.Format("{0}</ul>", message);
                    masterPage.PageError = message;
                }
                

            }
            catch (Exception ex)
            {
                var masterPage = (IControlRoomMaster)Master;
                masterPage.PageError = String.Format(SRPResources.ApplicationError1, ex.Message);
            }
        }