/// ////////////////////////////////////////////////////////////////////////
        /// EVENTS
        ///
        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                //--- Security check
                if (!Convert.ToBoolean(Session["sgLFS_APP_VIEW"]))
                {
                    Response.Redirect("./../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                }

                // Prepare initial
                Session.Remove("lfsM2TablesDummy");

                //... Restore data set lfs record
                tdsLfsRecord = (TDSLFSRecord)Session["tdsLfsRecord"];

                // ... for client
                CompaniesGateway companiesGateway = new CompaniesGateway();
                tbxCOMPANIES_ID.Text = companiesGateway.GetName((int)tdsLfsRecord.LFS_MASTER_AREA.Rows[0]["COMPANIES_ID"], Convert.ToInt32(Session["companyID"]));

                // ... for measurement type
                LFSMeasurementTypeGateway lfsMeasurementTypeGateway = new LFSMeasurementTypeGateway();
                DataSet dsLfsMeasurementType = lfsMeasurementTypeGateway.GetLFSMeasurementTypeForDropDownList("");

                ddlMeasurementType.DataSource = dsLfsMeasurementType;
                ddlMeasurementType.DataTextField = "MeasurementType";

                // Load LfsM2Tables data
                int companyId = Convert.ToInt32(Session["companyID"]);
                Guid id = new Guid(Request.QueryString["id"].ToString());
                hdfId.Value = Request.QueryString["id"].ToString();

                viewFullLengthLiningTDS = new ViewFullLengthLiningTDS();
                ViewFullLengthLiningLfsM2TablesGateway viewFullLengthLiningLfsM2TablesGateway = new ViewFullLengthLiningLfsM2TablesGateway(viewFullLengthLiningTDS);
                viewFullLengthLiningLfsM2TablesGateway.LoadById(id, companyId);

                Session["viewFullLengthLiningTDS"] = viewFullLengthLiningTDS;
                lfsM2Tables = viewFullLengthLiningTDS.LfsM2Tables;
                Session["lfsM2Tables"] = lfsM2Tables;

                // Databind
                Page.DataBind();

                TDSLFSRecord.LFS_MASTER_AREARow lfsMasterAreaRow = tdsLfsRecord.LFS_MASTER_AREA.FindByIDCOMPANY_ID(new Guid(tbxID.Text), Convert.ToInt32(Session["companyID"]));
                ddlMeasurementType.SelectedValue = (lfsMasterAreaRow.IsMeasurementTypeNull()) ? "" : lfsMasterAreaRow.MeasurementType;
            }
            else
            {
                // Restore data set lfs record
                tdsLfsRecord = (TDSLFSRecord)Session["tdsLfsRecord"];
                viewFullLengthLiningTDS = (ViewFullLengthLiningTDS)Session["viewFullLengthLiningTDS"];
            }
        }
        /// <summary>
        /// Save all to database (direct)
        /// </summary>
        /// <param name="companyId">companyId</param>
        /// <param name="newId">newId</param>
        public void Save(int companyId, Guid newId)
        {
            ViewFullLengthLiningTDS lfsM2TablesChanges = (ViewFullLengthLiningTDS)Data.GetChanges();

            if (lfsM2TablesChanges.LfsM2Tables.Rows.Count > 0)
            {
                ViewFullLengthLiningLfsM2TablesGateway viewFullLengthLiningLfsM2TablesGateway = new ViewFullLengthLiningLfsM2TablesGateway(lfsM2TablesChanges);

                foreach (ViewFullLengthLiningTDS.LfsM2TablesRow row in (ViewFullLengthLiningTDS.LfsM2TablesDataTable)lfsM2TablesChanges.LfsM2Tables)
                {
                    // Insert new Notes
                    if ((!row.Deleted) && (!row.InDatabase))
                    {
                        float? videoDistance = null; if (!row.IsVideoDistanceNull()) videoDistance = row.VideoDistance;
                        string clockPosition = ""; if (!row.IsClockPositionNull()) clockPosition = row.ClockPosition;
                        string liveOrAbandoned = ""; if (!row.IsLiveOrAbandonedNull()) liveOrAbandoned = row.LiveOrAbandoned;
                        string distanceToCentreOfLateral = ""; if (!row.IsDistanceToCentreOfLateralNull()) distanceToCentreOfLateral = row.DistanceToCentreOfLateral;
                        string lateralDiameter = ""; if (!row.IsLateralDiameterNull()) lateralDiameter = row.LateralDiameter;
                        string lateralType = ""; if (!row.IsLateralTypeNull()) lateralType = row.LateralType;
                        string dateTimeOpened = ""; if (!row.IsDateTimeOpenedNull()) dateTimeOpened = row.DateTimeOpened;
                        string comments = ""; if (!row.IsCommentsNull()) comments = row.Comments;
                        string reverseSetup = ""; if (!row.IsReverseSetupNull()) reverseSetup = row.ReverseSetup;
                        bool deleted = false;
                        bool archived = row.Archived;

                        LFSRecordM2Tables lfsRecordM2Tables = new LFSRecordM2Tables(null);
                        lfsRecordM2Tables.InsertDirect(newId, row.RefID, row.COMPANY_ID, videoDistance, clockPosition, liveOrAbandoned, distanceToCentreOfLateral, lateralDiameter, lateralType, dateTimeOpened, comments, reverseSetup, deleted, archived);
                    }

                    // Update Notes
                    if ((!row.Deleted) && (row.InDatabase))
                    {
                        Guid id = row.ID;
                        int refId = row.RefID;

                        // original values
                        float? originalVideoDistance = viewFullLengthLiningLfsM2TablesGateway.GetVideoDistanceOriginal(id, refId, companyId);
                        string originalClockPosition = viewFullLengthLiningLfsM2TablesGateway.GetClockPositionOriginal(id, refId, companyId);
                        string originalLiveOrAbandoned = viewFullLengthLiningLfsM2TablesGateway.GetLiveOrAbandonedOriginal(id, refId, companyId);
                        string originalDistanceToCentreOfLateral = viewFullLengthLiningLfsM2TablesGateway.GetDistanceToCentreOfLateralOriginal(id, refId, companyId);
                        string originalLateralDiameter = viewFullLengthLiningLfsM2TablesGateway.GetLateralDiameterOriginal(id, refId, companyId);
                        string originalLateralType = viewFullLengthLiningLfsM2TablesGateway.GetLateralTypeOriginal(id, refId, companyId);
                        string originalDateTimeOpened = viewFullLengthLiningLfsM2TablesGateway.GetDateTimeOpenedOriginal(id, refId, companyId);
                        string originalComments = viewFullLengthLiningLfsM2TablesGateway.GetCommentsOriginal(id, refId, companyId);
                        string originalReverseSetup = viewFullLengthLiningLfsM2TablesGateway.GetReverseSetupOriginal(id, refId, companyId);
                        bool originalDeleted = false;
                        bool originalArchived = viewFullLengthLiningLfsM2TablesGateway.GetArchivedOriginal(id, refId, companyId);

                        // new values
                        float? newVideoDistance = viewFullLengthLiningLfsM2TablesGateway.GetVideoDistance(id, refId, companyId);
                        string newClockPosition = viewFullLengthLiningLfsM2TablesGateway.GetClockPosition(id, refId, companyId);
                        string newLiveOrAbandoned = viewFullLengthLiningLfsM2TablesGateway.GetLiveOrAbandoned(id, refId, companyId);
                        string newDistanceToCentreOfLateral = viewFullLengthLiningLfsM2TablesGateway.GetDistanceToCentreOfLateral(id, refId, companyId);
                        string newLateralDiameter = viewFullLengthLiningLfsM2TablesGateway.GetLateralDiameter(id, refId, companyId);
                        string newLateralType = viewFullLengthLiningLfsM2TablesGateway.GetLateralType(id, refId, companyId);
                        string newDateTimeOpened = viewFullLengthLiningLfsM2TablesGateway.GetDateTimeOpened(id, refId, companyId);
                        string newComments = viewFullLengthLiningLfsM2TablesGateway.GetComments(id, refId, companyId);
                        string newReverseSetup = viewFullLengthLiningLfsM2TablesGateway.GetReverseSetup(id, refId, companyId);
                        bool newDeleted = false;
                        bool newArchived = viewFullLengthLiningLfsM2TablesGateway.GetArchived(id, refId, companyId);

                        LFSRecordM2Tables lfsRecordM2Tables = new LFSRecordM2Tables(null);
                        lfsRecordM2Tables.UpdateDirect(id, refId, row.COMPANY_ID, originalVideoDistance, originalClockPosition, originalLiveOrAbandoned, originalDistanceToCentreOfLateral, originalLateralDiameter, originalLateralType, originalDateTimeOpened, originalComments, originalReverseSetup, originalDeleted, originalArchived, id, refId, row.COMPANY_ID, newVideoDistance, newClockPosition, newLiveOrAbandoned, newDistanceToCentreOfLateral, newLateralDiameter, newLateralType, newDateTimeOpened, newComments, newReverseSetup, newDeleted, newArchived);
                    }

                    // Deleted notes
                    if ((row.Deleted) && (row.InDatabase))
                    {
                        LFSRecordM2Tables lfsRecordM2Tables = new LFSRecordM2Tables(null);
                        lfsRecordM2Tables.DeleteDirect(row.ID, row.RefID, row.COMPANY_ID);
                    }
                }
            }
        }