public void SaveRatings()
    {
        try
        {
            bool indefect = false;
            for (int i = 0; i < grdCatRating.Rows.Count; i++)
            {
                string SystemCode        = ((HiddenField)grdCatRating.Rows[i].FindControl("hdnCatCode")).Value;
                string SystemDescription = grdCatRating.Rows[i].Cells[2].Text;
                string SysLastReport     = grdCatRating.Rows[i].Cells[3].Text;
                if (SysLastReport == "&nbsp;")
                {
                    SysLastReport = "";
                }
                string SysCurrentReport = grdCatRating.Rows[i].Cells[4].Text;
                if (SysCurrentReport == "&nbsp;")
                {
                    SysCurrentReport = "";
                }
                string SysRating    = "";
                string ScheduleID   = "";
                string InspectionID = ViewState["InspectionID"].ToString();

                string CreatedBy    = "1";
                string ActiveStatus = "1";

                objInsp.InsertCategoryRating(SystemCode, SysLastReport, SysCurrentReport, SysRating, ScheduleID, InspectionID, CreatedBy, DateTime.Now, ActiveStatus);
            }

            for (int i = 0; i < grdSubCatRating.Rows.Count; i++)
            {
                string SystemCode           = ViewState["CategoryCode"].ToString();
                string SubSystemCode        = ((HiddenField)grdSubCatRating.Rows[i].FindControl("hdnSubCatCode")).Value;
                string SubSystemDescription = ((Label)grdSubCatRating.Rows[i].FindControl("lblSubCatDesc")).Text;
                string SubSysSecLastReport  = grdSubCatRating.Rows[i].Cells[3].Text;
                if (SubSysSecLastReport == "&nbsp;")
                {
                    SubSysSecLastReport = "";
                }
                string SubSysLastReport = grdSubCatRating.Rows[i].Cells[4].Text;
                if (SubSysLastReport == "&nbsp;")
                {
                    SubSysLastReport = "";
                }
                string SubSysCurrentReport = ((DropDownList)grdSubCatRating.Rows[i].FindControl("ddlRating")).SelectedItem.Text;
                if (SubSysCurrentReport == "--SELECT--")
                {
                    SubSysCurrentReport = "";
                }
                string SubSysRating      = ((DropDownList)grdSubCatRating.Rows[i].FindControl("ddlRating")).SelectedValue.ToString().Split('_')[0];
                string ScheduleID        = "";
                string AdditionalRemarks = txtAddRemarks.Text;
                string InspectionID      = ViewState["InspectionID"].ToString();
                string Remarks           = ((TextBox)grdSubCatRating.Rows[i].FindControl("txtRemarks")).Text;
                string CreatedBy         = "1";
                string ActiveStatus      = "1";


                int inDefect = objInsp.INSP_Get_WorklistReportByLocationID(Convert.ToInt32(ViewState["InspectionID"].ToString()), Convert.ToInt32(SubSystemCode));
                if (inDefect == 1)
                {
                    int Rating = objInsp.INSP_Get_RatingsByRating("Fair");
                    if (SubSysCurrentReport != "")
                    {
                        if (Convert.ToInt32(SubSysCurrentReport) >= Rating)
                        {
                            string js1 = "alert('Item has defect can not be rated Fair and Above'); ";
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "alert1", js1, true);


                            BindSubCategoryRating(ViewState["CategoryCode"].ToString(), ViewState["InspectionID"].ToString());
                            ((TextBox)grdSubCatRating.Rows[i].FindControl("txtRemarks")).Text          = Remarks;
                            ((Label)grdSubCatRating.HeaderRow.Cells[0].FindControl("lblCatNo")).Text   = hdnCurrentCatNo.Value;   //((HiddenField)grdCatRating.Rows[rowindex].FindControl("hdnCatRNo")).Value;
                            ((Label)grdSubCatRating.HeaderRow.Cells[1].FindControl("lblCatDesc")).Text = hdnCurrentCatDesc.Value; //((HiddenField)grdCatRating.Rows[rowindex].FindControl("hdnCatDesc")).Value;

                            ((DropDownList)grdSubCatRating.Rows[i].FindControl("ddlRating")).Focus();


                            indefect = true;
                        }
                        else
                        {
                            objInsp.InsertSubCategoryRating(SystemCode, SubSystemCode, SubSysSecLastReport, SubSysLastReport, SubSysCurrentReport, SubSysRating, Remarks, AdditionalRemarks, ScheduleID, InspectionID, CreatedBy, DateTime.Now, ActiveStatus);
                        }
                    }
                    else
                    {
                        objInsp.InsertSubCategoryRating(SystemCode, SubSystemCode, SubSysSecLastReport, SubSysLastReport, SubSysCurrentReport, SubSysRating, Remarks, AdditionalRemarks, ScheduleID, InspectionID, CreatedBy, DateTime.Now, ActiveStatus);
                    }
                }
                else
                {
                    objInsp.InsertSubCategoryRating(SystemCode, SubSystemCode, SubSysSecLastReport, SubSysLastReport, SubSysCurrentReport, SubSysRating, Remarks, AdditionalRemarks, ScheduleID, InspectionID, CreatedBy, DateTime.Now, ActiveStatus);
                }
            }

            if (indefect == false)
            {
                //pnlAddRemark.Visible = false;

                // grdSubCatRating.Visible = false;

                BindCategoryRating(ViewState["InspectionID"].ToString(), ViewState["CheckListIDs"].ToString());
                BindSubCategoryRating(ViewState["CategoryCode"].ToString(), ViewState["InspectionID"].ToString());


                updCat.Update();
                hdnDirtyCounter.Value = "0";
                //string js2 = "alert('Rating Saved Successfully');";
                //ScriptManager.RegisterStartupScript(this, this.GetType(), "alert9", js2, true);
            }

            int rowindex = UDFLib.ConvertToInteger(ViewState["SelectedRowIndex"].ToString());
            grdCatRating.SelectRow(rowindex);


            if (grdSubCatRating.Rows.Count > 0)
            {
                pnlAddRemark.Visible    = true;
                pnlSubcategory.Visible  = true;
                grdSubCatRating.Visible = true;
                txtAddRemarks.Visible   = true;
                //grdSubCatRating.Style.Add("display", "inline");
                ((Label)grdSubCatRating.HeaderRow.Cells[0].FindControl("lblCatNo")).Text   = ((HiddenField)grdCatRating.Rows[rowindex].FindControl("hdnCatRNo")).Value;
                ((Label)grdSubCatRating.HeaderRow.Cells[1].FindControl("lblCatDesc")).Text = ((HiddenField)grdCatRating.Rows[rowindex].FindControl("hdnCatDesc")).Value;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }