protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                //code for security privilage
                Session.Remove("WRITEFACILITY");

                SMHR_LOGININFO _obj_Smhr_LoginInfo = new SMHR_LOGININFO();

                _obj_Smhr_LoginInfo.OPERATION       = operation.Empty1;
                _obj_Smhr_LoginInfo.LOGIN_USERNAME  = Convert.ToString(Session["USERNAME"]).Trim();
                _obj_Smhr_LoginInfo.ORGANISATION_ID = Convert.ToInt32(Session["ORG_ID"]);
                _obj_Smhr_LoginInfo.LOGIN_PASS_CODE = Convert.ToString("Incidents Master");
                DataTable dtformdtls = BLL.get_LoginInfo(_obj_Smhr_LoginInfo);
                if (dtformdtls.Rows.Count != 0)
                {
                    if ((Convert.ToBoolean(dtformdtls.Rows[0]["TYPSEC_READ"]) == true) && (Convert.ToBoolean(dtformdtls.Rows[0]["TYPSEC_WRITE"]) == true))
                    {
                        Session["WRITEFACILITY"] = 1;//WHICH MEANS READ AND WRITE
                    }
                    else if ((Convert.ToBoolean(dtformdtls.Rows[0]["TYPSEC_READ"]) == true) && (Convert.ToBoolean(dtformdtls.Rows[0]["TYPSEC_WRITE"]) == false))
                    {
                        Session["WRITEFACILITY"] = 2;//WHICH MEANS READ NO WRITE
                    }
                    else if ((Convert.ToBoolean(dtformdtls.Rows[0]["TYPSEC_READ"]) == false) && (Convert.ToBoolean(dtformdtls.Rows[0]["TYPSEC_WRITE"]) == false))
                    {
                        Session["WRITEFACILITY"] = 3;//WHICH MEANS NO READ AND NO WRITE
                    }
                }
                else
                {
                    smhr_UNAUTHORIZED _obj_smhr_unauthorized = new smhr_UNAUTHORIZED();
                    _obj_smhr_unauthorized.UNAUTHORIZED_USERID     = Convert.ToInt32(Session["USER_ID"]);
                    _obj_smhr_unauthorized.UNAUTHORIZED_FORMID     = Convert.ToInt32(ViewState["FORMS_ID"]);
                    _obj_smhr_unauthorized.UNAUTHORIZED_MODULEID   = Convert.ToInt32(ViewState["MODULE_ID"]);
                    _obj_smhr_unauthorized.UNAUTHORIZED_ACCESSDATE = Convert.ToDateTime(DateTime.Now.ToString());
                    SMHR.BLL.UnAuthorized_Log(_obj_smhr_unauthorized);
                    Response.Redirect("~/frm_UnAuthorized.aspx", false);
                    return;
                }


                if (Convert.ToInt32(Session["WRITEFACILITY"]) == 2)
                {
                    RG_IncidentMaster.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.None;
                    btnSave.Visible = false;
                    //btn_Update.Visible = false;
                }
                else if (Convert.ToInt32(Session["WRITEFACILITY"]) == 3)
                {
                    smhr_UNAUTHORIZED _obj_smhr_unauthorized = new smhr_UNAUTHORIZED();
                    _obj_smhr_unauthorized.UNAUTHORIZED_USERID     = Convert.ToInt32(Session["USER_ID"]);
                    _obj_smhr_unauthorized.UNAUTHORIZED_FORMID     = Convert.ToInt32(ViewState["FORMS_ID"]);
                    _obj_smhr_unauthorized.UNAUTHORIZED_MODULEID   = Convert.ToInt32(ViewState["MODULE_ID"]);
                    _obj_smhr_unauthorized.UNAUTHORIZED_ACCESSDATE = Convert.ToDateTime(DateTime.Now.ToString());
                    SMHR.BLL.UnAuthorized_Log(_obj_smhr_unauthorized);
                    Response.Redirect("~/frm_UnAuthorized.aspx", false);
                }
                LoadGrid();
                RG_IncidentMaster.DataBind();
                RD_dtIncidentDtTime.MaxDate = DateTime.Now;
            }
        }
        catch (Exception ex)
        {
            SMHR.BLL.Error_Log(Session["USER_ID"].ToString(), ex.TargetSite.ToString(), ex.Message.Replace("'", "''"), "frm_IncidentMaster", ex.StackTrace, DateTime.Now);
            Response.Redirect("~/Frm_ErrorPage.aspx");
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        SMHR_WorkmanCompensation objWrkComp = new SMHR_WorkmanCompensation();

        try
        {
            switch (((Button)sender).ID.ToUpper())
            {
            case "BTNEDIT":     //To update data
                if (RD_dtIncidentDtTime.SelectedDate == null)
                {
                    BLL.ShowMessage(this, "Please Select Date & Time");
                    return;
                }
                objWrkComp.IncidentID       = Convert.ToInt32(lblIncidentID.Text);
                objWrkComp.IncidentName     = BLL.ReplaceQuote(RD_txtIncidentName.Text);
                objWrkComp.IncidentPlace    = BLL.ReplaceQuote(RD_txtPlaceOfIncident.Text);
                objWrkComp.IncidentDatetime = Convert.ToDateTime(RD_dtIncidentDtTime.SelectedDate.Value);
                objWrkComp.ORGANISATION_ID  = Convert.ToInt32(Session["ORG_ID"].ToString());
                //objWrkComp.BUID = BUFilter1.BusinessUnitID;
                //objWrkComp.DIRECTORATE_ID = BUFilter1.DirectorateID;
                //objWrkComp.DEPARTMENT_ID = BUFilter1.DepartmentID;
                //objWrkComp.SUBDEPARTMENT_ID = 0;

                //To check if Incident Name already exits
                objWrkComp.OPERATION = operation.Check;
                if (BLL.InsertUpdateIncidents(objWrkComp))
                {
                    BLL.ShowMessage(this, "Incident Name already exits");
                    return;
                }

                objWrkComp.OPERATION = operation.Update;
                if (BLL.InsertUpdateIncidents(objWrkComp))
                {
                    BLL.ShowMessage(this, "Information Updated Successfully");
                }
                else
                {
                    BLL.ShowMessage(this, "Information Not Updated");
                }

                rm_MR_Page.SelectedIndex = 0;
                LoadGrid();
                RG_IncidentMaster.DataBind();
                break;

            case "BTNSAVE":                        //To save data
                if (BUFilter1.BusinessUnitID == 0) //To check if BusinessUnit in BUFilter is selected
                {
                    BLL.ShowMessage(this, "Please select Business Unit");
                    return;
                }
                else if (RD_dtIncidentDtTime.SelectedDate == null)
                {
                    BLL.ShowMessage(this, "Please Select Date & Time");
                    return;
                }
                //SMHR_WorkmanCompensation objWrkComp = new SMHR_WorkmanCompensation();
                objWrkComp.IncidentName     = BLL.ReplaceQuote(RD_txtIncidentName.Text);
                objWrkComp.IncidentPlace    = BLL.ReplaceQuote(RD_txtPlaceOfIncident.Text.Trim());
                objWrkComp.IncidentDatetime = Convert.ToDateTime(RD_dtIncidentDtTime.SelectedDate.Value);
                objWrkComp.ORGANISATION_ID  = Convert.ToInt32(Session["ORG_ID"].ToString());
                objWrkComp.BUID             = BUFilter1.BusinessUnitID;
                objWrkComp.DIRECTORATE_ID   = BUFilter1.DirectorateID;
                objWrkComp.DEPARTMENT_ID    = BUFilter1.DepartmentID;
                objWrkComp.SUBDEPARTMENT_ID = 0;        //Add the sub-department

                //To check if Incident Name already exits
                objWrkComp.OPERATION = operation.Check;
                if (BLL.InsertUpdateIncidents(objWrkComp))
                {
                    BLL.ShowMessage(this, "Incident Name already exits");
                    return;
                }

                objWrkComp.OPERATION = operation.Insert;
                if (BLL.InsertUpdateIncidents(objWrkComp))
                {
                    BLL.ShowMessage(this, "Information Saved Successfully");
                }
                else
                {
                    BLL.ShowMessage(this, "Information Not Saved");
                }

                rm_MR_Page.SelectedIndex = 0;
                LoadGrid();
                RG_IncidentMaster.DataBind();
                break;
            }
        }
        catch (Exception ex)
        {
            SMHR.BLL.Error_Log(Session["USER_ID"].ToString(), ex.TargetSite.ToString(), ex.Message.Replace("'", "''"), "frm_IncidentMaster", ex.StackTrace, DateTime.Now);
            Response.Redirect("~/Frm_ErrorPage.aspx");
        }
    }