protected void DvItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            string returnURL = "~/ControlRoom/Modules/Reports/ReportList.aspx";
            if (e.CommandName.ToLower() == "back")
            {
                Response.Redirect(returnURL);
            }
            if (e.CommandName.ToLower() == "refresh")
            {
                try
                {
                    odsData.DataBind();
                    dv.DataBind();
                    dv.ChangeMode(DetailsViewMode.Edit);

                    var masterPage = (IControlRoomMaster)Master;
                    if (masterPage != null) masterPage.PageMessage = SRPResources.RefreshOK;
                }
                catch (Exception ex)
                {
                    var masterPage = (IControlRoomMaster)Master;
                    masterPage.PageError = String.Format(SRPResources.ApplicationError1, ex.Message);
                }
            }
            #region Save report 
            if (e.CommandName.ToLower() == "savereport")
            {
                if (lblPK.Text == "")
                {
                    try
                    {
                        //ADD 
                        var obj = new SRPReport();

                        obj.RTID =
                            FormatHelper.SafeToInt(
                                ((DropDownList)((DetailsView)sender).FindControl("RTID")).SelectedValue);
                        obj.ProgId =
                            FormatHelper.SafeToInt(
                                ((DropDownList)((DetailsView)sender).FindControl("ProgId")).SelectedValue);
                        obj.ReportName = ((TextBox)((DetailsView)sender).FindControl("ReportName")).Text;
                        obj.DisplayFilters = ((CheckBox)((DetailsView)sender).FindControl("DisplayFilters")).Checked;
                        obj.ReportFormat =
                            FormatHelper.SafeToInt(
                                ((DropDownList)((DetailsView)sender).FindControl("ReportFormat")).SelectedValue);

                        obj.DOBFrom =
                            FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("DOBFrom")).Text);
                        obj.DOBTo =
                            FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("DOBTo")).Text);
                        obj.AgeFrom =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("AgeFrom")).Text);
                        obj.AgeTo = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("AgeTo")).Text);
                        obj.SchoolGrade = ((TextBox)((DetailsView)sender).FindControl("SchoolGrade")).Text;
                        obj.FirstName = ((TextBox)((DetailsView)sender).FindControl("FirstName")).Text;
                        obj.LastName = ((TextBox)((DetailsView)sender).FindControl("LastName")).Text;
                        obj.Gender = ((DropDownList)((DetailsView)sender).FindControl("Gender")).SelectedValue;
                        obj.EmailAddress = ((TextBox)((DetailsView)sender).FindControl("EmailAddress")).Text;
                        obj.PhoneNumber = ((TextBox)((DetailsView)sender).FindControl("PhoneNumber")).Text;
                        obj.City = ((TextBox)((DetailsView)sender).FindControl("City")).Text;
                        obj.State = ((TextBox)((DetailsView)sender).FindControl("State")).Text;
                        obj.ZipCode = ((TextBox)((DetailsView)sender).FindControl("ZipCode")).Text;
                        obj.County = ((TextBox)((DetailsView)sender).FindControl("County")).Text;
                        obj.PrimaryLibrary =
                            FormatHelper.SafeToInt(
                                ((DropDownList)((DetailsView)sender).FindControl("PrimaryLibrary")).SelectedValue);
                        obj.SchoolName = ((DropDownList)((DetailsView)sender).FindControl("SchoolName")).SelectedValue;
                        obj.District = ((DropDownList)((DetailsView)sender).FindControl("District")).SelectedValue;
                        obj.SDistrict = ((DropDownList)((DetailsView)sender).FindControl("SDistrict")).SelectedValue;

                        obj.Teacher = ((TextBox)((DetailsView)sender).FindControl("Teacher")).Text;
                        obj.GroupTeamName = ((TextBox)((DetailsView)sender).FindControl("GroupTeamName")).Text;
                        obj.SchoolType =
                            FormatHelper.SafeToInt(
                                ((DropDownList)((DetailsView)sender).FindControl("SchoolType")).SelectedValue);

                        obj.LiteracyLevel1 =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("LiteracyLevel1")).Text);
                        obj.LiteracyLevel2 =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("LiteracyLevel2")).Text);

                        obj.Custom1 = ((TextBox)((DetailsView)sender).FindControl("Custom1")).Text;
                        obj.Custom2 = ((TextBox)((DetailsView)sender).FindControl("Custom2")).Text;
                        obj.Custom3 = ((TextBox)((DetailsView)sender).FindControl("Custom3")).Text;
                        obj.Custom4 = ((TextBox)((DetailsView)sender).FindControl("Custom4")).Text;
                        obj.Custom5 = ((TextBox)((DetailsView)sender).FindControl("Custom5")).Text;

                        obj.RegistrationDateStart =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("RegistrationDateStart")).Text);
                        obj.RegistrationDateEnd =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("RegistrationDateEnd")).Text);

                        obj.PointsMin =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("PointsMin")).Text);
                        obj.PointsMax =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("PointsMax")).Text);
                        obj.PointsStart =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("PointsStart")).Text);
                        obj.PointsEnd =
                            FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("PointsEnd")).Text);
                        obj.EventCode = ((TextBox)((DetailsView)sender).FindControl("EventCode")).Text;
                        obj.EarnedBadge =
                            FormatHelper.SafeToInt(
                                ((DropDownList)((DetailsView)sender).FindControl("EarnedBadge")).SelectedValue);
                        obj.PhysicalPrizeEarned =
                            ((TextBox)((DetailsView)sender).FindControl("PhysicalPrizeEarned")).Text;
                        obj.PhysicalPrizeRedeemed =
                            ((CheckBox)((DetailsView)sender).FindControl("PhysicalPrizeRedeemed")).Checked;
                        obj.PhysicalPrizeStartDate =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("PhysicalPrizeStartDate")).Text);
                        obj.PhysicalPrizeEndDate =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("PhysicalPrizeEndDate")).Text);

                        obj.ReviewsMin =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("ReviewsMin")).Text);
                        obj.ReviewsMax =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("ReviewsMax")).Text);
                        obj.ReviewTitle = ((TextBox)((DetailsView)sender).FindControl("ReviewTitle")).Text;
                        obj.ReviewAuthor = ((TextBox)((DetailsView)sender).FindControl("ReviewAuthor")).Text;
                        obj.ReviewStartDate =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("ReviewStartDate")).Text);
                        obj.ReviewEndDate =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("ReviewEndDate")).Text);
                        obj.RandomDrawingName = ((TextBox)((DetailsView)sender).FindControl("RandomDrawingName")).Text;
                        obj.RandomDrawingNum =
                            FormatHelper.SafeToInt(
                                ((TextBox)((DetailsView)sender).FindControl("RandomDrawingNum")).Text);
                        obj.RandomDrawingStartDate =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("RandomDrawingStartDate")).Text);
                        obj.RandomDrawingEndDate =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("RandomDrawingEndDate")).Text);
                        obj.HasBeenDrawn = ((CheckBox)((DetailsView)sender).FindControl("HasBeenDrawn")).Checked;
                        obj.HasRedeemend = ((CheckBox)((DetailsView)sender).FindControl("HasRedeemend")).Checked;

                        obj.PIDInc = ((CheckBox)((DetailsView)sender).FindControl("PIDInc")).Checked;
                        obj.UsernameInc = ((CheckBox)((DetailsView)sender).FindControl("UsernameInc")).Checked;
                        obj.DOBInc = ((CheckBox)((DetailsView)sender).FindControl("DOBInc")).Checked;
                        obj.AgeInc = ((CheckBox)((DetailsView)sender).FindControl("AgeInc")).Checked;
                        obj.SchoolGradeInc = ((CheckBox)((DetailsView)sender).FindControl("SchoolGradeInc")).Checked;
                        obj.FirstNameInc = ((CheckBox)((DetailsView)sender).FindControl("FirstNameInc")).Checked;
                        obj.LastNameInc = ((CheckBox)((DetailsView)sender).FindControl("LastNameInc")).Checked;
                        obj.GenderInc = ((CheckBox)((DetailsView)sender).FindControl("GenderInc")).Checked;
                        obj.EmailAddressInc = ((CheckBox)((DetailsView)sender).FindControl("EmailAddressInc")).Checked;
                        obj.PhoneNumberInc = ((CheckBox)((DetailsView)sender).FindControl("PhoneNumberInc")).Checked;
                        obj.CityInc = ((CheckBox)((DetailsView)sender).FindControl("CityInc")).Checked;
                        obj.StateInc = ((CheckBox)((DetailsView)sender).FindControl("StateInc")).Checked;
                        obj.ZipCodeInc = ((CheckBox)((DetailsView)sender).FindControl("ZipCodeInc")).Checked;
                        obj.CountyInc = ((CheckBox)((DetailsView)sender).FindControl("CountyInc")).Checked;
                        obj.PrimaryLibraryInc =
                            ((CheckBox)((DetailsView)sender).FindControl("PrimaryLibraryInc")).Checked;
                        obj.SchoolNameInc = ((CheckBox)((DetailsView)sender).FindControl("SchoolNameInc")).Checked;
                        obj.DistrictInc = ((CheckBox)((DetailsView)sender).FindControl("DistrictInc")).Checked;
                        obj.TeacherInc = ((CheckBox)((DetailsView)sender).FindControl("TeacherInc")).Checked;
                        obj.GroupTeamNameInc =
                            ((CheckBox)((DetailsView)sender).FindControl("GroupTeamNameInc")).Checked;
                        obj.SchoolTypeInc = ((CheckBox)((DetailsView)sender).FindControl("SchoolTypeInc")).Checked;
                        obj.LiteracyLevel1Inc =
                            ((CheckBox)((DetailsView)sender).FindControl("LiteracyLevel1Inc")).Checked;
                        obj.LiteracyLevel2Inc =
                            ((CheckBox)((DetailsView)sender).FindControl("LiteracyLevel2Inc")).Checked;
                        obj.Custom1Inc = ((CheckBox)((DetailsView)sender).FindControl("Custom1Inc")).Checked;
                        obj.Custom2Inc = ((CheckBox)((DetailsView)sender).FindControl("Custom2Inc")).Checked;
                        obj.Custom3Inc = ((CheckBox)((DetailsView)sender).FindControl("Custom3Inc")).Checked;
                        obj.Custom4Inc = ((CheckBox)((DetailsView)sender).FindControl("Custom4Inc")).Checked;
                        obj.Custom5Inc = ((CheckBox)((DetailsView)sender).FindControl("Custom5Inc")).Checked;
                        obj.RegistrationDateInc =
                            ((CheckBox)((DetailsView)sender).FindControl("RegistrationDateInc")).Checked;
                        obj.PointsInc = ((CheckBox)((DetailsView)sender).FindControl("PointsInc")).Checked;
                        obj.EarnedBadgeInc = ((CheckBox)((DetailsView)sender).FindControl("EarnedBadgeInc")).Checked;
                        obj.PhysicalPrizeNameInc =
                            ((CheckBox)((DetailsView)sender).FindControl("PhysicalPrizeNameInc")).Checked;
                        obj.PhysicalPrizeDateInc =
                            ((CheckBox)((DetailsView)sender).FindControl("PhysicalPrizeDateInc")).Checked;
                        obj.NumReviewsInc = ((CheckBox)((DetailsView)sender).FindControl("NumReviewsInc")).Checked;
                        obj.ReviewAuthorInc = ((CheckBox)((DetailsView)sender).FindControl("ReviewAuthorInc")).Checked;
                        obj.ReviewTitleInc = ((CheckBox)((DetailsView)sender).FindControl("ReviewTitleInc")).Checked;
                        obj.ReviewDateInc = ((CheckBox)((DetailsView)sender).FindControl("ReviewDateInc")).Checked;
                        obj.RandomDrawingNameInc =
                            ((CheckBox)((DetailsView)sender).FindControl("RandomDrawingNameInc")).Checked;
                        obj.RandomDrawingNumInc =
                            ((CheckBox)((DetailsView)sender).FindControl("RandomDrawingNumInc")).Checked;
                        obj.RandomDrawingDateInc =
                            ((CheckBox)((DetailsView)sender).FindControl("RandomDrawingDateInc")).Checked;
                        obj.HasBeenDrawnInc = ((CheckBox)((DetailsView)sender).FindControl("HasBeenDrawnInc")).Checked;
                        obj.HasRedeemendInc = ((CheckBox)((DetailsView)sender).FindControl("HasRedeemendInc")).Checked;


                        obj.Score1From =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score1From")).Text);
                        obj.Score1To =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score1To")).Text);
                        obj.Score2From =
                            FormatHelper.SafeToInt(
                                ((TextBox)((DetailsView)sender).FindControl("Score2From")).Text);
                        obj.Score2To =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score2To")).Text);

                        obj.Score1PctFrom =
                                FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score1PctFrom")).Text);
                        obj.Score1PctTo =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score1PctTo")).Text);
                        obj.Score2PctFrom =
                            FormatHelper.SafeToInt(
                                ((TextBox)((DetailsView)sender).FindControl("Score2PctFrom")).Text);
                        obj.Score2PctTo =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score2PctTo")).Text);

                        obj.Score1Inc = ((CheckBox)((DetailsView)sender).FindControl("Score1Inc")).Checked;
                        obj.Score2Inc = ((CheckBox)((DetailsView)sender).FindControl("Score2Inc")).Checked;
                        obj.Score1PctInc = ((CheckBox)((DetailsView)sender).FindControl("Score1PctInc")).Checked;
                        obj.Score2PctInc = ((CheckBox)((DetailsView)sender).FindControl("Score2PctInc")).Checked;

                        obj.AddedDate = DateTime.Now;
                        obj.AddedUser = ((SRPUser)Session[SessionData.UserProfile.ToString()]).Username;
                        //"N/A";  // Get from session
                        obj.LastModDate = obj.AddedDate;
                        obj.LastModUser = obj.AddedUser;

                        if (obj.IsValid(BusinessRulesValidationMode.INSERT))
                        {
                            obj.Insert();

                            lblPK.Text = obj.RID.ToString();

                            odsData.DataBind();
                            dv.DataBind();
                            dv.ChangeMode(DetailsViewMode.Edit);

                            var masterPage = (IControlRoomMaster)Master;
                            masterPage.PageMessage = SRPResources.AddedOK;
                        }
                        else
                        {
                            var masterPage = (IControlRoomMaster)Master;
                            string message = String.Format(SRPResources.ApplicationError1, "<ul>");
                            foreach (BusinessRulesValidationMessage m in obj.ErrorCodes)
                            {
                                message = string.Format(String.Format("{0}<li>{{0}}</li>", message), m.ErrorMessage);
                            }
                            message = string.Format("{0}</ul>", message);
                            masterPage.PageError = message;
                        }
                    }
                    catch (Exception ex)
                    {
                        var masterPage = (IControlRoomMaster)Master;
                        masterPage.PageError = String.Format(SRPResources.ApplicationError1, ex.Message);
                    }
                }
                else
                {

                    try
                    {
                        var obj = new SRPReport();
                        int pk = int.Parse(lblPK.Text);
                        obj.Fetch(pk);

                        obj.RTID = FormatHelper.SafeToInt(((DropDownList)((DetailsView)sender).FindControl("RTID")).SelectedValue);
                        obj.ProgId = FormatHelper.SafeToInt(((DropDownList)((DetailsView)sender).FindControl("ProgId")).SelectedValue);
                        obj.ReportName = ((TextBox)((DetailsView)sender).FindControl("ReportName")).Text;
                        obj.DisplayFilters = ((CheckBox)((DetailsView)sender).FindControl("DisplayFilters")).Checked;
                        obj.ReportFormat = FormatHelper.SafeToInt(((DropDownList)((DetailsView)sender).FindControl("ReportFormat")).SelectedValue);
                        obj.DOBFrom =
                            FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("DOBFrom")).Text);
                        obj.DOBTo =
                            FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("DOBTo")).Text);
                        obj.AgeFrom =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("AgeFrom")).Text);
                        obj.AgeTo = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("AgeTo")).Text);
                        obj.SchoolGrade = ((TextBox)((DetailsView)sender).FindControl("SchoolGrade")).Text;
                        obj.FirstName = ((TextBox)((DetailsView)sender).FindControl("FirstName")).Text;
                        obj.LastName = ((TextBox)((DetailsView)sender).FindControl("LastName")).Text;
                        obj.Gender = ((DropDownList)((DetailsView)sender).FindControl("Gender")).SelectedValue;
                        obj.EmailAddress = ((TextBox)((DetailsView)sender).FindControl("EmailAddress")).Text;
                        obj.PhoneNumber = ((TextBox)((DetailsView)sender).FindControl("PhoneNumber")).Text;
                        obj.City = ((TextBox)((DetailsView)sender).FindControl("City")).Text;
                        obj.State = ((TextBox)((DetailsView)sender).FindControl("State")).Text;
                        obj.ZipCode = ((TextBox)((DetailsView)sender).FindControl("ZipCode")).Text;
                        obj.County = ((TextBox)((DetailsView)sender).FindControl("County")).Text;

                        obj.PrimaryLibrary = FormatHelper.SafeToInt(((DropDownList)((DetailsView)sender).FindControl("PrimaryLibrary")).SelectedValue);
                        obj.SchoolName = ((DropDownList)((DetailsView)sender).FindControl("SchoolName")).SelectedValue;
                        obj.District = ((DropDownList)((DetailsView)sender).FindControl("District")).SelectedValue;
                        obj.SDistrict = ((DropDownList)((DetailsView)sender).FindControl("SDistrict")).SelectedValue;

                        obj.Teacher = ((TextBox)((DetailsView)sender).FindControl("Teacher")).Text;
                        obj.GroupTeamName = ((TextBox)((DetailsView)sender).FindControl("GroupTeamName")).Text;
                        obj.SchoolType = FormatHelper.SafeToInt(((DropDownList)((DetailsView)sender).FindControl("SchoolType")).SelectedValue);
                        obj.LiteracyLevel1 =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("LiteracyLevel1")).Text);
                        obj.LiteracyLevel2 =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("LiteracyLevel2")).Text);
                        obj.Custom1 = ((TextBox)((DetailsView)sender).FindControl("Custom1")).Text;
                        obj.Custom2 = ((TextBox)((DetailsView)sender).FindControl("Custom2")).Text;
                        obj.Custom3 = ((TextBox)((DetailsView)sender).FindControl("Custom3")).Text;
                        obj.Custom4 = ((TextBox)((DetailsView)sender).FindControl("Custom4")).Text;
                        obj.Custom5 = ((TextBox)((DetailsView)sender).FindControl("Custom5")).Text;
                        obj.RegistrationDateStart =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("RegistrationDateStart")).Text);
                        obj.RegistrationDateEnd =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("RegistrationDateEnd")).Text);
                        obj.PointsMin =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("PointsMin")).Text);
                        obj.PointsMax =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("PointsMax")).Text);
                        obj.PointsStart =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("PointsStart")).Text);
                        obj.PointsEnd =
                            FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("PointsEnd")).Text);
                        obj.EventCode = ((TextBox)((DetailsView)sender).FindControl("EventCode")).Text;
                        obj.EarnedBadge = FormatHelper.SafeToInt(((DropDownList)((DetailsView)sender).FindControl("EarnedBadge")).SelectedValue);
                        obj.PhysicalPrizeEarned =
                            ((TextBox)((DetailsView)sender).FindControl("PhysicalPrizeEarned")).Text;
                        obj.PhysicalPrizeRedeemed =
                            ((CheckBox)((DetailsView)sender).FindControl("PhysicalPrizeRedeemed")).Checked;
                        obj.PhysicalPrizeStartDate =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("PhysicalPrizeStartDate")).Text);
                        obj.PhysicalPrizeEndDate =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("PhysicalPrizeEndDate")).Text);
                        obj.ReviewsMin =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("ReviewsMin")).Text);
                        obj.ReviewsMax =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("ReviewsMax")).Text);
                        obj.ReviewTitle = ((TextBox)((DetailsView)sender).FindControl("ReviewTitle")).Text;
                        obj.ReviewAuthor = ((TextBox)((DetailsView)sender).FindControl("ReviewAuthor")).Text;
                        obj.ReviewStartDate =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("ReviewStartDate")).Text);
                        obj.ReviewEndDate =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("ReviewEndDate")).Text);
                        obj.RandomDrawingName = ((TextBox)((DetailsView)sender).FindControl("RandomDrawingName")).Text;
                        obj.RandomDrawingNum =
                            FormatHelper.SafeToInt(
                                ((TextBox)((DetailsView)sender).FindControl("RandomDrawingNum")).Text);
                        obj.RandomDrawingStartDate =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("RandomDrawingStartDate")).Text);
                        obj.RandomDrawingEndDate =
                            FormatHelper.SafeToDateTime(
                                ((TextBox)((DetailsView)sender).FindControl("RandomDrawingEndDate")).Text);
                        obj.HasBeenDrawn = ((CheckBox)((DetailsView)sender).FindControl("HasBeenDrawn")).Checked;
                        obj.HasRedeemend = ((CheckBox)((DetailsView)sender).FindControl("HasRedeemend")).Checked;
                        obj.PIDInc = ((CheckBox)((DetailsView)sender).FindControl("PIDInc")).Checked;
                        obj.UsernameInc = ((CheckBox)((DetailsView)sender).FindControl("UsernameInc")).Checked;
                        obj.DOBInc = ((CheckBox)((DetailsView)sender).FindControl("DOBInc")).Checked;
                        obj.AgeInc = ((CheckBox)((DetailsView)sender).FindControl("AgeInc")).Checked;
                        obj.SchoolGradeInc = ((CheckBox)((DetailsView)sender).FindControl("SchoolGradeInc")).Checked;
                        obj.FirstNameInc = ((CheckBox)((DetailsView)sender).FindControl("FirstNameInc")).Checked;
                        obj.LastNameInc = ((CheckBox)((DetailsView)sender).FindControl("LastNameInc")).Checked;
                        obj.GenderInc = ((CheckBox)((DetailsView)sender).FindControl("GenderInc")).Checked;
                        obj.EmailAddressInc = ((CheckBox)((DetailsView)sender).FindControl("EmailAddressInc")).Checked;
                        obj.PhoneNumberInc = ((CheckBox)((DetailsView)sender).FindControl("PhoneNumberInc")).Checked;
                        obj.CityInc = ((CheckBox)((DetailsView)sender).FindControl("CityInc")).Checked;
                        obj.StateInc = ((CheckBox)((DetailsView)sender).FindControl("StateInc")).Checked;
                        obj.ZipCodeInc = ((CheckBox)((DetailsView)sender).FindControl("ZipCodeInc")).Checked;
                        obj.CountyInc = ((CheckBox)((DetailsView)sender).FindControl("CountyInc")).Checked;
                        obj.PrimaryLibraryInc =
                            ((CheckBox)((DetailsView)sender).FindControl("PrimaryLibraryInc")).Checked;
                        obj.SchoolNameInc = ((CheckBox)((DetailsView)sender).FindControl("SchoolNameInc")).Checked;
                        obj.DistrictInc = ((CheckBox)((DetailsView)sender).FindControl("DistrictInc")).Checked;
                        obj.TeacherInc = ((CheckBox)((DetailsView)sender).FindControl("TeacherInc")).Checked;
                        obj.GroupTeamNameInc =
                            ((CheckBox)((DetailsView)sender).FindControl("GroupTeamNameInc")).Checked;
                        obj.SchoolTypeInc = ((CheckBox)((DetailsView)sender).FindControl("SchoolTypeInc")).Checked;
                        obj.LiteracyLevel1Inc =
                            ((CheckBox)((DetailsView)sender).FindControl("LiteracyLevel1Inc")).Checked;
                        obj.LiteracyLevel2Inc =
                            ((CheckBox)((DetailsView)sender).FindControl("LiteracyLevel2Inc")).Checked;
                        obj.Custom1Inc = ((CheckBox)((DetailsView)sender).FindControl("Custom1Inc")).Checked;
                        obj.Custom2Inc = ((CheckBox)((DetailsView)sender).FindControl("Custom2Inc")).Checked;
                        obj.Custom3Inc = ((CheckBox)((DetailsView)sender).FindControl("Custom3Inc")).Checked;
                        obj.Custom4Inc = ((CheckBox)((DetailsView)sender).FindControl("Custom4Inc")).Checked;
                        obj.Custom5Inc = ((CheckBox)((DetailsView)sender).FindControl("Custom5Inc")).Checked;
                        obj.RegistrationDateInc =
                            ((CheckBox)((DetailsView)sender).FindControl("RegistrationDateInc")).Checked;
                        obj.PointsInc = ((CheckBox)((DetailsView)sender).FindControl("PointsInc")).Checked;
                        obj.EarnedBadgeInc = ((CheckBox)((DetailsView)sender).FindControl("EarnedBadgeInc")).Checked;
                        obj.PhysicalPrizeNameInc =
                            ((CheckBox)((DetailsView)sender).FindControl("PhysicalPrizeNameInc")).Checked;
                        obj.PhysicalPrizeDateInc =
                            ((CheckBox)((DetailsView)sender).FindControl("PhysicalPrizeDateInc")).Checked;
                        obj.NumReviewsInc = ((CheckBox)((DetailsView)sender).FindControl("NumReviewsInc")).Checked;
                        obj.ReviewAuthorInc = ((CheckBox)((DetailsView)sender).FindControl("ReviewAuthorInc")).Checked;
                        obj.ReviewTitleInc = ((CheckBox)((DetailsView)sender).FindControl("ReviewTitleInc")).Checked;
                        obj.ReviewDateInc = ((CheckBox)((DetailsView)sender).FindControl("ReviewDateInc")).Checked;
                        obj.RandomDrawingNameInc =
                            ((CheckBox)((DetailsView)sender).FindControl("RandomDrawingNameInc")).Checked;
                        obj.RandomDrawingNumInc =
                            ((CheckBox)((DetailsView)sender).FindControl("RandomDrawingNumInc")).Checked;
                        obj.RandomDrawingDateInc =
                            ((CheckBox)((DetailsView)sender).FindControl("RandomDrawingDateInc")).Checked;
                        obj.HasBeenDrawnInc = ((CheckBox)((DetailsView)sender).FindControl("HasBeenDrawnInc")).Checked;
                        obj.HasRedeemendInc = ((CheckBox)((DetailsView)sender).FindControl("HasRedeemendInc")).Checked;

                        obj.Score1From =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score1From")).Text);
                        obj.Score1To =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score1To")).Text);
                        obj.Score2From =
                            FormatHelper.SafeToInt(
                                ((TextBox)((DetailsView)sender).FindControl("Score2From")).Text);
                        obj.Score2To =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score2To")).Text);

                        obj.Score1PctFrom =
                                FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score1PctFrom")).Text);
                        obj.Score1PctTo =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score1PctTo")).Text);
                        obj.Score2PctFrom =
                            FormatHelper.SafeToInt(
                                ((TextBox)((DetailsView)sender).FindControl("Score2PctFrom")).Text);
                        obj.Score2PctTo =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score2PctTo")).Text);

                        obj.Score1Inc = ((CheckBox)((DetailsView)sender).FindControl("Score1Inc")).Checked;
                        obj.Score2Inc = ((CheckBox)((DetailsView)sender).FindControl("Score2Inc")).Checked;
                        obj.Score1PctInc = ((CheckBox)((DetailsView)sender).FindControl("Score1PctInc")).Checked;
                        obj.Score2PctInc = ((CheckBox)((DetailsView)sender).FindControl("Score2PctInc")).Checked;

                        obj.LastModDate = DateTime.Now;
                        obj.LastModUser = ((SRPUser)Session[SessionData.UserProfile.ToString()]).Username;
                        //"N/A";  // Get from session

                        if (obj.IsValid(BusinessRulesValidationMode.UPDATE))
                        {
                            obj.Update();

                            odsData.DataBind();
                            dv.DataBind();
                            dv.ChangeMode(DetailsViewMode.Edit);

                            var masterPage = (IControlRoomMaster)Master;
                            masterPage.PageMessage = SRPResources.SaveOK;
                        }
                        else
                        {
                            var masterPage = (IControlRoomMaster)Master;
                            string message = String.Format(SRPResources.ApplicationError1, "<ul>");
                            foreach (BusinessRulesValidationMessage m in obj.ErrorCodes)
                            {
                                message = string.Format(String.Format("{0}<li>{{0}}</li>", message), m.ErrorMessage);
                            }
                            message = string.Format("{0}</ul>", message);
                            masterPage.PageError = message;
                        }
                    }
                    catch (Exception ex)
                    {
                        var masterPage = (IControlRoomMaster)Master;
                        masterPage.PageError = String.Format(SRPResources.ApplicationError1, ex.Message);
                    }
                }
            }
            #endregion

            #region save as template
            if (e.CommandName.ToLower() == "savetemplate")
            {
                var templateName = ((TextBox)((DetailsView)sender).FindControl("TemplateName")).Text.Trim();
                if (templateName == "")
                {
                    var masterPage = (IControlRoomMaster)Master;
                    masterPage.PageError = String.Format("To create a report template from the current report format, you need to enter a Template Name.");
                    ((TextBox)((DetailsView)sender).FindControl("TemplateName")).Focus();
                    return;
                }
                try
                {
                    //ADD 
                    var obj = new ReportTemplate();

                    obj.ProgId =
                        FormatHelper.SafeToInt(
                            ((DropDownList)((DetailsView)sender).FindControl("ProgId")).SelectedValue);
                    obj.ReportName = ((TextBox)((DetailsView)sender).FindControl("TemplateName")).Text;
                    obj.DisplayFilters = ((CheckBox)((DetailsView)sender).FindControl("DisplayFilters")).Checked;

                    obj.DOBFrom =
                        FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("DOBFrom")).Text);
                    obj.DOBTo =
                        FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("DOBTo")).Text);
                    obj.AgeFrom =
                        FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("AgeFrom")).Text);
                    obj.AgeTo = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("AgeTo")).Text);
                    obj.SchoolGrade = ((TextBox)((DetailsView)sender).FindControl("SchoolGrade")).Text;
                    obj.FirstName = ((TextBox)((DetailsView)sender).FindControl("FirstName")).Text;
                    obj.LastName = ((TextBox)((DetailsView)sender).FindControl("LastName")).Text;
                    obj.Gender = ((DropDownList)((DetailsView)sender).FindControl("Gender")).SelectedValue;
                    obj.EmailAddress = ((TextBox)((DetailsView)sender).FindControl("EmailAddress")).Text;
                    obj.PhoneNumber = ((TextBox)((DetailsView)sender).FindControl("PhoneNumber")).Text;
                    obj.City = ((TextBox)((DetailsView)sender).FindControl("City")).Text;
                    obj.State = ((TextBox)((DetailsView)sender).FindControl("State")).Text;
                    obj.ZipCode = ((TextBox)((DetailsView)sender).FindControl("ZipCode")).Text;
                    obj.County = ((TextBox)((DetailsView)sender).FindControl("County")).Text;

                    obj.PrimaryLibrary =
                        FormatHelper.SafeToInt(
                            ((DropDownList)((DetailsView)sender).FindControl("PrimaryLibrary")).SelectedValue);
                    obj.SchoolName = ((DropDownList)((DetailsView)sender).FindControl("SchoolName")).SelectedValue;
                    obj.District = ((DropDownList)((DetailsView)sender).FindControl("District")).SelectedValue;
                    obj.SDistrict = ((DropDownList)((DetailsView)sender).FindControl("SDistrict")).SelectedValue;

                    obj.Teacher = ((TextBox)((DetailsView)sender).FindControl("Teacher")).Text;
                    obj.GroupTeamName = ((TextBox)((DetailsView)sender).FindControl("GroupTeamName")).Text;
                    obj.SchoolType =
                        FormatHelper.SafeToInt(
                            ((DropDownList)((DetailsView)sender).FindControl("SchoolType")).SelectedValue);

                    obj.LiteracyLevel1 =
                        FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("LiteracyLevel1")).Text);
                    obj.LiteracyLevel2 =
                        FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("LiteracyLevel2")).Text);

                    obj.Custom1 = ((TextBox)((DetailsView)sender).FindControl("Custom1")).Text;
                    obj.Custom2 = ((TextBox)((DetailsView)sender).FindControl("Custom2")).Text;
                    obj.Custom3 = ((TextBox)((DetailsView)sender).FindControl("Custom3")).Text;
                    obj.Custom4 = ((TextBox)((DetailsView)sender).FindControl("Custom4")).Text;
                    obj.Custom5 = ((TextBox)((DetailsView)sender).FindControl("Custom5")).Text;

                    obj.RegistrationDateStart =
                        FormatHelper.SafeToDateTime(
                            ((TextBox)((DetailsView)sender).FindControl("RegistrationDateStart")).Text);
                    obj.RegistrationDateEnd =
                        FormatHelper.SafeToDateTime(
                            ((TextBox)((DetailsView)sender).FindControl("RegistrationDateEnd")).Text);

                    obj.PointsMin =
                        FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("PointsMin")).Text);
                    obj.PointsMax =
                        FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("PointsMax")).Text);
                    obj.PointsStart =
                        FormatHelper.SafeToDateTime(
                            ((TextBox)((DetailsView)sender).FindControl("PointsStart")).Text);
                    obj.PointsEnd =
                        FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("PointsEnd")).Text);
                    obj.EventCode = ((TextBox)((DetailsView)sender).FindControl("EventCode")).Text;
                    obj.EarnedBadge =
                        FormatHelper.SafeToInt(
                            ((DropDownList)((DetailsView)sender).FindControl("EarnedBadge")).SelectedValue);
                    obj.PhysicalPrizeEarned =
                        ((TextBox)((DetailsView)sender).FindControl("PhysicalPrizeEarned")).Text;
                    obj.PhysicalPrizeRedeemed =
                        ((CheckBox)((DetailsView)sender).FindControl("PhysicalPrizeRedeemed")).Checked;
                    obj.PhysicalPrizeStartDate =
                        FormatHelper.SafeToDateTime(
                            ((TextBox)((DetailsView)sender).FindControl("PhysicalPrizeStartDate")).Text);
                    obj.PhysicalPrizeEndDate =
                        FormatHelper.SafeToDateTime(
                            ((TextBox)((DetailsView)sender).FindControl("PhysicalPrizeEndDate")).Text);

                    obj.ReviewsMin =
                        FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("ReviewsMin")).Text);
                    obj.ReviewsMax =
                        FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("ReviewsMax")).Text);
                    obj.ReviewTitle = ((TextBox)((DetailsView)sender).FindControl("ReviewTitle")).Text;
                    obj.ReviewAuthor = ((TextBox)((DetailsView)sender).FindControl("ReviewAuthor")).Text;
                    obj.ReviewStartDate =
                        FormatHelper.SafeToDateTime(
                            ((TextBox)((DetailsView)sender).FindControl("ReviewStartDate")).Text);
                    obj.ReviewEndDate =
                        FormatHelper.SafeToDateTime(
                            ((TextBox)((DetailsView)sender).FindControl("ReviewEndDate")).Text);
                    obj.RandomDrawingName = ((TextBox)((DetailsView)sender).FindControl("RandomDrawingName")).Text;
                    obj.RandomDrawingNum =
                        FormatHelper.SafeToInt(
                            ((TextBox)((DetailsView)sender).FindControl("RandomDrawingNum")).Text);
                    obj.RandomDrawingStartDate =
                        FormatHelper.SafeToDateTime(
                            ((TextBox)((DetailsView)sender).FindControl("RandomDrawingStartDate")).Text);
                    obj.RandomDrawingEndDate =
                        FormatHelper.SafeToDateTime(
                            ((TextBox)((DetailsView)sender).FindControl("RandomDrawingEndDate")).Text);
                    obj.HasBeenDrawn = ((CheckBox)((DetailsView)sender).FindControl("HasBeenDrawn")).Checked;
                    obj.HasRedeemend = ((CheckBox)((DetailsView)sender).FindControl("HasRedeemend")).Checked;



                    obj.Score1From =
                        FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score1From")).Text);
                    obj.Score1To =
                        FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score1To")).Text);
                    obj.Score2From =
                        FormatHelper.SafeToInt(
                            ((TextBox)((DetailsView)sender).FindControl("Score2From")).Text);
                    obj.Score2To =
                        FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score2To")).Text);

                    obj.Score1PctFrom =
                            FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score1PctFrom")).Text);
                    obj.Score1PctTo =
                        FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score1PctTo")).Text);
                    obj.Score2PctFrom =
                        FormatHelper.SafeToInt(
                            ((TextBox)((DetailsView)sender).FindControl("Score2PctFrom")).Text);
                    obj.Score2PctTo =
                        FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("Score2PctTo")).Text);

                    obj.Score1Inc = ((CheckBox)((DetailsView)sender).FindControl("Score1Inc")).Checked;
                    obj.Score2Inc = ((CheckBox)((DetailsView)sender).FindControl("Score2Inc")).Checked;
                    obj.Score2PctInc = ((CheckBox)((DetailsView)sender).FindControl("Score2PctInc")).Checked;
                    obj.Score2PctInc = ((CheckBox)((DetailsView)sender).FindControl("Score2PctInc")).Checked;


                    obj.PIDInc = ((CheckBox)((DetailsView)sender).FindControl("PIDInc")).Checked;
                    obj.UsernameInc = ((CheckBox)((DetailsView)sender).FindControl("UsernameInc")).Checked;
                    obj.DOBInc = ((CheckBox)((DetailsView)sender).FindControl("DOBInc")).Checked;
                    obj.AgeInc = ((CheckBox)((DetailsView)sender).FindControl("AgeInc")).Checked;
                    obj.SchoolGradeInc = ((CheckBox)((DetailsView)sender).FindControl("SchoolGradeInc")).Checked;
                    obj.FirstNameInc = ((CheckBox)((DetailsView)sender).FindControl("FirstNameInc")).Checked;
                    obj.LastNameInc = ((CheckBox)((DetailsView)sender).FindControl("LastNameInc")).Checked;
                    obj.GenderInc = ((CheckBox)((DetailsView)sender).FindControl("GenderInc")).Checked;
                    obj.EmailAddressInc = ((CheckBox)((DetailsView)sender).FindControl("EmailAddressInc")).Checked;
                    obj.PhoneNumberInc = ((CheckBox)((DetailsView)sender).FindControl("PhoneNumberInc")).Checked;
                    obj.CityInc = ((CheckBox)((DetailsView)sender).FindControl("CityInc")).Checked;
                    obj.StateInc = ((CheckBox)((DetailsView)sender).FindControl("StateInc")).Checked;
                    obj.ZipCodeInc = ((CheckBox)((DetailsView)sender).FindControl("ZipCodeInc")).Checked;
                    obj.CountyInc = ((CheckBox)((DetailsView)sender).FindControl("CountyInc")).Checked;
                    obj.PrimaryLibraryInc =
                        ((CheckBox)((DetailsView)sender).FindControl("PrimaryLibraryInc")).Checked;
                    obj.SchoolNameInc = ((CheckBox)((DetailsView)sender).FindControl("SchoolNameInc")).Checked;
                    obj.DistrictInc = ((CheckBox)((DetailsView)sender).FindControl("DistrictInc")).Checked;
                    obj.TeacherInc = ((CheckBox)((DetailsView)sender).FindControl("TeacherInc")).Checked;
                    obj.GroupTeamNameInc =
                        ((CheckBox)((DetailsView)sender).FindControl("GroupTeamNameInc")).Checked;
                    obj.SchoolTypeInc = ((CheckBox)((DetailsView)sender).FindControl("SchoolTypeInc")).Checked;
                    obj.LiteracyLevel1Inc =
                        ((CheckBox)((DetailsView)sender).FindControl("LiteracyLevel1Inc")).Checked;
                    obj.LiteracyLevel2Inc =
                        ((CheckBox)((DetailsView)sender).FindControl("LiteracyLevel2Inc")).Checked;
                    obj.Custom1Inc = ((CheckBox)((DetailsView)sender).FindControl("Custom1Inc")).Checked;
                    obj.Custom2Inc = ((CheckBox)((DetailsView)sender).FindControl("Custom2Inc")).Checked;
                    obj.Custom3Inc = ((CheckBox)((DetailsView)sender).FindControl("Custom3Inc")).Checked;
                    obj.Custom4Inc = ((CheckBox)((DetailsView)sender).FindControl("Custom4Inc")).Checked;
                    obj.Custom5Inc = ((CheckBox)((DetailsView)sender).FindControl("Custom5Inc")).Checked;
                    obj.RegistrationDateInc =
                        ((CheckBox)((DetailsView)sender).FindControl("RegistrationDateInc")).Checked;
                    obj.PointsInc = ((CheckBox)((DetailsView)sender).FindControl("PointsInc")).Checked;
                    obj.EarnedBadgeInc = ((CheckBox)((DetailsView)sender).FindControl("EarnedBadgeInc")).Checked;
                    obj.PhysicalPrizeNameInc =
                        ((CheckBox)((DetailsView)sender).FindControl("PhysicalPrizeNameInc")).Checked;
                    obj.PhysicalPrizeDateInc =
                        ((CheckBox)((DetailsView)sender).FindControl("PhysicalPrizeDateInc")).Checked;
                    obj.NumReviewsInc = ((CheckBox)((DetailsView)sender).FindControl("NumReviewsInc")).Checked;
                    obj.ReviewAuthorInc = ((CheckBox)((DetailsView)sender).FindControl("ReviewAuthorInc")).Checked;
                    obj.ReviewTitleInc = ((CheckBox)((DetailsView)sender).FindControl("ReviewTitleInc")).Checked;
                    obj.ReviewDateInc = ((CheckBox)((DetailsView)sender).FindControl("ReviewDateInc")).Checked;
                    obj.RandomDrawingNameInc =
                        ((CheckBox)((DetailsView)sender).FindControl("RandomDrawingNameInc")).Checked;
                    obj.RandomDrawingNumInc =
                        ((CheckBox)((DetailsView)sender).FindControl("RandomDrawingNumInc")).Checked;
                    obj.RandomDrawingDateInc =
                        ((CheckBox)((DetailsView)sender).FindControl("RandomDrawingDateInc")).Checked;
                    obj.HasBeenDrawnInc = ((CheckBox)((DetailsView)sender).FindControl("HasBeenDrawnInc")).Checked;
                    obj.HasRedeemendInc = ((CheckBox)((DetailsView)sender).FindControl("HasRedeemendInc")).Checked;

                    obj.AddedDate = DateTime.Now;
                    obj.AddedUser = ((SRPUser)Session[SessionData.UserProfile.ToString()]).Username;
                    //"N/A";  // Get from session
                    obj.LastModDate = obj.AddedDate;
                    obj.LastModUser = obj.AddedUser;

                    if (obj.IsValid(BusinessRulesValidationMode.INSERT))
                    {
                        obj.Insert();

                        odsData.DataBind();
                        dv.DataBind();
                        dv.ChangeMode(DetailsViewMode.Edit);

                        var masterPage = (IControlRoomMaster)Master;
                        masterPage.PageMessage = "Report Template has been successfully created!";
                    }
                    else
                    {
                        var masterPage = (IControlRoomMaster)Master;
                        string message = String.Format(SRPResources.ApplicationError1, "<ul>");
                        foreach (BusinessRulesValidationMessage m in obj.ErrorCodes)
                        {
                            message = string.Format(String.Format("{0}<li>{{0}}</li>", message), m.ErrorMessage);
                        }
                        message = string.Format("{0}</ul>", message);
                        masterPage.PageError = message;
                    }
                }
                catch (Exception ex)
                {
                    var masterPage = (IControlRoomMaster)Master;
                    masterPage.PageError = String.Format(SRPResources.ApplicationError1, ex.Message);
                }
            }



            #endregion

            #region load from template
            if (e.CommandName.ToLower() == "loadtemplate")
            {
                try
                {
                    var RTID =
                            FormatHelper.SafeToInt(
                                ((DropDownList)((DetailsView)sender).FindControl("RTID")).SelectedValue);

                    if (RTID == 0) return;

                    var obj = ReportTemplate.FetchObject(RTID);

                    var dd = ((DropDownList)((DetailsView)sender).FindControl("ProgId"));
                    var i = dd.Items.FindByValue(obj.ProgId.ToString());
                    if (i != null) dd.SelectedValue = (obj.ProgId.ToString());

                    dd = ((DropDownList)((DetailsView)sender).FindControl("Gender"));
                    i = dd.Items.FindByValue(obj.Gender);
                    if (i != null) dd.SelectedValue = (obj.Gender);

                    dd = ((DropDownList)((DetailsView)sender).FindControl("PrimaryLibrary"));
                    i = dd.Items.FindByValue(obj.PrimaryLibrary.ToString());
                    if (i != null) dd.SelectedValue = (obj.PrimaryLibrary.ToString());

                    dd = ((DropDownList)((DetailsView)sender).FindControl("SchoolName"));
                    i = dd.Items.FindByValue(obj.SchoolName.ToString());
                    if (i != null) dd.SelectedValue = (obj.SchoolName.ToString());

                    dd = ((DropDownList)((DetailsView)sender).FindControl("District"));
                    i = dd.Items.FindByValue(obj.District.ToString());
                    if (i != null) dd.SelectedValue = (obj.District.ToString());

                    dd = ((DropDownList)((DetailsView)sender).FindControl("SDistrict"));
                    i = dd.Items.FindByValue(obj.SDistrict.ToString());
                    if (i != null) dd.SelectedValue = (obj.SDistrict.ToString());


                    dd = ((DropDownList)((DetailsView)sender).FindControl("SchoolType"));
                    i = dd.Items.FindByValue(obj.SchoolType.ToString());
                    if (i != null) dd.SelectedValue = (obj.SchoolType.ToString());

                    dd = ((DropDownList)((DetailsView)sender).FindControl("EarnedBadge"));
                    i = dd.Items.FindByValue(obj.EarnedBadge.ToString());
                    if (i != null) dd.SelectedValue = (obj.EarnedBadge.ToString());


                    //((TextBox)((DetailsView)sender).FindControl("ReportName")).Text = obj.ReportName;
                    ((TextBox)((DetailsView)sender).FindControl("SchoolGrade")).Text = obj.SchoolGrade;
                    ((TextBox)((DetailsView)sender).FindControl("FirstName")).Text = obj.FirstName;
                    ((TextBox)((DetailsView)sender).FindControl("LastName")).Text = obj.LastName;
                    ((TextBox)((DetailsView)sender).FindControl("PhoneNumber")).Text = obj.PhoneNumber;
                    ((TextBox)((DetailsView)sender).FindControl("City")).Text = obj.City;
                    ((TextBox)((DetailsView)sender).FindControl("State")).Text = obj.State;
                    ((TextBox)((DetailsView)sender).FindControl("ZipCode")).Text = obj.ZipCode;
                    ((TextBox)((DetailsView)sender).FindControl("County")).Text = obj.County;
                    //((TextBox)((DetailsView)sender).FindControl("SchoolName")).Text = obj.SchoolName;
                    //((TextBox)((DetailsView)sender).FindControl("District")).Text = obj.District;
                    ((TextBox)((DetailsView)sender).FindControl("Teacher")).Text = obj.Teacher;
                    ((TextBox)((DetailsView)sender).FindControl("GroupTeamName")).Text = obj.GroupTeamName;
                    ((TextBox)((DetailsView)sender).FindControl("Custom1")).Text = obj.Custom1;
                    ((TextBox)((DetailsView)sender).FindControl("Custom2")).Text = obj.Custom2;
                    ((TextBox)((DetailsView)sender).FindControl("Custom3")).Text = obj.Custom3;
                    ((TextBox)((DetailsView)sender).FindControl("Custom4")).Text = obj.Custom4;
                    ((TextBox)((DetailsView)sender).FindControl("Custom5")).Text = obj.Custom5;
                    ((TextBox)((DetailsView)sender).FindControl("EventCode")).Text = obj.EventCode;
                    ((TextBox)((DetailsView)sender).FindControl("PhysicalPrizeEarned")).Text = obj.PhysicalPrizeEarned;
                    ((TextBox)((DetailsView)sender).FindControl("ReviewTitle")).Text = obj.ReviewTitle;
                    ((TextBox)((DetailsView)sender).FindControl("ReviewAuthor")).Text = obj.ReviewAuthor;
                    ((TextBox)((DetailsView)sender).FindControl("RandomDrawingName")).Text = obj.RandomDrawingName;


                    ((TextBox)((DetailsView)sender).FindControl("DOBFrom")).Text = FormatHelper.ToWidgetDisplayDate(obj.DOBFrom);
                    ((TextBox)((DetailsView)sender).FindControl("DOBTo")).Text = FormatHelper.ToWidgetDisplayDate(obj.DOBTo);
                    ((TextBox)((DetailsView)sender).FindControl("RegistrationDateStart")).Text = FormatHelper.ToWidgetDisplayDate(obj.RegistrationDateStart);
                    ((TextBox)((DetailsView)sender).FindControl("RegistrationDateEnd")).Text = FormatHelper.ToWidgetDisplayDate(obj.RegistrationDateEnd);
                    ((TextBox)((DetailsView)sender).FindControl("PointsStart")).Text = FormatHelper.ToWidgetDisplayDate(obj.PointsStart);
                    ((TextBox)((DetailsView)sender).FindControl("PointsEnd")).Text = FormatHelper.ToWidgetDisplayDate(obj.PointsEnd);
                    ((TextBox)((DetailsView)sender).FindControl("PhysicalPrizeStartDate")).Text = FormatHelper.ToWidgetDisplayDate(obj.PhysicalPrizeStartDate);
                    ((TextBox)((DetailsView)sender).FindControl("PhysicalPrizeEndDate")).Text = FormatHelper.ToWidgetDisplayDate(obj.PhysicalPrizeEndDate);
                    ((TextBox)((DetailsView)sender).FindControl("ReviewStartDate")).Text = FormatHelper.ToWidgetDisplayDate(obj.ReviewStartDate);
                    ((TextBox)((DetailsView)sender).FindControl("ReviewEndDate")).Text = FormatHelper.ToWidgetDisplayDate(obj.ReviewEndDate);
                    ((TextBox)((DetailsView)sender).FindControl("RandomDrawingStartDate")).Text = FormatHelper.ToWidgetDisplayDate(obj.RandomDrawingStartDate);
                    ((TextBox)((DetailsView)sender).FindControl("RandomDrawingEndDate")).Text = FormatHelper.ToWidgetDisplayDate(obj.RandomDrawingEndDate);


                    ((TextBox)((DetailsView)sender).FindControl("AgeFrom")).Text = FormatHelper.ToWidgetDisplayInt(obj.AgeFrom);
                    ((TextBox)((DetailsView)sender).FindControl("AgeTo")).Text = FormatHelper.ToWidgetDisplayInt(obj.AgeTo);
                    ((TextBox)((DetailsView)sender).FindControl("LiteracyLevel1")).Text = FormatHelper.ToWidgetDisplayInt(obj.LiteracyLevel1);
                    ((TextBox)((DetailsView)sender).FindControl("LiteracyLevel2")).Text = FormatHelper.ToWidgetDisplayInt(obj.LiteracyLevel2);
                    ((TextBox)((DetailsView)sender).FindControl("PointsMin")).Text = FormatHelper.ToWidgetDisplayInt(obj.PointsMin);
                    ((TextBox)((DetailsView)sender).FindControl("PointsMax")).Text = FormatHelper.ToWidgetDisplayInt(obj.PointsMax);
                    ((TextBox)((DetailsView)sender).FindControl("ReviewsMin")).Text = FormatHelper.ToWidgetDisplayInt(obj.ReviewsMin);
                    ((TextBox)((DetailsView)sender).FindControl("ReviewsMax")).Text = FormatHelper.ToWidgetDisplayInt(obj.ReviewsMax);
                    ((TextBox)((DetailsView)sender).FindControl("RandomDrawingNum")).Text = FormatHelper.ToWidgetDisplayInt(obj.RandomDrawingNum);


                    ((CheckBox)((DetailsView)sender).FindControl("DisplayFilters")).Checked = obj.DisplayFilters;
                    ((CheckBox)((DetailsView)sender).FindControl("PhysicalPrizeRedeemed")).Checked = obj.PhysicalPrizeRedeemed;
                    ((CheckBox)((DetailsView)sender).FindControl("HasBeenDrawn")).Checked = obj.HasBeenDrawn;
                    ((CheckBox)((DetailsView)sender).FindControl("HasRedeemend")).Checked = obj.HasRedeemend;
                    ((CheckBox)((DetailsView)sender).FindControl("PIDInc")).Checked = obj.PIDInc;
                    ((CheckBox)((DetailsView)sender).FindControl("UsernameInc")).Checked = obj.UsernameInc;
                    ((CheckBox)((DetailsView)sender).FindControl("AgeInc")).Checked = obj.AgeInc;
                    ((CheckBox)((DetailsView)sender).FindControl("SchoolGradeInc")).Checked = obj.SchoolGradeInc;
                    ((CheckBox)((DetailsView)sender).FindControl("FirstNameInc")).Checked = obj.FirstNameInc;
                    ((CheckBox)((DetailsView)sender).FindControl("LastNameInc")).Checked = obj.LastNameInc;
                    ((CheckBox)((DetailsView)sender).FindControl("GenderInc")).Checked = obj.GenderInc;
                    ((CheckBox)((DetailsView)sender).FindControl("EmailAddressInc")).Checked = obj.EmailAddressInc;
                    ((CheckBox)((DetailsView)sender).FindControl("PhoneNumberInc")).Checked = obj.PhoneNumberInc;
                    ((CheckBox)((DetailsView)sender).FindControl("CityInc")).Checked = obj.CityInc;
                    ((CheckBox)((DetailsView)sender).FindControl("StateInc")).Checked = obj.StateInc;
                    ((CheckBox)((DetailsView)sender).FindControl("ZipCodeInc")).Checked = obj.ZipCodeInc;
                    ((CheckBox)((DetailsView)sender).FindControl("CountyInc")).Checked = obj.CountyInc;
                    ((CheckBox)((DetailsView)sender).FindControl("PrimaryLibraryInc")).Checked = obj.PrimaryLibraryInc;
                    ((CheckBox)((DetailsView)sender).FindControl("SchoolNameInc")).Checked = obj.SchoolNameInc;
                    ((CheckBox)((DetailsView)sender).FindControl("TeacherInc")).Checked = obj.TeacherInc;
                    ((CheckBox)((DetailsView)sender).FindControl("GroupTeamNameInc")).Checked = obj.GroupTeamNameInc;
                    ((CheckBox)((DetailsView)sender).FindControl("SchoolTypeInc")).Checked = obj.SchoolTypeInc;
                    ((CheckBox)((DetailsView)sender).FindControl("LiteracyLevel1Inc")).Checked = obj.LiteracyLevel1Inc;
                    ((CheckBox)((DetailsView)sender).FindControl("LiteracyLevel2Inc")).Checked = obj.LiteracyLevel2Inc;
                    ((CheckBox)((DetailsView)sender).FindControl("Custom1Inc")).Checked = obj.Custom1Inc;
                    ((CheckBox)((DetailsView)sender).FindControl("Custom2Inc")).Checked = obj.Custom2Inc;
                    ((CheckBox)((DetailsView)sender).FindControl("Custom3Inc")).Checked = obj.Custom3Inc;
                    ((CheckBox)((DetailsView)sender).FindControl("Custom4Inc")).Checked = obj.Custom4Inc;
                    ((CheckBox)((DetailsView)sender).FindControl("Custom5Inc")).Checked = obj.Custom5Inc;
                    ((CheckBox)((DetailsView)sender).FindControl("RegistrationDateInc")).Checked = obj.RegistrationDateInc;
                    ((CheckBox)((DetailsView)sender).FindControl("PointsInc")).Checked = obj.PointsInc;
                    ((CheckBox)((DetailsView)sender).FindControl("EarnedBadgeInc")).Checked = obj.EarnedBadgeInc;
                    ((CheckBox)((DetailsView)sender).FindControl("PhysicalPrizeNameInc")).Checked = obj.PhysicalPrizeNameInc;
                    ((CheckBox)((DetailsView)sender).FindControl("NumReviewsInc")).Checked = obj.NumReviewsInc;
                    ((CheckBox)((DetailsView)sender).FindControl("ReviewAuthorInc")).Checked = obj.ReviewAuthorInc;
                    ((CheckBox)((DetailsView)sender).FindControl("ReviewTitleInc")).Checked = obj.ReviewTitleInc;
                    ((CheckBox)((DetailsView)sender).FindControl("ReviewDateInc")).Checked = obj.ReviewDateInc;
                    ((CheckBox)((DetailsView)sender).FindControl("RandomDrawingNameInc")).Checked = obj.RandomDrawingNameInc;
                    ((CheckBox)((DetailsView)sender).FindControl("RandomDrawingNumInc")).Checked = obj.RandomDrawingNumInc;
                    ((CheckBox)((DetailsView)sender).FindControl("RandomDrawingDateInc")).Checked = obj.RandomDrawingDateInc;
                    ((CheckBox)((DetailsView)sender).FindControl("HasBeenDrawnInc")).Checked = obj.HasBeenDrawnInc;
                    ((CheckBox)((DetailsView)sender).FindControl("HasRedeemendInc")).Checked = obj.HasRedeemendInc;

                    ((TextBox)((DetailsView)sender).FindControl("Score1From")).Text = FormatHelper.ToWidgetDisplayInt(obj.Score1From);
                    ((TextBox)((DetailsView)sender).FindControl("Score2From")).Text = FormatHelper.ToWidgetDisplayInt(obj.Score2From);
                    ((TextBox)((DetailsView)sender).FindControl("Score1PctFrom")).Text = FormatHelper.ToWidgetDisplayInt(obj.Score1PctFrom);
                    ((TextBox)((DetailsView)sender).FindControl("Score2PctFrom")).Text = FormatHelper.ToWidgetDisplayInt(obj.Score2PctFrom);

                    ((TextBox)((DetailsView)sender).FindControl("Score1To")).Text = FormatHelper.ToWidgetDisplayInt(obj.Score1To);
                    ((TextBox)((DetailsView)sender).FindControl("Score2To")).Text = FormatHelper.ToWidgetDisplayInt(obj.Score2To);
                    ((TextBox)((DetailsView)sender).FindControl("Score1PctTo")).Text = FormatHelper.ToWidgetDisplayInt(obj.Score1PctTo);
                    ((TextBox)((DetailsView)sender).FindControl("Score2PctTo")).Text = FormatHelper.ToWidgetDisplayInt(obj.Score2PctTo);

                    ((CheckBox)((DetailsView)sender).FindControl("Score1Inc")).Checked = obj.Score1Inc;
                    ((CheckBox)((DetailsView)sender).FindControl("Score2Inc")).Checked = obj.Score2Inc;
                    ((CheckBox)((DetailsView)sender).FindControl("Score1PctInc")).Checked = obj.Score1PctInc;
                    ((CheckBox)((DetailsView)sender).FindControl("Score2PctInc")).Checked = obj.Score2PctInc;

                    var masterPage = (IControlRoomMaster)Master;
                    if (masterPage != null) masterPage.PageMessage = "Report template loaded.";
                }
                catch (Exception ex)
                {
                    var masterPage = (IControlRoomMaster)Master;
                    masterPage.PageError = String.Format(SRPResources.ApplicationError1, ex.Message);
                }
            }
            #endregion


            #region RUN REPORT
            if (e.CommandName.ToLower() == "runreport")
            {
                try
                {
                    SqlParameter[] arrParams = null;
                    var retColumns = string.Empty;
                    var whereClause = string.Empty;
                    var fromClause = "Patron p ";

                    var filterStr = string.Empty;
                    //var tmpFilter= string.Empty;
                    var minorSep = "~";
                    var majorSep = "|";

                    if (((CheckBox)((DetailsView)sender).FindControl("PIDInc")).Checked)
                        retColumns = Coalesce(retColumns, "p.PID as PatronID", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("UsernameInc")).Checked)
                        retColumns = Coalesce(retColumns, "p.Username", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("DOBInc")).Checked)
                        retColumns = Coalesce(retColumns, "convert(varchar, DOB, 101) as DOB", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("AgeInc")).Checked)
                        retColumns = Coalesce(retColumns, "case when Age > 0 then Age else null end as Age", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("SchoolGradeInc")).Checked)
                        retColumns = Coalesce(retColumns, "SchoolGrade", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("FirstNameInc")).Checked)
                        retColumns = Coalesce(retColumns, "FirstName", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("LastNameInc")).Checked)
                        retColumns = Coalesce(retColumns, "LastName", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("GenderInc")).Checked)
                        retColumns = Coalesce(retColumns, "Gender", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("EmailAddressInc")).Checked)
                        retColumns = Coalesce(retColumns, "EmailAddress", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("PhoneNumberInc")).Checked)
                        retColumns = Coalesce(retColumns, "PhoneNumber", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("CityInc")).Checked)
                        retColumns = Coalesce(retColumns, "City", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("StateInc")).Checked)
                        retColumns = Coalesce(retColumns, "State", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("ZipCodeInc")).Checked)
                        retColumns = Coalesce(retColumns, "ZipCode", ",");

                    if (((CheckBox)((DetailsView)sender).FindControl("Score1Inc")).Checked)
                        retColumns = Coalesce(retColumns, "Score1", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("Score2Inc")).Checked)
                        retColumns = Coalesce(retColumns, "Score2", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("Score1PctInc")).Checked)
                        retColumns = Coalesce(retColumns, "Score1Pct", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("Score2PctInc")).Checked)
                        retColumns = Coalesce(retColumns, "Score2Pct", ",");


                    if (((CheckBox)((DetailsView)sender).FindControl("DistrictInc")).Checked)
                    {
                        // CODE Translate
                        fromClause = Coalesce(fromClause, "left outer join Code C4 on C4.CID = p.District", " ");
                        retColumns = Coalesce(retColumns, "C4.Code as District", ",");
                    }

                    if (((CheckBox)((DetailsView)sender).FindControl("PrimaryLibraryInc")).Checked)
                    {
                        // CODE Translate
                        fromClause = Coalesce(fromClause, "left outer join Code C1 on C1.CID = p.PrimaryLibrary", " ");
                        retColumns = Coalesce(retColumns, "C1.Code as PrimaryLibrary", ",");
                    }

                    if (((CheckBox)((DetailsView)sender).FindControl("SDistrictInc")).Checked)
                    {
                        // CODE Translate
                        fromClause = Coalesce(fromClause, "left outer join Code C5 on C5.CID = p.SDistrict", " ");
                        retColumns = Coalesce(retColumns, "C5.Code as SDistrict", ",");
                    }

                    if (((CheckBox)((DetailsView)sender).FindControl("SchoolNameInc")).Checked)
                    {
                        // CODE Translate
                        fromClause = Coalesce(fromClause, "left outer join Code C3 on C3.CID = p.SchoolName", " ");
                        retColumns = Coalesce(retColumns, "C3.Code as SchoolName", ",");
                    }


                    //if (((CheckBox)((DetailsView)sender).FindControl("SchoolNameInc")).Checked)
                    //    retColumns = Coalesce(retColumns, "SchoolName", ",");
                    //if (((CheckBox)((DetailsView)sender).FindControl("DistrictInc")).Checked)
                    //    retColumns = Coalesce(retColumns, "District", ",");

                    if (((CheckBox)((DetailsView)sender).FindControl("TeacherInc")).Checked)
                        retColumns = Coalesce(retColumns, "Teacher", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("GroupTeamNameInc")).Checked)
                        retColumns = Coalesce(retColumns, "GroupTeamName", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("SchoolTypeInc")).Checked)
                    {
                        //CODE Translate1
                        fromClause = Coalesce(fromClause, "left outer join Code C2 on C2.CID = p.SchoolType", " ");
                        retColumns = Coalesce(retColumns, "C2.Code as SchoolType", ",");
                    }

                    if (((CheckBox)((DetailsView)sender).FindControl("LiteracyLevel1Inc")).Checked)
                        retColumns = Coalesce(retColumns, "case when LiteracyLevel1 > 0 then LiteracyLevel1 else null end as LiteracyLevel1", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("LiteracyLevel2Inc")).Checked)
                        retColumns = Coalesce(retColumns, "case when LiteracyLevel2 > 0 then LiteracyLevel2 else null end as LiteracyLevel2", ",");

                    if (((CheckBox)((DetailsView)sender).FindControl("Custom1Inc")).Checked)
                        retColumns = Coalesce(retColumns, "Custom1", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("Custom2Inc")).Checked)
                        retColumns = Coalesce(retColumns, "Custom2", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("Custom3Inc")).Checked)
                        retColumns = Coalesce(retColumns, "Custom3", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("Custom4Inc")).Checked)
                        retColumns = Coalesce(retColumns, "Custom4", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("Custom5Inc")).Checked)
                        retColumns = Coalesce(retColumns, "Custom5", ",");
                    if (((CheckBox)((DetailsView)sender).FindControl("RegistrationDateInc")).Checked)
                        retColumns = Coalesce(retColumns, "convert(varchar, RegistrationDate, 101) as RegistrationDate", ",");


                    ProcessSimpleStringFilter((DetailsView)sender, "FirstName", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);
                    ProcessSimpleStringFilter((DetailsView)sender, "LastName", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);
                    ProcessSimpleStringFilter((DetailsView)sender, "SchoolGrade", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);
                    ProcessSimpleStringFilter((DetailsView)sender, "EmailAddress", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);
                    ProcessSimpleStringFilter((DetailsView)sender, "PhoneNumber", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);
                    ProcessSimpleStringFilter((DetailsView)sender, "City", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);
                    ProcessSimpleStringFilter((DetailsView)sender, "State", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);
                    ProcessSimpleStringFilter((DetailsView)sender, "ZipCode", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);
                    ProcessSimpleStringFilter((DetailsView)sender, "County", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);
                    ProcessSimpleStringFilter((DetailsView)sender, "Teacher", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);
                    ProcessSimpleStringFilter((DetailsView)sender, "GroupTeamName", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);

                    //ProcessSimpleStringFilter((DetailsView)sender, "District", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);
                    //ProcessSimpleStringFilter((DetailsView)sender, "SchoolName", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);


                    string txt;
                    string parmField;
                    int _int;
                    DateTime _dt;
                    string filterField;

                    //ProgID
                    fromClause = Coalesce(fromClause, "left outer join Programs PGM on PGM.PID = p.ProgId", " ");
                    retColumns = Coalesce(retColumns, "PGM.AdminName as Program", ",");
                    parmField = "ProgID";
                    txt = ((DropDownList)(((DetailsView)sender).FindControl(parmField))).SelectedValue;
                    _int = int.Parse(txt);
                    if (_int > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + parmField, _int));
                        whereClause = Coalesce(whereClause, string.Format("(p.{0} = @{1})", parmField, parmField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", "Program", minorSep, ((DropDownList)(((DetailsView)sender).FindControl(parmField))).SelectedItem.Text), majorSep);
                    }

                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                    txt = ((DropDownList)((DetailsView)sender).FindControl("Gender")).SelectedValue.Trim();
                    if (txt.Length > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + "Gender", txt));
                        whereClause = Coalesce(whereClause, string.Format("({0} = @{0})", "Gender"), " AND ");
                        //retColumns = Coalesce(retColumns, filterField, ",");
                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", "Gender", minorSep, ((DropDownList)(((DetailsView)sender).FindControl(parmField))).SelectedItem.Text), majorSep);
                    }

                    parmField = "PrimaryLibrary";
                    txt = ((DropDownList)(((DetailsView)sender).FindControl(parmField))).SelectedValue;
                    _int = txt.SafeToInt();
                    if (_int > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + parmField, _int));
                        whereClause = Coalesce(whereClause, string.Format("({0} = @{1})", parmField, parmField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", parmField, minorSep, ((DropDownList)(((DetailsView)sender).FindControl(parmField))).SelectedItem.Text), majorSep);
                    }

                    parmField = "District";
                    txt = ((DropDownList)(((DetailsView)sender).FindControl(parmField))).SelectedValue;
                    _int = txt.SafeToInt();
                    if (_int > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + parmField, _int));
                        whereClause = Coalesce(whereClause, string.Format("({0} = @{1})", parmField, parmField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", "Library District", minorSep, ((DropDownList)(((DetailsView)sender).FindControl(parmField))).SelectedItem.Text), majorSep);
                    }

                    parmField = "SDistrict";
                    txt = ((DropDownList)(((DetailsView)sender).FindControl(parmField))).SelectedValue;
                    _int = txt.SafeToInt();
                    if (_int > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + parmField, _int));
                        whereClause = Coalesce(whereClause, string.Format("({0} = @{1})", parmField, parmField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", "School District", minorSep, ((DropDownList)(((DetailsView)sender).FindControl(parmField))).SelectedItem.Text), majorSep);
                    }

                    parmField = "SchoolName";
                    txt = ((DropDownList)(((DetailsView)sender).FindControl(parmField))).SelectedValue;
                    _int = txt.SafeToInt();
                    if (_int > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + parmField, _int));
                        whereClause = Coalesce(whereClause, string.Format("({0} = @{1})", parmField, parmField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", "School Name", minorSep, ((DropDownList)(((DetailsView)sender).FindControl(parmField))).SelectedItem.Text), majorSep);
                    }


                    parmField = "SchoolType";
                    txt = ((DropDownList)(((DetailsView)sender).FindControl(parmField))).SelectedValue;
                    _int = txt.SafeToInt();
                    if (_int > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + parmField, _int));
                        whereClause = Coalesce(whereClause, string.Format("({0} = @{1})", parmField, parmField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", parmField, minorSep, ((DropDownList)(((DetailsView)sender).FindControl(parmField))).SelectedItem.Text), majorSep);
                    }

                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////


                    parmField = "LiteracyLevel1";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(parmField))).Text.Trim();
                    _int = txt.SafeToInt();
                    if (_int > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + parmField, _int));
                        whereClause = Coalesce(whereClause, string.Format("({0} = @{1})", parmField, parmField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1}like {2}", parmField, minorSep, txt), majorSep);
                    }

                    parmField = "LiteracyLevel2";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(parmField))).Text.Trim();
                    _int = txt.SafeToInt();
                    if (_int > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + parmField, _int));
                        whereClause = Coalesce(whereClause, string.Format("({0} = @{1})", parmField, parmField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1}like {2}", parmField, minorSep, txt), majorSep);
                    }


                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                    ProcessSimpleStringFilter((DetailsView)sender, "Custom1", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);
                    ProcessSimpleStringFilter((DetailsView)sender, "Custom2", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);
                    ProcessSimpleStringFilter((DetailsView)sender, "Custom3", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);
                    ProcessSimpleStringFilter((DetailsView)sender, "Custom4", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);
                    ProcessSimpleStringFilter((DetailsView)sender, "Custom5", ref whereClause, ref retColumns, ref arrParams, ref filterStr, minorSep, majorSep);

                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                    filterField = "Score1From";
                    parmField = "Score1";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, int.Parse(txt)));
                        whereClause = Coalesce(whereClause, string.Format("({0} >= @{1})", parmField, filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} >= {2}", parmField, minorSep, txt), majorSep);
                    }

                    filterField = "Score2From";
                    parmField = "Score2";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, int.Parse(txt)));
                        whereClause = Coalesce(whereClause, string.Format("({0} >= @{1})", parmField, filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} >= {2}", parmField, minorSep, txt), majorSep);
                    }

                    filterField = "Score1To";
                    parmField = "Score1";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, int.Parse(txt)));
                        whereClause = Coalesce(whereClause, string.Format("({0} <= @{1})", parmField, filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} <= {2}", parmField, minorSep, txt), majorSep);
                    }

                    filterField = "Score2To";
                    parmField = "Score2";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, int.Parse(txt)));
                        whereClause = Coalesce(whereClause, string.Format("({0} <= @{1})", parmField, filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} <= {2}", parmField, minorSep, txt), majorSep);
                    }


                    filterField = "Score1PctFrom";
                    parmField = "Score1Pct";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, int.Parse(txt)));
                        whereClause = Coalesce(whereClause, string.Format("({0} >= @{1})", parmField, filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} >= {2}", parmField, minorSep, txt), majorSep);
                    }

                    filterField = "Score2PctFrom";
                    parmField = "Score2Pct";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, int.Parse(txt)));
                        whereClause = Coalesce(whereClause, string.Format("({0} >= @{1})", parmField, filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} >= {2}", parmField, minorSep, txt), majorSep);
                    }

                    filterField = "Score1PctTo";
                    parmField = "Score1Pct";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, int.Parse(txt)));
                        whereClause = Coalesce(whereClause, string.Format("({0} <= @{1})", parmField, filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} <= {2}", parmField, minorSep, txt), majorSep);
                    }

                    filterField = "Score2PctTo";
                    parmField = "Score2Pct";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, int.Parse(txt)));
                        whereClause = Coalesce(whereClause, string.Format("({0} <= @{1})", parmField, filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} <= {2}", parmField, minorSep, txt), majorSep);
                    }
                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////


                    filterField = "AgeFrom";
                    parmField = "Age";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, int.Parse(txt)));
                        whereClause = Coalesce(whereClause, string.Format("({0} >= @{1})", parmField, filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1}like {2}", filterField, minorSep, txt), majorSep);
                    }

                    filterField = "AgeTo";
                    parmField = "Age";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, int.Parse(txt)));
                        whereClause = Coalesce(whereClause, string.Format("({0} <= @{1})", parmField, filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1}like {2}", filterField, minorSep, txt), majorSep);
                    }

                    filterField = "DOBFrom";
                    parmField = "DOB";
                    _dt = FormatHelper.SafeToDateTime(((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim());
                    if (_dt != DateTime.MinValue)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, _dt));
                        whereClause = Coalesce(whereClause, string.Format("({0} >= @{1})", parmField, filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1}like {2}", filterField, minorSep, txt), majorSep);
                    }

                    filterField = "DOBTo";
                    parmField = "DOB";
                    _dt = FormatHelper.SafeToDateTime(((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim());
                    if (_dt != DateTime.MinValue)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, _dt));
                        whereClause = Coalesce(whereClause, string.Format("({0} <= @{1})", parmField, filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1}like {2}", filterField, minorSep, txt), majorSep);
                    }


                    filterField = "RegistrationDateStart";
                    parmField = "RegistrationDate";
                    _dt = FormatHelper.SafeToDateTime(((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim());
                    if (_dt != DateTime.MinValue)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, _dt));
                        whereClause = Coalesce(whereClause, string.Format("({0} >= @{1})", parmField, filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1}like {2}", filterField, minorSep, txt), majorSep);
                    }

                    filterField = "RegistrationDateEnd";
                    parmField = "RegistrationDate";
                    _dt = FormatHelper.SafeToDateTime(((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim());
                    if (_dt != DateTime.MinValue)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, _dt));
                        whereClause = Coalesce(whereClause, string.Format("({0} <= @{1})", parmField, filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1}like {2}", filterField, minorSep, txt), majorSep);
                    }

                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    var needPointsDate = false;
                    if (((CheckBox)((DetailsView)sender).FindControl("PointsInc")).Checked)
                    {
                        retColumns = Coalesce(retColumns, "(select SUM(convert(BIGINT, NumPoints)) from PatronPoints pp where pp.PID = p.PID AND (@PointsStart is null or pp.AwardDate >= @PointsStart) and (@PointsEnd is null or pp.AwardDate <= @PointsEnd)) as NumPoints", ",");
                        needPointsDate = true;
                    }

                    filterField = "PointsMin";
                    parmField = "(select SUM(convert(BIGINT, NumPoints)) from PatronPoints pp where pp.PID = p.PID AND (@PointsStart is null or pp.AwardDate >= @PointsStart) and (@PointsEnd is null or pp.AwardDate <= @PointsEnd))";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, int.Parse(txt)));
                        whereClause = Coalesce(whereClause, string.Format("({0} >= @{1} or @{1} is null)", parmField, filterField), " AND ");
                        needPointsDate = true;

                        filterStr = Coalesce(filterStr, string.Format("{0}{1}like {2}", filterField, minorSep, txt), majorSep);
                    }

                    filterField = "PointsMax";
                    parmField = "(select SUM(convert(BIGINT, NumPoints)) from PatronPoints pp where pp.PID = p.PID AND (@PointsStart is null or pp.AwardDate >= @PointsStart) and (@PointsEnd is null or pp.AwardDate <= @PointsEnd))";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, int.Parse(txt)));
                        whereClause = Coalesce(whereClause, string.Format("({0} <= @{1} or @{1} is null)", parmField, filterField), " AND ");
                        needPointsDate = true;

                        filterStr = Coalesce(filterStr, string.Format("{0}{1}like {2}", filterField, minorSep, txt), majorSep);
                    }


                    filterField = "PointsStart";
                    _dt = FormatHelper.SafeToDateTime(((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim());
                    if (_dt != DateTime.MinValue)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, _dt));

                        filterStr = Coalesce(filterStr, string.Format("{0}{1}like {2}", filterField, minorSep, ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim()), majorSep);
                    }
                    else if (needPointsDate)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, DBNull.Value));
                    }

                    filterField = "PointsEnd";
                    _dt = FormatHelper.SafeToDateTime(((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim());
                    if (_dt != DateTime.MinValue)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, _dt));

                        filterStr = Coalesce(filterStr, string.Format("{0}{1}like {2}", filterField, minorSep, ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim()), majorSep);
                    }
                    else if (needPointsDate)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, DBNull.Value));
                    }


                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////

                    filterField = "EventCode";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        retColumns = Coalesce(retColumns, "pp1.EventCode", ",");
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, txt));
                        whereClause = Coalesce(whereClause, string.Format("(pp1.{0} like '%' + @{1} + '%')", filterField, filterField), " AND ");
                        fromClause = Coalesce(fromClause, "left join PatronPoints pp1 on p.PID = pp1.PID", " ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} like {2}", filterField, minorSep, txt), majorSep);
                    }




                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////

                    var needBadges = false;
                    if (((CheckBox)((DetailsView)sender).FindControl("EarnedBadgeInc")).Checked)
                    {
                        needBadges = true;
                        retColumns = Coalesce(retColumns, "b.AdminName as BadgeName", ",");
                    }

                    filterField = "EarnedBadge";
                    txt = ((DropDownList)(((DetailsView)sender).FindControl(filterField))).SelectedValue;
                    _int = int.Parse(txt);
                    if (_int > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, _int));
                        whereClause = Coalesce(whereClause, string.Format("({0} = @{1})", "pb.BadgeId", filterField), " AND ");
                        needBadges = true;

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", filterField, minorSep, ((DropDownList)(((DetailsView)sender).FindControl(filterField))).SelectedItem.Text), majorSep);
                    }

                    if (needBadges)
                    {
                        fromClause = Coalesce(fromClause, "left join PatronBadges pb on pb.PID = p.PID left outer join Badge b on pb.BadgeID = b.BID", " ");
                    }

                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////

                    var needPrize = false;
                    if (((CheckBox)((DetailsView)sender).FindControl("PhysicalPrizeNameInc")).Checked)
                    {
                        needPrize = true;
                        retColumns = Coalesce(retColumns, "pr.PrizeName", ",");
                        retColumns = Coalesce(retColumns, "pr.RedeemedFlag", ",");
                    }

                    filterField = "PhysicalPrizeEarned";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        needPrize = true;

                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, txt));
                        whereClause = Coalesce(whereClause, string.Format("({0} like '%' + @{1} + '%')", "pr.PrizeName", filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1}like {2}", "PrizeName", minorSep, txt), majorSep);

                        filterField = "PhysicalPrizeRedeemed";
                        var chk = ((CheckBox)(((DetailsView)sender).FindControl(filterField))).Checked;

                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, (chk ? 1 : 0)));
                        whereClause = Coalesce(whereClause, string.Format("(pr.RedeemedFlag = @{0})", filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", "Redeemed", minorSep, (chk ? 1 : 0)), majorSep);
                    }

                    if (((CheckBox)((DetailsView)sender).FindControl("PhysicalPrizeDateInc")).Checked)
                    {
                        needPrize = true;
                        retColumns = Coalesce(retColumns, "pr.AddedDate as PrizeAwardDate", ",");
                    }

                    filterField = "PhysicalPrizeStartDate";
                    _dt = FormatHelper.SafeToDateTime(((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim());
                    if (_dt != DateTime.MinValue)
                    {
                        needPrize = true;
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, _dt));
                        whereClause = Coalesce(whereClause, string.Format("(pr.AddedDate >= @{0} or @{0} is null)", filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", filterField, minorSep, ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim()), majorSep);
                    }

                    filterField = "PhysicalPrizeEndDate";
                    _dt = FormatHelper.SafeToDateTime(((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim());
                    if (_dt != DateTime.MinValue)
                    {
                        needPrize = true;
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, _dt));
                        whereClause = Coalesce(whereClause, string.Format("(pr.AddedDate <= @{0} or @{0} is null)", filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", filterField, minorSep, ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim()), majorSep);

                    }

                    if (needPrize)
                    {
                        fromClause = Coalesce(fromClause, "left join PatronPrizes pr on p.PID = pr.PID", " ");
                    }

                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////

                    var needReview = false;
                    var needReviewDates = false;
                    if (((CheckBox)((DetailsView)sender).FindControl("NumReviewsInc")).Checked)
                    {
                        needReview = true;
                        retColumns = Coalesce(retColumns, "(select Count(*) from PatronReview pr where pr.PID = p.PID AND (@ReviewStartDate is null or pr.ReviewDate >= @ReviewStartDate) and (@ReviewEndDate is null or pr.ReviewDate <= @ReviewEndDate)) as NumReviews", ",");
                        needReviewDates = true;

                    }

                    filterField = "ReviewsMin";
                    parmField = "(select Count(*) from PatronReview pr where pr.PID = p.PID AND (@ReviewStartDate is null or pr.ReviewDate >= @ReviewStartDate) and (@ReviewEndDate is null or pr.ReviewDate <= @ReviewEndDate))";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, int.Parse(txt)));
                        whereClause = Coalesce(whereClause, string.Format("({0} >= @{1} or @{1} is null)", parmField, filterField), " AND ");
                        needReviewDates = true;

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", filterField, minorSep, txt), majorSep);
                    }

                    filterField = "ReviewsMax";
                    parmField = "(select Count(*) from PatronReview pr where pr.PID = p.PID AND (@ReviewStartDate is null or pr.ReviewDate >= @ReviewStartDate) and (@ReviewEndDate is null or pr.ReviewDate <= @ReviewEndDate))";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, int.Parse(txt)));
                        whereClause = Coalesce(whereClause, string.Format("({0} <= @{1} or @{1} is null)", parmField, filterField), " AND ");
                        needReviewDates = true;

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", filterField, minorSep, txt), majorSep);
                    }

                    if (((CheckBox)((DetailsView)sender).FindControl("ReviewTitleInc")).Checked)
                    {
                        needReview = true;
                        retColumns = Coalesce(retColumns, "patr.Title", ",");
                    }

                    filterField = "ReviewTitle";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        needReview = true;
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, txt));
                        whereClause = Coalesce(whereClause, string.Format("(patr.Title like '%' + @{0} + '%')", filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} like {2}", filterField, minorSep, txt), majorSep);
                    }

                    if (((CheckBox)((DetailsView)sender).FindControl("ReviewAuthorInc")).Checked)
                    {
                        needReview = true;
                        retColumns = Coalesce(retColumns, "patr.Author", ",");
                    }

                    filterField = "ReviewAuthor";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        needReview = true;
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, txt));
                        whereClause = Coalesce(whereClause, string.Format("(patr.Author like '%' + @{0} + '%')", filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} like {2}", filterField, minorSep, txt), majorSep);
                    }


                    if (((CheckBox)((DetailsView)sender).FindControl("ReviewDateInc")).Checked)
                    {
                        needReview = true;
                        retColumns = Coalesce(retColumns, "convert(varchar, patr.ReviewDate, 101) as ReviewDate", ",");
                    }

                    filterField = "ReviewStartDate";
                    _dt = FormatHelper.SafeToDateTime(((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim());
                    if (_dt != DateTime.MinValue)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, _dt));
                        whereClause = Coalesce(whereClause, string.Format("(@{0} is null or patr.ReviewDate >= @{0})", filterField), " AND ");
                        needReview = true;

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", filterField, minorSep, ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim()), majorSep);
                    }
                    else if (needReviewDates)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, DBNull.Value));
                        needReview = true;
                    }

                    filterField = "ReviewEndDate";
                    _dt = FormatHelper.SafeToDateTime(((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim());
                    if (_dt != DateTime.MinValue)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, _dt));
                        whereClause = Coalesce(whereClause, string.Format("(@{0} is null or patr.ReviewDate <= @{0})", filterField), " AND ");
                        needReview = true;

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", filterField, minorSep, ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim()), majorSep);
                    }
                    else if (needReviewDates)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, DBNull.Value));
                        needReview = true;
                    }


                    if (needReview)
                    {
                        fromClause = Coalesce(fromClause, "left join PatronReview patr on patr.PID = p.PID", " ");
                    }

                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////

                    var needDrawing = false;
                    //var needDrawingDates = false;
                    if (((CheckBox)((DetailsView)sender).FindControl("RandomDrawingNameInc")).Checked)
                    {
                        needDrawing = true;
                        retColumns = Coalesce(retColumns, "pdr.PrizeName as RandomDrawingName", ",");
                    }
                    filterField = "RandomDrawingName";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim();
                    if (txt.Length > 0)
                    {
                        needReview = true;
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, txt));
                        whereClause = Coalesce(whereClause, string.Format("(pdr.PrizeName like '%' + @{0} + '%')", filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", filterField, minorSep, txt), majorSep);
                    }

                    if (((CheckBox)((DetailsView)sender).FindControl("RandomDrawingNumInc")).Checked)
                    {
                        needDrawing = true;
                        retColumns = Coalesce(retColumns, "pdr.PDID as RandomDrawingNum", ",");
                    }
                    parmField = "RandomDrawingNum";
                    txt = ((TextBox)(((DetailsView)sender).FindControl(parmField))).Text.Trim();
                    _int = txt.SafeToInt();
                    if (_int > 0)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + parmField, _int));
                        whereClause = Coalesce(whereClause, string.Format("({0} = @{1})", "pdr.PDID", parmField), " AND ");
                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", filterField, minorSep, txt), majorSep);
                    }

                    if (((CheckBox)((DetailsView)sender).FindControl("HasRedeemendInc")).Checked)
                    {
                        needDrawing = true;
                        retColumns = Coalesce(retColumns, "pdw.PrizePickedUpFlag as UserRedeemedPrize", ",");

                        filterField = "HasBeenDrawn";
                        var chk = ((CheckBox)(((DetailsView)sender).FindControl(filterField))).Checked;

                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, (chk ? 1 : 0)));
                        whereClause = Coalesce(whereClause, string.Format("(pdw.PrizePickedUpFlag = @{0})", filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", filterField, minorSep, (chk ? 1 : 0)), majorSep);
                    }


                    if (((CheckBox)((DetailsView)sender).FindControl("HasBeenDrawnInc")).Checked)
                    {
                        needDrawing = true;
                        retColumns = Coalesce(retColumns, "'Yes' as UserHasBeenDrawn", ",");
                    }

                    if (((CheckBox)((DetailsView)sender).FindControl("RandomDrawingDateInc")).Checked)
                    {
                        needDrawing = true;
                        retColumns = Coalesce(retColumns, "convert(varchar, pdr.DrawingDateTime, 101) as RandomDrawingDate", ",");
                    }

                    filterField = "RandomDrawingStartDate";
                    parmField = "pdr.DrawingDateTime";
                    _dt = FormatHelper.SafeToDateTime(((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim());
                    if (_dt != DateTime.MinValue)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, _dt));
                        whereClause = Coalesce(whereClause, string.Format("({0} >= @{1})", parmField, filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", filterField, minorSep, ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim()), majorSep);
                    }

                    filterField = "RandomDrawingEndDate";
                    parmField = "pdr.DrawingDateTime";
                    _dt = FormatHelper.SafeToDateTime(((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim());
                    if (_dt != DateTime.MinValue)
                    {
                        AddSqlParameter(ref arrParams, new SqlParameter("@" + filterField, _dt));
                        whereClause = Coalesce(whereClause, string.Format("({0} <= @{1})", parmField, filterField), " AND ");

                        filterStr = Coalesce(filterStr, string.Format("{0}{1} = {2}", filterField, minorSep, ((TextBox)(((DetailsView)sender).FindControl(filterField))).Text.Trim()), majorSep);
                    }

                    if (needDrawing)
                    {
                        fromClause = Coalesce(fromClause, "left outer join PrizeDrawingWinners pdw on pdw.PatronId = p.PID left outer join PrizeDrawing pdr on pdw.PDID = pdr.PDID", " ");
                    }
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////

                    if (retColumns.Length == 0)
                    {
                        var masterPage = (IControlRoomMaster)Master;
                        masterPage.PageError = String.Format("You must select the columns to display.");
                        return;
                    }

                    AddSqlParameter(ref arrParams, new SqlParameter("@TenID", CRTenantID == null ? -1 : CRTenantID));
                    whereClause = Coalesce(whereClause, "(p.TenID = @TenID)", " AND ");

                    // Should this be DISTINCT ?????????
                    var SQL = "SELECT DISTINCT " + retColumns + " FROM " + fromClause;
                    if (whereClause.Length > 0) SQL = SQL + " WHERE " + whereClause;







                    //lblSQl.Text = SQL;
                    var ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, SQL, arrParams);

                    Session["rptSql"] = ds;


                    //gv.DataSource = ds;
                    //gv.DataBind();

                    parmField = "ReportFormat";
                    txt = ((DropDownList)(((DetailsView)sender).FindControl(parmField))).SelectedValue;
                    _int = int.Parse(txt);
                    if (_int == 0)
                    {
                        string path = "HTMLReportResults.aspx";
                        Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "var win = window.open('" + path + "','',''); if (win == null || typeof(win)=='undefined') {alert('Turn off your pop-up blocker!');}", true);
                        //Response.Redirect("HTMLReportResults.aspx");

                        Session["rptFilter"] = filterStr.Replace(minorSep, " ").Replace(majorSep, "<br/>");
                    }
                    if (_int == 2)
                    {
                        string excelFilename = Server.MapPath("~/Adhocreport.xlsx");
                        //CreateExcelFile.CreateExcelDocument(ds, excelFilename, filterStr.Replace(minorSep, " ").Replace(majorSep, "\n"));
                        CreateExcelFile.CreateExcelDocument(ds, excelFilename);

                        Response.ContentType = "application/vnd.ms-excel";
                        Response.AppendHeader("Content-Disposition", "attachment; filename=Adhocreport.xlsx");
                        EnableViewState = false;
                        Response.BinaryWrite(File.ReadAllBytes(excelFilename));
                        File.Delete(excelFilename);
                        Response.End();
                    }
                }
                catch (Exception ex)
                {
                    var masterPage = (IControlRoomMaster)Master;
                    masterPage.PageError = String.Format(SRPResources.ApplicationError1, ex.Message);
                }
            }




            #endregion
        }
        public static int Delete(ReportTemplate o)
        {

            int iReturn = -1; //assume the worst

            SqlParameter[] arrParams = new SqlParameter[1];

            arrParams[0] = new SqlParameter("@RTID", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.RTID, o.RTID.GetTypeCode()));

            try
            {

                iReturn = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "app_ReportTemplate_Delete", arrParams);

            }

            catch (SqlException exx)
            {

                System.Diagnostics.Debug.Write(exx.Message);

            }

            return iReturn;

        }
        public bool Fetch(int RTID)
        {

            // declare reader

            SqlDataReader dr;

            SqlParameter[] arrParams = new SqlParameter[1];

            arrParams[0] = new SqlParameter("@RTID", RTID);

            dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "app_ReportTemplate_GetByID", arrParams);

            if (dr.Read())
            {

                // declare return value

                ReportTemplate result = new ReportTemplate();

                DateTime _datetime;

                int _int;

                //decimal _decimal;

                if (int.TryParse(dr["RTID"].ToString(), out _int)) this.RTID = _int;
                if (int.TryParse(dr["ProgId"].ToString(), out _int)) this.ProgId = _int;
                this.ReportName = dr["ReportName"].ToString();
                this.DisplayFilters = bool.Parse(dr["DisplayFilters"].ToString());
                if (DateTime.TryParse(dr["DOBFrom"].ToString(), out _datetime)) this.DOBFrom = _datetime;
                if (DateTime.TryParse(dr["DOBTo"].ToString(), out _datetime)) this.DOBTo = _datetime;
                if (int.TryParse(dr["AgeFrom"].ToString(), out _int)) this.AgeFrom = _int;
                if (int.TryParse(dr["AgeTo"].ToString(), out _int)) this.AgeTo = _int;
                this.SchoolGrade = dr["SchoolGrade"].ToString();
                this.FirstName = dr["FirstName"].ToString();
                this.LastName = dr["LastName"].ToString();
                this.Gender = dr["Gender"].ToString();
                this.EmailAddress = dr["EmailAddress"].ToString();
                this.PhoneNumber = dr["PhoneNumber"].ToString();
                this.City = dr["City"].ToString();
                this.State = dr["State"].ToString();
                this.ZipCode = dr["ZipCode"].ToString();
                this.County = dr["County"].ToString();
                if (int.TryParse(dr["PrimaryLibrary"].ToString(), out _int)) this.PrimaryLibrary = _int;
                this.SchoolName = dr["SchoolName"].ToString();
                this.District = dr["District"].ToString();
                this.SDistrict = dr["SDistrict"].ToString();
                this.Teacher = dr["Teacher"].ToString();
                this.GroupTeamName = dr["GroupTeamName"].ToString();
                if (int.TryParse(dr["SchoolType"].ToString(), out _int)) this.SchoolType = _int;
                if (int.TryParse(dr["LiteracyLevel1"].ToString(), out _int)) this.LiteracyLevel1 = _int;
                if (int.TryParse(dr["LiteracyLevel2"].ToString(), out _int)) this.LiteracyLevel2 = _int;
                this.Custom1 = dr["Custom1"].ToString();
                this.Custom2 = dr["Custom2"].ToString();
                this.Custom3 = dr["Custom3"].ToString();
                this.Custom4 = dr["Custom4"].ToString();
                this.Custom5 = dr["Custom5"].ToString();
                if (DateTime.TryParse(dr["RegistrationDateStart"].ToString(), out _datetime)) this.RegistrationDateStart = _datetime;
                if (DateTime.TryParse(dr["RegistrationDateEnd"].ToString(), out _datetime)) this.RegistrationDateEnd = _datetime;
                if (int.TryParse(dr["PointsMin"].ToString(), out _int)) this.PointsMin = _int;
                if (int.TryParse(dr["PointsMax"].ToString(), out _int)) this.PointsMax = _int;
                if (DateTime.TryParse(dr["PointsStart"].ToString(), out _datetime)) this.PointsStart = _datetime;
                if (DateTime.TryParse(dr["PointsEnd"].ToString(), out _datetime)) this.PointsEnd = _datetime;
                this.EventCode = dr["EventCode"].ToString();
                if (int.TryParse(dr["EarnedBadge"].ToString(), out _int)) this.EarnedBadge = _int;
                this.PhysicalPrizeEarned = dr["PhysicalPrizeEarned"].ToString();
                this.PhysicalPrizeRedeemed = bool.Parse(dr["PhysicalPrizeRedeemed"].ToString());
                if (DateTime.TryParse(dr["PhysicalPrizeStartDate"].ToString(), out _datetime)) this.PhysicalPrizeStartDate = _datetime;
                if (DateTime.TryParse(dr["PhysicalPrizeEndDate"].ToString(), out _datetime)) this.PhysicalPrizeEndDate = _datetime;
                if (int.TryParse(dr["ReviewsMin"].ToString(), out _int)) this.ReviewsMin = _int;
                if (int.TryParse(dr["ReviewsMax"].ToString(), out _int)) this.ReviewsMax = _int;
                this.ReviewTitle = dr["ReviewTitle"].ToString();
                this.ReviewAuthor = dr["ReviewAuthor"].ToString();
                if (DateTime.TryParse(dr["ReviewStartDate"].ToString(), out _datetime)) this.ReviewStartDate = _datetime;
                if (DateTime.TryParse(dr["ReviewEndDate"].ToString(), out _datetime)) this.ReviewEndDate = _datetime;
                this.RandomDrawingName = dr["RandomDrawingName"].ToString();
                if (int.TryParse(dr["RandomDrawingNum"].ToString(), out _int)) this.RandomDrawingNum = _int;
                if (DateTime.TryParse(dr["RandomDrawingStartDate"].ToString(), out _datetime)) this.RandomDrawingStartDate = _datetime;
                if (DateTime.TryParse(dr["RandomDrawingEndDate"].ToString(), out _datetime)) this.RandomDrawingEndDate = _datetime;
                this.HasBeenDrawn = bool.Parse(dr["HasBeenDrawn"].ToString());
                this.HasRedeemend = bool.Parse(dr["HasRedeemend"].ToString());
                this.PIDInc = bool.Parse(dr["PIDInc"].ToString());
                this.UsernameInc = bool.Parse(dr["UsernameInc"].ToString());
                this.DOBInc = bool.Parse(dr["DOBInc"].ToString());
                this.AgeInc = bool.Parse(dr["AgeInc"].ToString());
                this.SchoolGradeInc = bool.Parse(dr["SchoolGradeInc"].ToString());
                this.FirstNameInc = bool.Parse(dr["FirstNameInc"].ToString());
                this.LastNameInc = bool.Parse(dr["LastNameInc"].ToString());
                this.GenderInc = bool.Parse(dr["GenderInc"].ToString());
                this.EmailAddressInc = bool.Parse(dr["EmailAddressInc"].ToString());
                this.PhoneNumberInc = bool.Parse(dr["PhoneNumberInc"].ToString());
                this.CityInc = bool.Parse(dr["CityInc"].ToString());
                this.StateInc = bool.Parse(dr["StateInc"].ToString());
                this.ZipCodeInc = bool.Parse(dr["ZipCodeInc"].ToString());
                this.CountyInc = bool.Parse(dr["CountyInc"].ToString());
                this.PrimaryLibraryInc = bool.Parse(dr["PrimaryLibraryInc"].ToString());
                this.SchoolNameInc = bool.Parse(dr["SchoolNameInc"].ToString());
                this.DistrictInc = bool.Parse(dr["DistrictInc"].ToString());
                this.SDistrictInc = bool.Parse(dr["SDistrictInc"].ToString());
                this.TeacherInc = bool.Parse(dr["TeacherInc"].ToString());
                this.GroupTeamNameInc = bool.Parse(dr["GroupTeamNameInc"].ToString());
                this.SchoolTypeInc = bool.Parse(dr["SchoolTypeInc"].ToString());
                this.LiteracyLevel1Inc = bool.Parse(dr["LiteracyLevel1Inc"].ToString());
                this.LiteracyLevel2Inc = bool.Parse(dr["LiteracyLevel2Inc"].ToString());
                this.Custom1Inc = bool.Parse(dr["Custom1Inc"].ToString());
                this.Custom2Inc = bool.Parse(dr["Custom2Inc"].ToString());
                this.Custom3Inc = bool.Parse(dr["Custom3Inc"].ToString());
                this.Custom4Inc = bool.Parse(dr["Custom4Inc"].ToString());
                this.Custom5Inc = bool.Parse(dr["Custom5Inc"].ToString());
                this.RegistrationDateInc = bool.Parse(dr["RegistrationDateInc"].ToString());
                this.PointsInc = bool.Parse(dr["PointsInc"].ToString());
                this.EarnedBadgeInc = bool.Parse(dr["EarnedBadgeInc"].ToString());
                this.PhysicalPrizeNameInc = bool.Parse(dr["PhysicalPrizeNameInc"].ToString());
                this.PhysicalPrizeDateInc = bool.Parse(dr["PhysicalPrizeDateInc"].ToString());
                this.NumReviewsInc = bool.Parse(dr["NumReviewsInc"].ToString());
                this.ReviewAuthorInc = bool.Parse(dr["ReviewAuthorInc"].ToString());
                this.ReviewTitleInc = bool.Parse(dr["ReviewTitleInc"].ToString());
                this.ReviewDateInc = bool.Parse(dr["ReviewDateInc"].ToString());
                this.RandomDrawingNameInc = bool.Parse(dr["RandomDrawingNameInc"].ToString());
                this.RandomDrawingNumInc = bool.Parse(dr["RandomDrawingNumInc"].ToString());
                this.RandomDrawingDateInc = bool.Parse(dr["RandomDrawingDateInc"].ToString());
                this.HasBeenDrawnInc = bool.Parse(dr["HasBeenDrawnInc"].ToString());
                this.HasRedeemendInc = bool.Parse(dr["HasRedeemendInc"].ToString());
                if (DateTime.TryParse(dr["LastModDate"].ToString(), out _datetime)) this.LastModDate = _datetime;
                this.LastModUser = dr["LastModUser"].ToString();
                if (DateTime.TryParse(dr["AddedDate"].ToString(), out _datetime)) this.AddedDate = _datetime;
                this.AddedUser = dr["AddedUser"].ToString();

                if (int.TryParse(dr["TenID"].ToString(), out _int)) this.TenID = _int;
                if (int.TryParse(dr["FldInt1"].ToString(), out _int)) this.FldInt1 = _int;
                if (int.TryParse(dr["FldInt2"].ToString(), out _int)) this.FldInt2 = _int;
                if (int.TryParse(dr["FldInt3"].ToString(), out _int)) this.FldInt3 = _int;
                this.FldBit1 = bool.Parse(dr["FldBit1"].ToString());
                this.FldBit2 = bool.Parse(dr["FldBit2"].ToString());
                this.FldBit3 = bool.Parse(dr["FldBit3"].ToString());
                this.FldText1 = dr["FldText1"].ToString();
                this.FldText2 = dr["FldText2"].ToString();
                this.FldText3 = dr["FldText3"].ToString();

                if (int.TryParse(dr["Score1From"].ToString(), out _int)) this.Score1From = _int;
                if (int.TryParse(dr["Score1To"].ToString(), out _int)) this.Score1To = _int;
                if (int.TryParse(dr["Score1PctFrom"].ToString(), out _int)) this.Score1PctFrom = _int;
                if (int.TryParse(dr["Score1PctTo"].ToString(), out _int)) this.Score1PctTo = _int;
                if (int.TryParse(dr["Score2From"].ToString(), out _int)) this.Score2From = _int;
                if (int.TryParse(dr["Score2To"].ToString(), out _int)) this.Score2To = _int;
                if (int.TryParse(dr["Score2PctFrom"].ToString(), out _int)) this.Score2PctFrom = _int;
                if (int.TryParse(dr["Score2PctTo"].ToString(), out _int)) this.Score2PctTo = _int;

                this.Score1Inc = bool.Parse(dr["Score1Inc"].ToString());
                this.Score2Inc = bool.Parse(dr["Score2Inc"].ToString());
                this.Score1PctInc = bool.Parse(dr["Score1PctInc"].ToString());
                this.Score2PctInc = bool.Parse(dr["Score2PctInc"].ToString());

                dr.Close();

                return true;

            }

            dr.Close();

            return false;

        }
        public static int Update(ReportTemplate o)
        {

            int iReturn = -1; //assume the worst

            SqlParameter[] arrParams = new SqlParameter[124];

            arrParams[0] = new SqlParameter("@RTID", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.RTID, o.RTID.GetTypeCode()));
            arrParams[1] = new SqlParameter("@ProgId", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.ProgId, o.ProgId.GetTypeCode()));
            arrParams[2] = new SqlParameter("@ReportName", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.ReportName, o.ReportName.GetTypeCode()));
            arrParams[3] = new SqlParameter("@DisplayFilters", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.DisplayFilters, o.DisplayFilters.GetTypeCode()));
            arrParams[4] = new SqlParameter("@DOBFrom", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.DOBFrom, o.DOBFrom.GetTypeCode()));
            arrParams[5] = new SqlParameter("@DOBTo", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.DOBTo, o.DOBTo.GetTypeCode()));
            arrParams[6] = new SqlParameter("@AgeFrom", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.AgeFrom, o.AgeFrom.GetTypeCode()));
            arrParams[7] = new SqlParameter("@AgeTo", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.AgeTo, o.AgeTo.GetTypeCode()));
            arrParams[8] = new SqlParameter("@SchoolGrade", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.SchoolGrade, o.SchoolGrade.GetTypeCode()));
            arrParams[9] = new SqlParameter("@FirstName", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FirstName, o.FirstName.GetTypeCode()));
            arrParams[10] = new SqlParameter("@LastName", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.LastName, o.LastName.GetTypeCode()));
            arrParams[11] = new SqlParameter("@Gender", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Gender, o.Gender.GetTypeCode()));
            arrParams[12] = new SqlParameter("@EmailAddress", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.EmailAddress, o.EmailAddress.GetTypeCode()));
            arrParams[13] = new SqlParameter("@PhoneNumber", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PhoneNumber, o.PhoneNumber.GetTypeCode()));
            arrParams[14] = new SqlParameter("@City", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.City, o.City.GetTypeCode()));
            arrParams[15] = new SqlParameter("@State", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.State, o.State.GetTypeCode()));
            arrParams[16] = new SqlParameter("@ZipCode", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.ZipCode, o.ZipCode.GetTypeCode()));
            arrParams[17] = new SqlParameter("@County", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.County, o.County.GetTypeCode()));
            arrParams[18] = new SqlParameter("@PrimaryLibrary", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PrimaryLibrary, o.PrimaryLibrary.GetTypeCode()));
            arrParams[19] = new SqlParameter("@SchoolName", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.SchoolName, o.SchoolName.GetTypeCode()));
            arrParams[20] = new SqlParameter("@District", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.District, o.District.GetTypeCode()));
            arrParams[21] = new SqlParameter("@Teacher", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Teacher, o.Teacher.GetTypeCode()));
            arrParams[22] = new SqlParameter("@GroupTeamName", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.GroupTeamName, o.GroupTeamName.GetTypeCode()));
            arrParams[23] = new SqlParameter("@SchoolType", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.SchoolType, o.SchoolType.GetTypeCode()));
            arrParams[24] = new SqlParameter("@LiteracyLevel1", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.LiteracyLevel1, o.LiteracyLevel1.GetTypeCode()));
            arrParams[25] = new SqlParameter("@LiteracyLevel2", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.LiteracyLevel2, o.LiteracyLevel2.GetTypeCode()));
            arrParams[26] = new SqlParameter("@Custom1", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Custom1, o.Custom1.GetTypeCode()));
            arrParams[27] = new SqlParameter("@Custom2", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Custom2, o.Custom2.GetTypeCode()));
            arrParams[28] = new SqlParameter("@Custom3", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Custom3, o.Custom3.GetTypeCode()));
            arrParams[29] = new SqlParameter("@Custom4", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Custom4, o.Custom4.GetTypeCode()));
            arrParams[30] = new SqlParameter("@Custom5", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Custom5, o.Custom5.GetTypeCode()));
            arrParams[31] = new SqlParameter("@RegistrationDateStart", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.RegistrationDateStart, o.RegistrationDateStart.GetTypeCode()));
            arrParams[32] = new SqlParameter("@RegistrationDateEnd", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.RegistrationDateEnd, o.RegistrationDateEnd.GetTypeCode()));
            arrParams[33] = new SqlParameter("@PointsMin", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PointsMin, o.PointsMin.GetTypeCode()));
            arrParams[34] = new SqlParameter("@PointsMax", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PointsMax, o.PointsMax.GetTypeCode()));
            arrParams[35] = new SqlParameter("@PointsStart", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PointsStart, o.PointsStart.GetTypeCode()));
            arrParams[36] = new SqlParameter("@PointsEnd", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PointsEnd, o.PointsEnd.GetTypeCode()));
            arrParams[37] = new SqlParameter("@EventCode", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.EventCode, o.EventCode.GetTypeCode()));
            arrParams[38] = new SqlParameter("@EarnedBadge", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.EarnedBadge, o.EarnedBadge.GetTypeCode()));
            arrParams[39] = new SqlParameter("@PhysicalPrizeEarned", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PhysicalPrizeEarned, o.PhysicalPrizeEarned.GetTypeCode()));
            arrParams[40] = new SqlParameter("@PhysicalPrizeRedeemed", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PhysicalPrizeRedeemed, o.PhysicalPrizeRedeemed.GetTypeCode()));
            arrParams[41] = new SqlParameter("@PhysicalPrizeStartDate", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PhysicalPrizeStartDate, o.PhysicalPrizeStartDate.GetTypeCode()));
            arrParams[42] = new SqlParameter("@PhysicalPrizeEndDate", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PhysicalPrizeEndDate, o.PhysicalPrizeEndDate.GetTypeCode()));
            arrParams[43] = new SqlParameter("@ReviewsMin", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.ReviewsMin, o.ReviewsMin.GetTypeCode()));
            arrParams[44] = new SqlParameter("@ReviewsMax", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.ReviewsMax, o.ReviewsMax.GetTypeCode()));
            arrParams[45] = new SqlParameter("@ReviewTitle", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.ReviewTitle, o.ReviewTitle.GetTypeCode()));
            arrParams[46] = new SqlParameter("@ReviewAuthor", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.ReviewAuthor, o.ReviewAuthor.GetTypeCode()));
            arrParams[47] = new SqlParameter("@ReviewStartDate", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.ReviewStartDate, o.ReviewStartDate.GetTypeCode()));
            arrParams[48] = new SqlParameter("@ReviewEndDate", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.ReviewEndDate, o.ReviewEndDate.GetTypeCode()));
            arrParams[49] = new SqlParameter("@RandomDrawingName", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.RandomDrawingName, o.RandomDrawingName.GetTypeCode()));
            arrParams[50] = new SqlParameter("@RandomDrawingNum", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.RandomDrawingNum, o.RandomDrawingNum.GetTypeCode()));
            arrParams[51] = new SqlParameter("@RandomDrawingStartDate", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.RandomDrawingStartDate, o.RandomDrawingStartDate.GetTypeCode()));
            arrParams[52] = new SqlParameter("@RandomDrawingEndDate", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.RandomDrawingEndDate, o.RandomDrawingEndDate.GetTypeCode()));
            arrParams[53] = new SqlParameter("@HasBeenDrawn", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.HasBeenDrawn, o.HasBeenDrawn.GetTypeCode()));
            arrParams[54] = new SqlParameter("@HasRedeemend", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.HasRedeemend, o.HasRedeemend.GetTypeCode()));
            arrParams[55] = new SqlParameter("@PIDInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PIDInc, o.PIDInc.GetTypeCode()));
            arrParams[56] = new SqlParameter("@UsernameInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.UsernameInc, o.UsernameInc.GetTypeCode()));
            arrParams[57] = new SqlParameter("@DOBInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.DOBInc, o.DOBInc.GetTypeCode()));
            arrParams[58] = new SqlParameter("@AgeInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.AgeInc, o.AgeInc.GetTypeCode()));
            arrParams[59] = new SqlParameter("@SchoolGradeInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.SchoolGradeInc, o.SchoolGradeInc.GetTypeCode()));
            arrParams[60] = new SqlParameter("@FirstNameInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FirstNameInc, o.FirstNameInc.GetTypeCode()));
            arrParams[61] = new SqlParameter("@LastNameInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.LastNameInc, o.LastNameInc.GetTypeCode()));
            arrParams[62] = new SqlParameter("@GenderInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.GenderInc, o.GenderInc.GetTypeCode()));
            arrParams[63] = new SqlParameter("@EmailAddressInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.EmailAddressInc, o.EmailAddressInc.GetTypeCode()));
            arrParams[64] = new SqlParameter("@PhoneNumberInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PhoneNumberInc, o.PhoneNumberInc.GetTypeCode()));
            arrParams[65] = new SqlParameter("@CityInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.CityInc, o.CityInc.GetTypeCode()));
            arrParams[66] = new SqlParameter("@StateInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.StateInc, o.StateInc.GetTypeCode()));
            arrParams[67] = new SqlParameter("@ZipCodeInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.ZipCodeInc, o.ZipCodeInc.GetTypeCode()));
            arrParams[68] = new SqlParameter("@CountyInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.CountyInc, o.CountyInc.GetTypeCode()));
            arrParams[69] = new SqlParameter("@PrimaryLibraryInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PrimaryLibraryInc, o.PrimaryLibraryInc.GetTypeCode()));
            arrParams[70] = new SqlParameter("@SchoolNameInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.SchoolNameInc, o.SchoolNameInc.GetTypeCode()));
            arrParams[71] = new SqlParameter("@DistrictInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.DistrictInc, o.DistrictInc.GetTypeCode()));
            arrParams[72] = new SqlParameter("@TeacherInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.TeacherInc, o.TeacherInc.GetTypeCode()));
            arrParams[73] = new SqlParameter("@GroupTeamNameInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.GroupTeamNameInc, o.GroupTeamNameInc.GetTypeCode()));
            arrParams[74] = new SqlParameter("@SchoolTypeInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.SchoolTypeInc, o.SchoolTypeInc.GetTypeCode()));
            arrParams[75] = new SqlParameter("@LiteracyLevel1Inc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.LiteracyLevel1Inc, o.LiteracyLevel1Inc.GetTypeCode()));
            arrParams[76] = new SqlParameter("@LiteracyLevel2Inc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.LiteracyLevel2Inc, o.LiteracyLevel2Inc.GetTypeCode()));
            arrParams[77] = new SqlParameter("@Custom1Inc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Custom1Inc, o.Custom1Inc.GetTypeCode()));
            arrParams[78] = new SqlParameter("@Custom2Inc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Custom2Inc, o.Custom2Inc.GetTypeCode()));
            arrParams[79] = new SqlParameter("@Custom3Inc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Custom3Inc, o.Custom3Inc.GetTypeCode()));
            arrParams[80] = new SqlParameter("@Custom4Inc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Custom4Inc, o.Custom4Inc.GetTypeCode()));
            arrParams[81] = new SqlParameter("@Custom5Inc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Custom5Inc, o.Custom5Inc.GetTypeCode()));
            arrParams[82] = new SqlParameter("@RegistrationDateInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.RegistrationDateInc, o.RegistrationDateInc.GetTypeCode()));
            arrParams[83] = new SqlParameter("@PointsInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PointsInc, o.PointsInc.GetTypeCode()));
            arrParams[84] = new SqlParameter("@EarnedBadgeInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.EarnedBadgeInc, o.EarnedBadgeInc.GetTypeCode()));
            arrParams[85] = new SqlParameter("@PhysicalPrizeNameInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PhysicalPrizeNameInc, o.PhysicalPrizeNameInc.GetTypeCode()));
            arrParams[86] = new SqlParameter("@PhysicalPrizeDateInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PhysicalPrizeDateInc, o.PhysicalPrizeDateInc.GetTypeCode()));
            arrParams[87] = new SqlParameter("@NumReviewsInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.NumReviewsInc, o.NumReviewsInc.GetTypeCode()));
            arrParams[88] = new SqlParameter("@ReviewAuthorInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.ReviewAuthorInc, o.ReviewAuthorInc.GetTypeCode()));
            arrParams[89] = new SqlParameter("@ReviewTitleInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.ReviewTitleInc, o.ReviewTitleInc.GetTypeCode()));
            arrParams[90] = new SqlParameter("@ReviewDateInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.ReviewDateInc, o.ReviewDateInc.GetTypeCode()));
            arrParams[91] = new SqlParameter("@RandomDrawingNameInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.RandomDrawingNameInc, o.RandomDrawingNameInc.GetTypeCode()));
            arrParams[92] = new SqlParameter("@RandomDrawingNumInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.RandomDrawingNumInc, o.RandomDrawingNumInc.GetTypeCode()));
            arrParams[93] = new SqlParameter("@RandomDrawingDateInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.RandomDrawingDateInc, o.RandomDrawingDateInc.GetTypeCode()));
            arrParams[94] = new SqlParameter("@HasBeenDrawnInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.HasBeenDrawnInc, o.HasBeenDrawnInc.GetTypeCode()));
            arrParams[95] = new SqlParameter("@HasRedeemendInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.HasRedeemendInc, o.HasRedeemendInc.GetTypeCode()));
            arrParams[96] = new SqlParameter("@LastModDate", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.LastModDate, o.LastModDate.GetTypeCode()));
            arrParams[97] = new SqlParameter("@LastModUser", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.LastModUser, o.LastModUser.GetTypeCode()));
            arrParams[98] = new SqlParameter("@AddedDate", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.AddedDate, o.AddedDate.GetTypeCode()));
            arrParams[99] = new SqlParameter("@AddedUser", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.AddedUser, o.AddedUser.GetTypeCode()));
            arrParams[100] = new SqlParameter("@SDistrict", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.SDistrict, o.SDistrict.GetTypeCode()));
            arrParams[101] = new SqlParameter("@SDistrictInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.SDistrictInc, o.SDistrictInc.GetTypeCode()));

            arrParams[102] = new SqlParameter("@TenID", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.TenID, o.TenID.GetTypeCode()));
            arrParams[103] = new SqlParameter("@FldInt1", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldInt1, o.FldInt1.GetTypeCode()));
            arrParams[104] = new SqlParameter("@FldInt2", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldInt2, o.FldInt2.GetTypeCode()));
            arrParams[105] = new SqlParameter("@FldInt3", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldInt3, o.FldInt3.GetTypeCode()));
            arrParams[106] = new SqlParameter("@FldBit1", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldBit1, o.FldBit1.GetTypeCode()));
            arrParams[107] = new SqlParameter("@FldBit2", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldBit2, o.FldBit2.GetTypeCode()));
            arrParams[108] = new SqlParameter("@FldBit3", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldBit3, o.FldBit3.GetTypeCode()));
            arrParams[109] = new SqlParameter("@FldText1", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldText1, o.FldText1.GetTypeCode()));
            arrParams[110] = new SqlParameter("@FldText2", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldText2, o.FldText2.GetTypeCode()));
            arrParams[111] = new SqlParameter("@FldText3", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldText3, o.FldText3.GetTypeCode()));

            arrParams[112] = new SqlParameter("@Score1From", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Score1From, o.Score1From.GetTypeCode()));
            arrParams[113] = new SqlParameter("@Score1To", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Score1To, o.Score1To.GetTypeCode()));
            arrParams[114] = new SqlParameter("@Score1PctFrom", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Score1PctFrom, o.Score1PctFrom.GetTypeCode()));
            arrParams[115] = new SqlParameter("@Score1PctTo", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Score1PctTo, o.Score1PctTo.GetTypeCode()));
            arrParams[116] = new SqlParameter("@Score2From", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Score2From, o.Score2From.GetTypeCode()));
            arrParams[117] = new SqlParameter("@Score2To", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Score2To, o.Score2To.GetTypeCode()));
            arrParams[118] = new SqlParameter("@Score2PctFrom", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Score2PctFrom, o.Score2PctFrom.GetTypeCode()));
            arrParams[119] = new SqlParameter("@Score2PctTo", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Score2PctTo, o.Score2PctTo.GetTypeCode()));

            arrParams[120] = new SqlParameter("@Score1Inc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Score1Inc, o.Score1Inc.GetTypeCode()));
            arrParams[121] = new SqlParameter("@Score2Inc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Score2Inc, o.Score2Inc.GetTypeCode()));
            arrParams[122] = new SqlParameter("@Score1PctInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Score1PctInc, o.Score1PctInc.GetTypeCode()));
            arrParams[123] = new SqlParameter("@Score2PctInc", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Score2PctInc, o.Score2PctInc.GetTypeCode()));

            try
            {

                iReturn = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "app_ReportTemplate_Update", arrParams);

            }

            catch (SqlException exx)
            {

                System.Diagnostics.Debug.Write(exx.Message);

            }

            return iReturn;

        }