public string GetNewPointRepairsDetailId(AddRecordTDS addRecordTDS)
        {
            string detailIDs = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string newDetailID;
            int lastDetailIDIndex = -1;

            foreach (AddRecordTDS.PointRepairsRow row in addRecordTDS.PointRepairs)
            {
                if (row.Deleted == false)
                {
                    int rowIndex = detailIDs.IndexOf(row.DetailID);
                    if (lastDetailIDIndex < rowIndex)
                    {
                        lastDetailIDIndex = rowIndex;
                    }
                }
            }

            if (lastDetailIDIndex < 25)
            {
                lastDetailIDIndex++;
                newDetailID = detailIDs[lastDetailIDIndex].ToString();
            }
            else
            {
                newDetailID = "-1";
            }

            return newDetailID;
        }
        /// ////////////////////////////////////////////////////////////////////////
        /// EVENTS
        ///
        protected void Page_Load(object sender, System.EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            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.");
                }

                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) && ((string)Request.QueryString["record_deleted"] == null))
                {
                    Response.Redirect("./../error_page.aspx?error=" + "Invalid query string in view_fulllength.aspx");
                }

                // Prepare initial values
                Session.Remove("pointRepairDummy");
                addRecordTDS = new AddRecordTDS();

                // If coming from navigator2.aspx or view_scopesheet.aspx
                if (((string)Request.QueryString["source_page"] == "navigator2.aspx") || ((string)Request.QueryString["source_page"] == "view_scopesheet.aspx"))
                {
                    int companyId = Convert.ToInt32(Session["companyID"]);
                    Guid id = (Guid)Session["lfsMasterAreaId"];

                    //... Get lfs master area record
                    LFSRecordGateway lfsRecordGateway = new LFSRecordGateway();
                    tdsLfsRecord = lfsRecordGateway.GetRecordByIdCompanyId(id, companyId);

                    AddRecordPointRepairsGateway addRecordPointRepairsGateway = new AddRecordPointRepairsGateway(addRecordTDS);
                    addRecordPointRepairsGateway.LoadById(id, companyId);

                    //... Store datasets
                    Session["tdsLfsRecord"] = tdsLfsRecord;
                    Session["addRecordTDS"] = addRecordTDS;
                    pointRepairs = addRecordTDS.PointRepairs;
                    Session["pointRepairs"] = pointRepairs;
                }

                // If coming from delete_record.aspx
                if ((string)Request.QueryString["record_deleted"] == "false")
                {
                    //... Restore dataset lfs record
                    tdsLfsRecord = (TDSLFSRecord)Session["tdsLfsRecord"];
                    addRecordTDS = (AddRecordTDS)Session["addRecordTDS"];
                }

                // Prepare initial data
                // ... 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 traffic control
                LFSTrafficControlGateway lfsTrafficControlGateway = new LFSTrafficControlGateway();
                DataSet dsLfsTrafficControl = lfsTrafficControlGateway.GetLFSTrafficControlForDropDownList("");

                ddlDegreeOfTrafficControl.DataSource = dsLfsTrafficControl;
                ddlDegreeOfTrafficControl.DataTextField = "TrafficControl";

                // Databind
                Page.DataBind();

                TDSLFSRecord.LFS_MASTER_AREARow lfsMasterAreaRow = tdsLfsRecord.LFS_MASTER_AREA.FindByIDCOMPANY_ID(new Guid(tbxID.Text), Convert.ToInt32(Session["companyID"]));
                ddlDegreeOfTrafficControl.SelectedValue = (lfsMasterAreaRow.IsDegreeOfTrafficControlNull()) ? "" : lfsMasterAreaRow.DegreeOfTrafficControl;
            }
            else
            {
                // Restore datasets
                tdsLfsRecord = (TDSLFSRecord)Session["tdsLfsRecord"];
                addRecordTDS = (AddRecordTDS)Session["addRecordTDS"];
            }
        }
        private void UpdateDatabase()
        {
            // Initialize extra data
            AddRecordTDS addRecordTDS = new AddRecordTDS();
            ViewJLinersheetTDS viewJLinersheetTDS = new ViewJLinersheetTDS();
            string tdsToWork = "viewFullLengthLining";

            // Save data
            int companyId = Convert.ToInt32(Session["companyID"]);
            LFSRecordGateway lfsRecordGateway = new LFSRecordGateway();
            lfsRecordGateway.UpdateRecord2(tdsLfsRecord, companyId, addRecordTDS, viewFullLengthLiningTDS, viewJLinersheetTDS, tdsToWork);

            //--- Store dataset lfs record
            Session["tdsLfsRecord"] = tdsLfsRecord;
            Session["viewFullLengthLiningTDS"] = viewFullLengthLiningTDS;
            Session["viewJLinersheetTDS"] = viewJLinersheetTDS;
        }
 /// <summary>
 /// InitData
 /// </summary>
 protected override void InitData()
 {
     _data = new AddRecordTDS();
 }
        // ////////////////////////////////////////////////////////////////////////
        // PUBLIC METHODS
        //
        /// <summary>
        /// Insert
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="repairSize">repairSize</param>
        /// <param name="installDate">installDate</param>
        /// <param name="distance">distance</param>
        /// <param name="cost">cost</param>
        /// <param name="reinstates">reinstates</param>
        /// <param name="ltAtMh">ltAtMh</param>
        /// <param name="vtAtMh">vtAtMh</param>
        /// <param name="linerDistance">linerDistance</param>
        /// <param name="direction">direction</param>
        /// <param name="mhShot">mhShot</param>
        /// <param name="comments">comments</param>
        /// <param name="deleted">deleted</param>
        /// <param name="extraRepair">extraRepair</param>
        /// <param name="cancelled">cancelled</param>
        /// <param name="approved">approved</param>
        /// <param name="notApproved">notApproved</param>
        /// <param name="archived">archived</param>
        /// <param name="companyId">companyId</param>        
        /// <param name="addRecordTDS">addRecordTDS</param>
        /// <param name="inDatabase">inDatabase</param>
        public void Insert(Guid id, string repairSize, DateTime? installDate, string distance, decimal? cost, int? reinstates, string ltAtMh, string vtAtMh, string linerDistance, string direction, string mhShot, string comments, bool deleted, bool extraRepair, bool cancelled, bool approved, bool notApproved, bool archived, int companyId, AddRecordTDS addRecordTDS, bool inDatabase)
        {
            AddRecordTDS.PointRepairsRow row = ((AddRecordTDS.PointRepairsDataTable)Table).NewPointRepairsRow();
            row.ID = id;
            row.RefID = GetNewRefId();
            row.COMPANY_ID = companyId;
            row.DetailID = GetNewPointRepairsDetailId(addRecordTDS);
            row.RepairSize = repairSize;
            if (installDate.HasValue) row.InstallDate = (DateTime)installDate; else row.IsInstallDateNull();
            row.Distance = distance;
            if (cost.HasValue) row.Cost = (decimal)cost; else row.IsCostNull();
            if (reinstates.HasValue) row.Reinstates = (int)reinstates; else row.IsReinstatesNull();
            row.LTAtMH = ltAtMh;
            row.VTAtMH = vtAtMh;
            row.LinerDistance = linerDistance;
            row.Direction = direction;
            row.MHShot = mhShot;
            row.Comments = comments;
            row.Deleted = deleted;
            row.ExtraRepair = extraRepair;
            row.Cancelled = cancelled;
            row.Approved = approved;
            row.NotApproved = notApproved;
            row.Archived = archived;
            row.InDatabase = inDatabase;

            ((AddRecordTDS.PointRepairsDataTable)Table).AddPointRepairsRow(row);
        }
        //
        // UpdateRecord2
        //
        // Updates the database from a typed dataset containing a row for LFS_MASTER_AREA,
        // and zero or more rows for LFS_M2_TABLES.
        //
        public void UpdateRecord2(TDSLFSRecord dataSet, int companyId, AddRecordTDS addRecordTDS, ViewFullLengthLiningTDS viewFullLengthLiningTDS, ViewJLinersheetTDS viewJLinersheetTDS, string tdsToWork)
        {
            //--- Prepare transaction
            //--- ... Open connection and start transaction
            dcConnection.Open();
            SqlTransaction transaction2 = dcConnection.BeginTransaction();

            //... Assign transaction to data adapters
            this.daLFSMasterArea.InsertCommand.Transaction = transaction2;
            this.daLFSMasterArea.UpdateCommand.Transaction = transaction2;
            this.daLFSMasterArea.DeleteCommand.Transaction = transaction2;

            this.daLFSJunctionLiner2.InsertCommand.Transaction = transaction2;
            this.daLFSJunctionLiner2.UpdateCommand.Transaction = transaction2;
            this.daLFSJunctionLiner2.DeleteCommand.Transaction = transaction2;

            this.daLfsMasterAreaRct.InsertCommand.Transaction = transaction2;
            this.daLfsMasterAreaRct.UpdateCommand.Transaction = transaction2;
            this.daLfsMasterAreaRct.DeleteCommand.Transaction = transaction2;

            this.daLfsJunctionLinerRct.InsertCommand.Transaction = transaction2;
            this.daLfsJunctionLinerRct.UpdateCommand.Transaction = transaction2;
            this.daLfsJunctionLinerRct.DeleteCommand.Transaction = transaction2;

            try
            {
                //Update database

                //... deletes
                daLFSJunctionLiner2.Update(dataSet.LFS_JUNCTION_LINER2.Select("", "", DataViewRowState.Deleted));
                daLFSMasterArea.Update(dataSet.LFS_MASTER_AREA.Select("", "", DataViewRowState.Deleted));

                //... updates
                daLFSJunctionLiner2.Update(dataSet.LFS_JUNCTION_LINER2.Select("", "", DataViewRowState.ModifiedCurrent));
                daLFSMasterArea.Update(dataSet.LFS_MASTER_AREA.Select("", "", DataViewRowState.ModifiedCurrent));

                //... inserts
                // Point Repairs
                daLFSMasterArea.Update(dataSet.LFS_MASTER_AREA.Select("", "", DataViewRowState.Added));
                daLFSJunctionLiner2.Update(dataSet.LFS_JUNCTION_LINER2.Select("", "", DataViewRowState.Added));

                //... record change tracking records
                daLfsMasterAreaRct.Update(dataSet);

                //--- Commit transaction
                transaction2.Commit();

            }
            catch (DBConcurrencyException dBConcurrencyException)
            {
                //--- Rollback transaction
                transaction2.Rollback();

                //--- Throw exception
                throw new Exception("Concurrency error: Another user already updated the data you are working on.  Your operation has been cancelled.", dBConcurrencyException);
            }
            catch (SqlException sqlException)
            {
                //--- Rollback transaction
                transaction2.Rollback();

                //--- Throw exception
                byte severityLevel = sqlException.Class;
                if (severityLevel <= 16)
                {
                    throw new Exception("Low severity error. Your operation has been cancelled.  SQL Error " + severityLevel + ".");
                }
                if ((severityLevel >= 17) && (severityLevel <= 19))
                {
                    throw new Exception("Mid severity error. Your operation has been cancelled.  SQL Error " + severityLevel + ".");
                }
                if (severityLevel >= 20)
                {
                    throw new Exception("High severity error. Your operation has been cancelled.  SQL Error " + severityLevel + ".");
                }
            }
            catch (Exception e)
            {
                //--- Rollback transaction
                transaction2.Rollback();

                //--- Throw exception
                throw new Exception("Unknow error.  Your operation has been cancelled.", e);
            }
            finally
            {
                //--- Close connection
                if (dcConnection.State == System.Data.ConnectionState.Open)
                {
                    dcConnection.Close();
                }

                object[] array = dataSet.LFS_MASTER_AREA.Rows[0].ItemArray;
                Guid newId = (Guid)array.GetValue(0);

                if (tdsToWork == "addRecord")
                {
                    AddRecordPointRepairs addRecordPointRepairs = new AddRecordPointRepairs(addRecordTDS);
                    addRecordPointRepairs.Save(companyId, newId);
                }

                if (tdsToWork == "viewFullLengthLining")
                {
                    ViewFullLengthLiningLfsM2Tables viewFullLengthLiningLfsM2Tables = new ViewFullLengthLiningLfsM2Tables(viewFullLengthLiningTDS);
                    viewFullLengthLiningLfsM2Tables.Save(companyId, newId);
                }

                if (tdsToWork == "viewJLinersheet")
                {
                    ViewJLinersheetJunctionLiner viewJLinersheetJunctionLiner = new ViewJLinersheetJunctionLiner(viewJLinersheetTDS);
                    viewJLinersheetJunctionLiner.Save(companyId, newId);
                }
            }
        }
        /// ////////////////////////////////////////////////////////////////////////
        /// 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.");
                }

                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) && ((string)Request.QueryString["record_deleted"] == null))
                {
                    Response.Redirect("./../error_page.aspx?error=" + "Invalid query string in view_fulllength.aspx");
                }

                //... Prepare initial
                Session.Remove("pointRepairDummy");
                addRecordTDS = new AddRecordTDS();

                // If coming from navigator2.aspx or view_pointliner.aspx
                if (((string)Request.QueryString["source_page"] == "navigator2.aspx") || ((string)Request.QueryString["source_page"] == "view_pointliner.aspx"))
                {
                    int companyId = Convert.ToInt32(Session["companyID"]);
                    Guid id = (Guid)Session["lfsMasterAreaId"];

                    // Get lfs master area record
                    LFSRecordGateway lfsRecordGateway = new LFSRecordGateway();
                    tdsLfsRecord = lfsRecordGateway.GetRecordByIdCompanyId(id, companyId);

                    AddRecordPointRepairsGateway addRecordPointRepairsGateway = new AddRecordPointRepairsGateway(addRecordTDS);
                    addRecordPointRepairsGateway.LoadById(id, companyId);

                    //... Store datasets
                    Session["tdsLfsRecord"] = tdsLfsRecord;
                    Session["addRecordTDS"] = addRecordTDS;
                    pointRepairs = addRecordTDS.PointRepairs;
                    Session["pointRepairs"] = pointRepairs;
                }

                // If coming from delete_record.aspx
                if ((string)Request.QueryString["record_deleted"] == "false")
                {
                    //... Restore dataset lfs record
                    tdsLfsRecord = (TDSLFSRecord)Session["tdsLfsRecord"];
                    addRecordTDS = (AddRecordTDS)Session["addRecordTDS"];
                }

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

                // Databind
                Page.DataBind();
            }
            else
            {
                // Restore dataset lfs record
                tdsLfsRecord = (TDSLFSRecord)Session["tdsLfsRecord"];
                addRecordTDS = (AddRecordTDS)Session["addRecordTDS"];
            }
        }
        /// ////////////////////////////////////////////////////////////////////////
        /// EVENTS
        ///
        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                //--- Security check
                if (!(Convert.ToBoolean(Session["sgLFS_APP_VIEW"]) && Convert.ToBoolean(Session["sgLFS_APP_ADD"])))
                {
                    Response.Redirect("./../error_page.aspx?error=" + "You are not authorized to add new records. Contact your system administrator.");
                }

                //... Add lfs master area record
                TDSLFSRecord.LFS_MASTER_AREARow lfsMasterAreaRow = tdsLfsRecord.LFS_MASTER_AREA.NewLFS_MASTER_AREARow();
                lfsMasterAreaRow.ID = newId;
                lfsMasterAreaRow.COMPANY_ID = Convert.ToInt32(Session["companyID"]);
                lfsMasterAreaRow.RecordID = "";
                tdsLfsRecord.LFS_MASTER_AREA.AddLFS_MASTER_AREARow(lfsMasterAreaRow);

                //... Prepare initial
                Session.Remove("pointRepairDummy");
                addRecordTDS = new AddRecordTDS();
                lblMaxNumber.Visible = false;

                //... Store datasets
                Session["tdsLfsRecord"] = tdsLfsRecord;
                Session["addRecordTDS"] = addRecordTDS;
                pointRepairs = addRecordTDS.PointRepairs;
                Session["pointRepairs"] = pointRepairs;

                // ... For clients
                CompaniesGateway companiesGateway = new CompaniesGateway();
                DataSet dsCompanies = companiesGateway.GetCompaniesForDropDownList("-1", "", Convert.ToInt32(Session["companyID"]));

                ddlCOMPANIES_ID.DataSource = dsCompanies;
                ddlCOMPANIES_ID.DataValueField = "COMPANIES_ID";
                ddlCOMPANIES_ID.DataTextField = "NAME";
                ddlCOMPANIES_ID.SelectedValue = "-1";

                //... Databind
                ddlCOMPANIES_ID.DataBind();
            }
            else
            {
                //... Restore dataset lfs record
                tdsLfsRecord = (TDSLFSRecord)Session["tdsLfsRecord"];
                addRecordTDS = (AddRecordTDS)Session["addRecordTDS"];
            }
        }