private void UpdateDatabase()
        {
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int unitId = Int32.Parse(hdfUnitId.Value);

            DB.Open();
            DB.BeginTransaction();
            try
            {
                // process view
                UnitInformationTDS dataset = new UnitInformationTDS();
                dataset.ChecklistDetails.Merge(unitsChecklistRulesEdit, true);

                UnitInformationChecklistDetails model = new UnitInformationChecklistDetails(dataset);
                model.Save(unitId, companyId);

                DB.CommitTransaction();

                // Store datasets
                unitInformationTDS.AcceptChanges();
                Session["unitInformationTDS"] = unitInformationTDS;
                Session["unitsChecklistRulesEdit"] = unitsChecklistRulesEdit;
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        private void ChecklistRulesInformationProcessGrid()
        {
            UnitInformationTDS dataSet = new UnitInformationTDS();
            dataSet.ChecklistDetails.Merge(unitsChecklistRulesEdit, true);
            UnitInformationChecklistDetails model = new UnitInformationChecklistDetails(dataSet);

            // update rows
            if (Session["unitsChecklistRulesEditDummy"] == null)
            {
                foreach (GridViewRow row in grdChecklistRulesInformation.Rows)
                {
                    int ruleId = int.Parse(grdChecklistRulesInformation.DataKeys[row.RowIndex].Values["RuleID"].ToString());
                    DateTime? lastService = null;
                    DateTime? nextDue = null;
                    bool done = ((CheckBox)row.FindControl("cbxDone")).Checked;
                    string state = ((DropDownList)row.FindControl("ddlState")).SelectedValue;

                    if (((RadDatePicker)row.FindControl("tkrdpLastService")).SelectedDate.HasValue)
                    {
                        lastService = ((RadDatePicker)row.FindControl("tkrdpLastService")).SelectedDate.Value;
                    }

                    if (((RadDatePicker)row.FindControl("tkrdpNextDue")).SelectedDate.HasValue)
                    {
                        nextDue = ((RadDatePicker)row.FindControl("tkrdpNextDue")).SelectedDate.Value;
                    }

                    model.Update(ruleId, lastService, nextDue, state, done);
                }

                model.Table.AcceptChanges();

                unitsChecklistRulesEdit = (UnitInformationTDS.ChecklistDetailsDataTable)model.Table;
                Session["unitsChecklistRulesEdit"] = unitsChecklistRulesEdit;
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // INITIAL EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_UNITS_VIEW"]) && Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_UNITS_EDIT"])))
                {
                    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["unit_id"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in units_checklist_rules.aspx");
                }

                // Tag page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfUnitId.Value = Request.QueryString["unit_id"].ToString();
                hdfUpdate.Value = "no";

                // If coming from
                // ... units_summary.aspx and units_edit.aspx
                if (Request.QueryString["source_page"] == "units_summary.aspx" || Request.QueryString["source_page"] == "units_edit.aspx")
                {
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];

                    unitInformationTDS = new UnitInformationTDS();
                    unitsChecklistRulesEdit = new UnitInformationTDS.ChecklistDetailsDataTable();

                    int companyId = Int32.Parse(hdfCompanyId.Value.Trim());
                    int unitId = Int32.Parse(hdfUnitId.Value.Trim());

                    // Load
                    UnitInformationTDS dataSet = new UnitInformationTDS();
                    dataSet.ChecklistDetails.Merge(unitsChecklistRulesEdit, true);
                    UnitInformationChecklistDetails model = new UnitInformationChecklistDetails(dataSet);

                    model.LoadByUnitIdWithoutInProgressRules(unitId, int.Parse(hdfCompanyId.Value));

                    // Store tables
                    unitsChecklistRulesEdit = (UnitInformationTDS.ChecklistDetailsDataTable)model.Table;
                    Session["unitsChecklistRulesEdit"] = unitsChecklistRulesEdit;

                    grdChecklistRulesInformation.DataBind();
                }
            }
            else
            {
                // Restore datasets
                unitInformationTDS = (UnitInformationTDS)Session["unitInformationTDS"];
                unitsChecklistRulesEdit = (UnitInformationTDS.ChecklistDetailsDataTable)Session["unitsChecklistRulesEdit"];
            }
        }
        private void LoadChecklitData(int unitId)
        {
            // Load
            UnitInformationTDS dataSet = new UnitInformationTDS();
            dataSet.ChecklistDetails.Merge(unitsChecklistRulesTemp, true);
            UnitInformationChecklistDetails model = new UnitInformationChecklistDetails(dataSet);

            model.Load(unitId, int.Parse(hdfCompanyId.Value));

            // Store tables
            unitsChecklistRulesTemp = (UnitInformationTDS.ChecklistDetailsDataTable)model.Table;
            Session["unitsChecklistRulesTemp"] = unitsChecklistRulesTemp;

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

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_UNITS_VIEW"]) && Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_UNITS_DELETE"])))
                {
                    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["unit_id"] == null) || ((string)Request.QueryString["unit_type"] == null) || ((string)Request.QueryString["unit_state"] == null) || ((string)Request.QueryString["active_tab"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in units_delete.aspx");
                }

                // Tag Page
                hdfUnitId.Value = Request.QueryString["unit_id"].ToString();
                hdfUnitType.Value = Request.QueryString["unit_type"].ToString();
                hdfUnitState.Value = Request.QueryString["unit_state"].ToString();
                hdfCompanyId.Value = Session["companyID"].ToString();

                int companyId = Int32.Parse(hdfCompanyId.Value.Trim());
                int unitId = Int32.Parse(hdfUnitId.Value.Trim());

                // If comming from
                // ... units_navigator2.aspx
                if (Request.QueryString["source_page"] == "units_navigator2.aspx")
                {
                    StoreNavigatorState();
                    ViewState["update"] = "no";

                    unitInformationTDS = new UnitInformationTDS();

                    UnitInformationUnitDetails unitInformationUnitDetails = new UnitInformationUnitDetails(unitInformationTDS);
                    unitInformationUnitDetails.LoadByUnitId(unitId, companyId);

                    UnitInformationChecklistDetails unitInformationChecklistDetails = new UnitInformationChecklistDetails(unitInformationTDS);
                    unitInformationChecklistDetails.Load(unitId, companyId);

                    UnitInformationServiceDetails unitInformationServiceDetails = new UnitInformationServiceDetails(unitInformationTDS);
                    unitInformationServiceDetails.Load(unitId, companyId);

                    UnitInformationInspectionDetails unitInformationInspectionDetails = new UnitInformationInspectionDetails(unitInformationTDS);
                    unitInformationInspectionDetails.LoadByUnitId(unitId, companyId);

                    UnitInformationCostExceptionsInformation unitInformationCostExceptionsInformation = new UnitInformationCostExceptionsInformation(unitInformationTDS);
                    unitInformationCostExceptionsInformation.LoadAllByUnitId(unitId, companyId);

                    UnitInformationCostInformation unitInformationCostInformation = new UnitInformationCostInformation(unitInformationTDS);
                    unitInformationCostInformation.LoadAllByUnitId(unitId, companyId);

                    // Store dataset
                    Session["unitInformationTDS"] = unitInformationTDS;
                }

                // ... units_summary.aspx
                if (Request.QueryString["source_page"] == "units_summary.aspx")
                {
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];

                    // Restore datasets
                    unitInformationTDS = (UnitInformationTDS)Session["unitInformationTDS"];
                }
            }
            else
            {
                // Restore datasets
                unitInformationTDS = (UnitInformationTDS)Session["unitInformationTDS"];
            }
        }
        private void Delete()
        {
            Page.Validate();

            if (Page.IsValid)
            {
                // Delete
                int unitId = Int32.Parse(hdfUnitId.Value);
                int companyId = Int32.Parse(hdfCompanyId.Value);

                UnitInformationUnitDetails unitInformationUnitDetails = new UnitInformationUnitDetails(unitInformationTDS);
                unitInformationUnitDetails.Delete(unitId);

                UnitInformationChecklistDetails unitInformationChecklistDetails = new UnitInformationChecklistDetails(unitInformationTDS);
                unitInformationChecklistDetails.DeleteAll();

                UnitInformationInspectionDetails unitInformationInspectionDetails = new UnitInformationInspectionDetails(unitInformationTDS);
                unitInformationInspectionDetails.DeleteAll();

                UnitInformationCostExceptionsInformation unitInformationCostExceptionsInformation = new UnitInformationCostExceptionsInformation(unitInformationTDS);
                unitInformationCostExceptionsInformation.DeleteAll(unitId);

                UnitInformationCostInformation unitInformationCostInformation = new UnitInformationCostInformation(unitInformationTDS);
                unitInformationCostInformation.DeleteAll(unitId);

                // Update databse
                UpdateDatabase();

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

                // Redirect
                string url = "";
                url = "./units_navigator2.aspx?source_page=units_delete.aspx" + GetNavigatorState() + "&update=yes";
                Response.Redirect(url);
            }
        }