private void Page_Load(object sender, System.EventArgs e)
        {
            string sBack;
            bool   bBack = false;

            try
            {
                OrgId = _functions.GetUserOrgId(HttpContext.Current.User.Identity.Name, false);

                if (Request.QueryString["id"] == null)
                {
                    Session["lastpage"] = "wo_default.aspx";
                    Session["error"]    = _functions.ErrorMessage(104);
                    Response.Redirect("error.aspx", false);
                    return;
                }
                try
                {
                    OrderId = Convert.ToInt32(Request.QueryString["id"]);
                }
                catch (FormatException fex)
                {
                    Session["lastpage"] = "wo_default.aspx";
                    Session["error"]    = _functions.ErrorMessage(105);
                    Response.Redirect("error.aspx", false);
                    return;
                }

                if (Request.QueryString["back"] != null)
                {
                    // see whence came user
                    switch (Request.QueryString["back"])
                    {
                    case "view":                             // from the work order detail screen
                        m_sCurrentUrl  = "wo_addPreventiveMaintenance.aspx?id=" + OrderId.ToString() + "&back=view";
                        m_sMainBackUrl = "wo_viewWorkOrder.aspx?id=" + OrderId.ToString();
                        m_sNextUrl     = m_sMainBackUrl;

                        tblMain.Rows[4].Visible = false;
                        tblMain.Rows[5].Visible = true;

                        // getting the Bread Crumbs from Cookie and add last item
                        if (Request.Cookies.Count > 0 && Request.Cookies["bfp_navigate"] != null)
                        {
                            Header.BrdCrumbsSerialization = Request.Cookies["bfp_navigate"].Value;
                        }
                        Header.AddBreadCrumb("Work Order Detail", "/wo_viewWorkOrder.aspx?id=" + OrderId.ToString() + "&#Issues");

                        SaveCancelControl.ParentPageURL = ParentPageURL;
                        SaveCancelControl.ButtonText    = " Save ";

                        bBack = false;
                        break;

                    case "inspect":                             // from the Add Inpection screen
                        m_sCurrentUrl  = "wo_addPreventiveMaintenance.aspx?id=" + OrderId.ToString() + "&back=inspect";
                        m_sMainBackUrl = "wo_addInspections.aspx?id=" + OrderId.ToString();
                        m_sNextUrl     = "wo_addRepairs.aspx?id=" + OrderId.ToString() + "&back=open";

                        bBack = true;
                        break;

                    case "preview":                             // from the Preview screen
                        m_sCurrentUrl  = "wo_addPreventiveMaintenance.aspx?id=" + OrderId.ToString() + "&back=preview";
                        m_sMainBackUrl = "wo_preview.aspx?id=" + OrderId.ToString();
                        m_sNextUrl     = "wo_preview.aspx?id=" + OrderId.ToString();

                        bBack = true;
                        break;

                    default:                             // from the Add Issues screen
                        m_sCurrentUrl  = "wo_addPreventiveMaintenance.aspx?id=" + OrderId.ToString();
                        m_sMainBackUrl = "wo_addRepairs.aspx?id=" + OrderId.ToString() + "&back=open";
                        m_sNextUrl     = "wo_addInspections.aspx?id=" + OrderId.ToString();

                        bBack = true;
                        break;
                    }
                }
                else
                {
                    m_sCurrentUrl  = "wo_addPreventiveMaintenance.aspx?id=" + OrderId.ToString();
                    m_sMainBackUrl = "wo_addRepairs.aspx?id=" + OrderId.ToString() + "&back=open";
                    m_sNextUrl     = "wo_addInspections.aspx?id=" + OrderId.ToString();
                    bBack          = true;
                }
                // if previos screen was from wizard screen then
                if (bBack)
                {
                    tblMain.Rows[4].Visible = true;
                    tblMain.Rows[5].Visible = false;

//					Header.PageTitle = PageTitle;

                    NextBackControl.BackPage = "wo_addRepairs.aspx?id=" + OrderId.ToString() + "&back=open";
                }

                // reloading if coming from the PM Item History screen
                if (Session["reload"] != null)
                {
                    if ((bool)Session["reload"] == true)
                    {
                        Session["reload"] = null;
                        Response.Redirect(m_sCurrentUrl, false);
                        return;
                    }
                }

                if (!IsPostBack)
                {
                    order        = new clsWorkOrders();
                    order.iOrgId = OrgId;
                    order.iId    = OrderId;
                    dtPMServices = order.GetPMServicesListForWorkOrder();
                    if (dtPMServices.Rows.Count > 0)
                    {
                        lblCurrentUnits.Text = "The current units for this Equipment is <b>" +
                                               (order.dmMileage.IsNull?"Unknown":Convert.ToDouble(order.dmMileage.Value).ToString()) +
                                               "</b>, date is <b>" + (order.daReportDate.IsNull?DateTime.Now.ToString("MM/dd/yyyy"):order.daReportDate.Value.ToString("MM/dd/yyyy")) + "</b>";
                        dgPMServices.DataSource = new DataView(dtPMServices);
                        dgPMServices.DataBind();

                        dtFuturePMService = order.GetFuturePMItems();
                        if (dtFuturePMService.Rows.Count > 0)
                        {
                            dgFuturePMItems.DataSource = new DataView(dtFuturePMService);
                            dgFuturePMItems.DataBind();
                        }
                        else
                        {
                            tblMain.Rows[2].Visible = false;
                            tblMain.Rows[3].Visible = false;
                        }
                    }
                    else
                    {
                        Response.Redirect(m_sNextUrl, false);
                    }
                }
            }
            catch (Exception ex)
            {
                _functions.Log(ex, HttpContext.Current.User.Identity.Name, SourcePageName);
                Session["lastpage"]     = m_sMainBackUrl;
                Session["error"]        = ex.Message;
                Session["error_report"] = ex.ToString();
                Response.Redirect("error.aspx", false);
            }
            finally
            {
                if (order != null)
                {
                    order.Dispose();
                }
            }
        }
        private void dgFuturePMItems_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            try
            {
                int FutureOrderId;
                if (e.CommandName == "Assign")
                {
                    order           = new clsWorkOrders();
                    order.cAction   = "S";
                    order.iOrgId    = OrgId;
                    order.iPMItemId = Convert.ToInt32(e.Item.Cells[0].Text);
                    // getting the undone repair information
                    if (order.WorkOrderPMItemsDetail() == -1)
                    {
                        Session["lastpage"] = m_sCurrentUrl;
                        Session["error"]    = _functions.ErrorMessage(123);
                        Response.Redirect("error.aspx", false);
                    }
                    FutureOrderId = order.iId.Value;
                    // updating the old undone repair
                    order.cAction          = "U";
                    order.iId              = OrderId;
                    order.iServiceResultId = SqlInt32.Null;
                    order.iServiceCheckId  = SqlInt32.Null;
                    order.iRepairMultId    = SqlInt32.Null;
                    if (order.WorkOrderPMItemsDetail() == -1)
                    {
                        Session["lastpage"] = m_sCurrentUrl;
                        Session["error"]    = _functions.ErrorMessage(123);
                        Response.Redirect("error.aspx", false);
                    }
                    // check future work order
                    // if it is empty then to delete it
                    order.iId = FutureOrderId;
                    if (order.DeleteFutureWorkOrder() == -1)
                    {
                        Session["lastpage"] = m_sCurrentUrl;
                        Session["error"]    = _functions.ErrorMessage(120);
                        Response.Redirect("error.aspx", false);
                    }

                    // show updated PM Items
                    order.iId               = OrderId;
                    dtPMServices            = order.GetPMServicesListForWorkOrder();
                    dgPMServices.DataSource = new DataView(dtPMServices);
                    dgPMServices.DataBind();

                    dtFuturePMService = order.GetFuturePMItems();
                    if (dtFuturePMService.Rows.Count > 0)
                    {
                        dgFuturePMItems.DataSource = new DataView(dtFuturePMService);
                        dgFuturePMItems.DataBind();
                    }
                    else
                    {
                        tblMain.Rows[2].Visible = false;
                        tblMain.Rows[3].Visible = false;
                    }
                }
            }
            catch (Exception ex)
            {
                _functions.Log(ex, HttpContext.Current.User.Identity.Name, SourcePageName);
                Session["lastpage"]     = m_sCurrentUrl;
                Session["error"]        = ex.Message;
                Session["error_report"] = ex.ToString();
                Response.Redirect("error.aspx", false);
            }
            finally
            {
                if (order != null)
                {
                    order.Dispose();
                }
            }
        }