protected void btnDelete_Click(object sender, EventArgs e)
        {
            PostPageChanges();

            ArrayList jlinerIdArrayList = GetJlinerIdArrayList();
            if (jlinerIdArrayList.Count > 0)
            {
                FlatSectionJliner flatSectionJliner = new FlatSectionJliner();
                flatSectionJliner.LoadBySaleDetailIdArrayList(jlinerIdArrayList, 1);

                if (flatSectionJliner.Table.Rows.Count > 0)
                {
                    // Delete store dataset
                    Session.Remove("flatSectionJlinerTDS");

                    // Store datasets
                    Session["jlinerNavigatorTDS"] = jlinerNavigatorTDS;
                    Session["flatSectionJlinerTDS"] = flatSectionJliner.Data;

                    // Redirect
                    string url = "./jliner_delete.aspx?source_page=jliner_navigator2.aspx&client=" + hdfCurrentClient.Value + GetNavigatorState();
                    Response.Redirect(url);
                }
            }
            else
            {
                RestoreNavigatorState();
                cvSelection.IsValid = false;
            }
        }
        private void Save()
        {
            FlatSectionJliner flatSectionJliner = new FlatSectionJliner(flatSectionJlinerTDS);
            DataView dataViewFlatSectionJliner = new DataView(flatSectionJlinerTDS.FlatSectionJliner);
            dataViewFlatSectionJliner.RowFilter = "(Selected = 1) AND (Deleted = 0)";
            string summary = "";
            bool existsDataModificated = false;

            foreach (DataRowView row in dataViewFlatSectionJliner)
            {
                if (row["Issue"].ToString() == "No" || row["Issue"].ToString() == "Dig Required After Lining")
                {
                    string id_ = row["ID_"].ToString();
                    DateTime? value = null; if (tbxDate.Text.ToString() != "") value = DateTime.Parse(tbxDate.Text);

                    // ... Update row
                    flatSectionJliner.UpdateField(id_, ddlFieldToUpdate.SelectedValue, value);

                    existsDataModificated = true;
                }
                else
                {
                    if (summary.Trim().Length == 0)
                    {
                        summary = "The following laterals were not updated:\n\n";
                        summary = summary + "\t - " + row["ID_"].ToString() + "\n";
                    }
                    else
                    {
                        summary = summary + "\t - " + row["ID_"].ToString() + "\n";
                    }
                }
            }

            if (summary.Trim().Length == 0)
            {
                summary = "All the laterals were updated\n\n";
            }

            tbxSummary.Text = summary;

            // Store datasets
            Session["flatSectionJlinerTDS"] = flatSectionJlinerTDS;

            if (existsDataModificated)
            {
                // Update section and jliners
                sectionTDS = new SectionTDS();
                flatSectionJliner.Save(sectionTDS);

                // Update database
                UpdateDatabase();
            }
        }
        private int Update()
        {
            FlatSectionJliner flatSectionJliner = new FlatSectionJliner(flatSectionJlinerTDS);
            int jlinerSelected = 0;

            // Update flatSectionJlinerTDS
            foreach (GridViewRow row in grdvJliner.Rows)
            {
                string id_ = ((TextBox)row.FindControl("tbxId_")).Text;
                bool selected = ((CheckBox)row.FindControl("cbxSelected")).Checked;

                flatSectionJliner.UpdateSelected(id_, selected);

                if (selected) jlinerSelected++;
            }

            // Save jliners selection
            flatSectionJlinerTDS.AcceptChanges();

            // Store datasets
            Session["flatSectionJlinerTDS"] = flatSectionJlinerTDS;

            return jlinerSelected;
        }
        private void Save2()
        {
            Page.Validate();

            if (Page.IsValid)
            {
                FlatSectionJliner flatSectionJliner = new FlatSectionJliner(flatSectionJlinerTDS);

                // Update flatSectionJlinerTDS
                foreach (GridViewRow row in grdvJliner.Rows)
                {
                    // ... Get standard fields
                    Guid id = new Guid(((HiddenField)row.FindControl("hdfId")).Value);
                    int companyId = int.Parse(((HiddenField)row.FindControl("hdfCompanyId")).Value);
                    int refId = int.Parse(((HiddenField)row.FindControl("hdfRefId")).Value);
                    string recordId = ((HiddenField)row.FindControl("hdfRecordId")).Value;
                    string detailId = ((HiddenField)row.FindControl("hdfDetailId")).Value;
                    string id_ = ((TextBox)row.FindControl("tbxId_")).Text.Trim();
                    string address = ((TextBox)row.FindControl("tbxAddress")).Text.Trim();
                    DateTime? pipeLocated = null; if (((TextBox)row.FindControl("tbxPipeLocated")).Text.Trim() != "") pipeLocated = DateTime.Parse(((TextBox)row.FindControl("tbxPipeLocated")).Text.Trim());
                    DateTime? servicesLocated = null; if (((TextBox)row.FindControl("tbxServicesLocated")).Text.Trim() != "") servicesLocated = DateTime.Parse(((TextBox)row.FindControl("tbxServicesLocated")).Text.Trim());
                    DateTime? coInstalled = null; if (((TextBox)row.FindControl("tbxCoInstalled")).Text.Trim() != "") coInstalled = DateTime.Parse(((TextBox)row.FindControl("tbxCoInstalled")).Text.Trim());
                    DateTime? backfilledConcrete = null; if (((TextBox)row.FindControl("tbxBackfilledConcrete")).Text.Trim() != "") backfilledConcrete = DateTime.Parse(((TextBox)row.FindControl("tbxBackfilledConcrete")).Text.Trim());
                    DateTime? backfilledSoil = null; if (((TextBox)row.FindControl("tbxBackfilledSoil")).Text.Trim() != "") backfilledSoil = DateTime.Parse(((TextBox)row.FindControl("tbxBackfilledSoil")).Text.Trim());
                    DateTime? grouted = null; if (((TextBox)row.FindControl("tbxGrouted")).Text.Trim() != "") grouted = DateTime.Parse(((TextBox)row.FindControl("tbxGrouted")).Text.Trim());
                    DateTime? cored = null; if (((TextBox)row.FindControl("tbxCored")).Text.Trim() != "") cored = DateTime.Parse(((TextBox)row.FindControl("tbxCored")).Text.Trim());
                    DateTime? prepped = null; if (((TextBox)row.FindControl("tbxPrepped")).Text.Trim() != "") prepped = DateTime.Parse(((TextBox)row.FindControl("tbxPrepped")).Text.Trim());
                    DateTime? measured = null; if (((TextBox)row.FindControl("tbxMeasured")).Text.Trim() != "") measured = DateTime.Parse(((TextBox)row.FindControl("tbxMeasured")).Text.Trim());
                    string linerSize = ""; if (((TextBox)row.FindControl("tbxLinerSize")).Text.Trim() != "") linerSize = ((TextBox)row.FindControl("tbxLinerSize")).Text.Trim();
                    bool liningThruCo = ((CheckBox)row.FindControl("cbxLiningThruCo")).Checked;
                    DateTime? inProcess = null; if (((TextBox)row.FindControl("tbxInProcess")).Text.Trim() != "") inProcess = DateTime.Parse(((TextBox)row.FindControl("tbxInProcess")).Text.Trim());
                    DateTime? inStock = null; if (((TextBox)row.FindControl("tbxInStock")).Text.Trim() != "") inStock = DateTime.Parse(((TextBox)row.FindControl("tbxInStock")).Text.Trim());
                    DateTime? delivered = null; if (((TextBox)row.FindControl("tbxDelivered")).Text.Trim() != "") delivered = DateTime.Parse(((TextBox)row.FindControl("tbxDelivered")).Text.Trim());
                    DateTime? preVideo = null; if (((TextBox)row.FindControl("tbxPreVideo")).Text.Trim() != "") preVideo = DateTime.Parse(((TextBox)row.FindControl("tbxPreVideo")).Text.Trim());
                    DateTime? linerInstalled = null; if (((TextBox)row.FindControl("tbxLinerInstalled")).Text.Trim() != "") linerInstalled = DateTime.Parse(((TextBox)row.FindControl("tbxLinerInstalled")).Text.Trim());
                    DateTime? finalVideo = null; if (((TextBox)row.FindControl("tbxFinalVideo")).Text.Trim() != "") finalVideo = DateTime.Parse(((TextBox)row.FindControl("tbxFinalVideo")).Text.Trim());
                    double? distanceFromUSMH = null; if (((TextBox)row.FindControl("tbxDistanceFromUSMH")).Text.Trim() != "") distanceFromUSMH = double.Parse(((TextBox)row.FindControl("tbxDistanceFromUSMH")).Text.Trim());
                    string comments = ""; if (((TextBox)row.FindControl("tbxComments")).Text.Trim() != "") comments = ((TextBox)row.FindControl("tbxComments")).Text.Trim();
                    string history = ""; if (((TextBox)row.FindControl("tbxHistory")).Text.Trim() != "") history = ((TextBox)row.FindControl("tbxHistory")).Text.Trim();
                    string issue = ((DropDownList)row.FindControl("ddlIssue")).SelectedValue.Trim();
                    decimal? cost = null; if (((TextBox)row.FindControl("tbxCost")).Text.Trim() != "") cost = decimal.Parse(((TextBox)row.FindControl("tbxCost")).Text.Trim());
                    bool deleted = false;
                    bool selected = true;
                    DateTime? videoInspection = null; if (((TextBox)row.FindControl("tbxVideoInspection")).Text.Trim() != "") videoInspection = DateTime.Parse(((TextBox)row.FindControl("tbxVideoInspection")).Text.Trim());
                    string videoLengthToPropertyLine = ""; if (((TextBox)row.FindControl("tbxVideoLengthToPropertyLine")).Text.Trim() != "") videoLengthToPropertyLine = ((TextBox)row.FindControl("tbxVideoLengthToPropertyLine")).Text.Trim();
                    bool coRequired = ((CheckBox)row.FindControl("cbxCoReq")).Checked;
                    bool pitRequired = ((CheckBox)row.FindControl("cbxPitReq")).Checked;
                    string coPitLocation = ""; coPitLocation = ((DropDownList)row.FindControl("ddlCoPitLocation")).SelectedValue;
                    bool postContractDigRequired = ((CheckBox)row.FindControl("cbxPostContractDigRequired")).Checked;
                    DateTime? coCutDown = null; if (((TextBox)row.FindControl("tbxCoCutDown")).Text.Trim() != "") coCutDown = DateTime.Parse(((TextBox)row.FindControl("tbxCoCutDown")).Text.Trim());
                    DateTime? finalRestoration = null; if (((TextBox)row.FindControl("tbxFinalRestoration")).Text.Trim() != "") finalRestoration = DateTime.Parse(((TextBox)row.FindControl("tbxFinalRestoration")).Text.Trim());
                    string clientLateralId = ""; if (((TextBox)row.FindControl("tbxClientLateralId")).Text.Trim() != "") clientLateralId = ((TextBox)row.FindControl("tbxClientLateralId")).Text.Trim();
                    string hamiltonInspectionNumber = ""; if (((TextBox)row.FindControl("tbxHamiltonInspectionNumber")).Text.Trim() != "") hamiltonInspectionNumber = ((TextBox)row.FindControl("tbxHamiltonInspectionNumber")).Text.Trim();
                    DateTime? noticeDelivered = null; if (((TextBox)row.FindControl("tbxNoticeDelivered")).Text.Trim() != "") noticeDelivered = DateTime.Parse(((TextBox)row.FindControl("tbxNoticeDelivered")).Text.Trim());

                    // ... Calculate fields
                    double? distanceFromDSMH = null;
                    if (distanceFromUSMH.HasValue)
                    {
                        SectionGateway sectionGateway = new SectionGateway();
                        sectionGateway.LoadById(id, companyId);

                        Distance length = new Distance(sectionGateway.GetActualLength(id)) - new Distance(((double)distanceFromUSMH).ToString());
                        distanceFromDSMH = length.ToDoubleInEng3();
                    }

                    // ... Update row
                    flatSectionJliner.Update(id_, id, refId, companyId, recordId, detailId, address, pipeLocated, servicesLocated, coInstalled, backfilledConcrete, backfilledSoil, grouted, cored, prepped, measured, linerSize, inProcess, inStock, delivered, preVideo, linerInstalled, finalVideo, distanceFromUSMH, distanceFromDSMH, comments, history, issue, cost, deleted, selected, videoInspection, coRequired, pitRequired, coPitLocation, postContractDigRequired, coCutDown, finalRestoration, clientLateralId, videoLengthToPropertyLine, liningThruCo, hamiltonInspectionNumber, noticeDelivered);
                }

                // Store datasets
                Session["flatSectionJlinerTDS"] = flatSectionJlinerTDS;

                // Update section and jliners
                sectionTDS = new SectionTDS();
                flatSectionJliner.Save(sectionTDS);
            }
        }
        private void UpdateDatabase()
        {
            DB.Open();
            DB.BeginTransaction();
            try
            {
                Guid id = new Guid(hdfId.Value);
                int refId = Int32.Parse(hdfRefId.Value);
                int companyId = Int32.Parse(hdfCompanyId.Value);

                // Save Comments
                FlatSectionJlinerJuntionLiner2Comment flatSectionJlinerJuntionLiner2Comment = new FlatSectionJlinerJuntionLiner2Comment(flatSectionJlinerTDS);
                flatSectionJlinerJuntionLiner2Comment.Save(id, companyId);

                // Save History
                FlatSectionJlinerJuntionLiner2History flatSectionJlinerJuntionLiner2History = new FlatSectionJlinerJuntionLiner2History(flatSectionJlinerTDS);
                flatSectionJlinerJuntionLiner2History.Save(id, companyId);

                // Save jliner
                FlatSectionJliner flatSectionJlinerUpdateCommentsHistory = new FlatSectionJliner();
                flatSectionJlinerUpdateCommentsHistory.LoadByIdRefId(id, refId, companyId);
                flatSectionJlinerUpdateCommentsHistory.UpdateComments(id.ToString());
                flatSectionJlinerUpdateCommentsHistory.UpdateHistory(id.ToString());

                Session["flatSectionJlinerTDS"] = flatSectionJlinerTDS;
                flatSectionJlinerUpdateCommentsHistory.Save(companyId);

                // Update FlatSectionJliner for new Comments
                FlatSectionJliner flatSectionJlinerForCommentsHistory = new FlatSectionJliner(flatSectionJlinerTDS);
                flatSectionJlinerForCommentsHistory.UpdateCommentsHistoryForSummaryEdit();

                Session["flatSectionJlinerTDS"] = flatSectionJlinerTDS;

                DB.CommitTransaction();
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        private void Delete()
        {
            Page.Validate();

            if (Page.IsValid)
            {
                // Delete jliners
                FlatSectionJliner flatSectionJliner = new FlatSectionJliner(flatSectionJlinerTDS);

                // ... Delete in flatSectionJlinerTDS
                foreach (FlatSectionJlinerTDS.FlatSectionJlinerRow flatSectionJlinerRow in (FlatSectionJlinerTDS.FlatSectionJlinerDataTable)flatSectionJliner.Table)
                {
                    if ((flatSectionJlinerRow.Selected) && (!flatSectionJlinerRow.Deleted))
                    {
                        flatSectionJliner.Delete(flatSectionJlinerRow.ID_);
                    }
                }

                // ... Update section and jliners from flatSectionJlinerTDS
                flatSectionJliner.Save(sectionTDS);

                // Update databse
                UpdateDatabase();

                // Redirect
                string url = "";
                if (Request.QueryString["source_page"] == "jliner_navigator2.aspx")
                {
                    url = "./jliner_navigator2.aspx?source_page=jliner_delete.aspx&client=" + hdfCurrentClient.Value + GetNavigatorState() + "&update=yes";
                }

                if (Request.QueryString["source_page"] == "flat_section_jliner_summary.aspx")
                {
                    url = "./flat_section_jliner_summary.aspx?source_page=jliner_delete.aspx&client=" + hdfCurrentClient.Value + GetNavigatorState() + "&update=yes";
                }
                Response.Redirect(url);
            }
        }