protected void Page_Load(object sender, EventArgs e)
        {
            _inBloomApi = new inBloomApi();
            _sqlHelper = new SqlHelper();
            _user = (User) Session["UserDetail"];

            try
            {
                _inBloomApi.AccessToken = Session[SessionEnum.AccessToken.ToString()].ToString();
            }
            catch (Exception ex)
            {
                Response.Redirect("Search.aspx");
            }

            if (!IsPostBack)
            {
                try
                {
                    if (!IsPostBack)
                    {
                        if (Session[SessionEnum.SchoolId.ToString()] == null)
                        {
                        }
                        else
                        {
                            LoadFieldNames();

                            _schoolId = Session[SessionEnum.SchoolId.ToString()].ToString();
                            _courseId = Session[SessionEnum.CourseId.ToString()].ToString();
                            _sectionId = Session[SessionEnum.SectionId.ToString()].ToString();

                            _user = (User) Session["UserDetail"];

                            if (Session["FlagId"] == null || Session["FlagType"] == null)
                            {
                                // show result for school and courses only.
                                Response.Redirect("Search.aspx");
                            }
                            else
                            {
                                _flagId = Convert.ToInt16(Session["FlagId"].ToString());
                                _flagType = Session["FlagType"].ToString();

                                if (_flagType.Equals("Flag"))
                                {
                                    // get all the values for flag
                                    IList<Flag> _flag = _sqlHelper.GetFlag(_flagId);

                                    lblFlagName.Text = _flag[0].FlagName;
                                    lblFlagDescription.Text = _flag[0].FlagDescription;

                                    if (_flag.Count > 0)
                                    {
                                        int DataElementId = _flag[0].DataElementId;
                                        int ConditionId = _flag[0].ConditionId;
                                        _value1 = _flag[0].ValueSet1;
                                        _value2 = _flag[0].ValueSet2;
                                        _flagUser = _flag[0].UserId;

                                        _dataType = _sqlHelper.GetDataTypeByDataElementId(DataElementId);
                                        _fieldName = _sqlHelper.GetFieldNameByDataElementId(DataElementId);
                                        _entityName = _sqlHelper.GetEntityNameByDataElementId(DataElementId);

                                        DataTable dt = _inBloomApi.RunFlag(_fieldName, _dataType, _responseType, _flagUser, _schoolId, _courseId, _sectionId, ConditionId, _value1, _value2, _entityName, _isPreview);

                                        Session.Add("FieldName", _fieldName);
                                        gvResult.DataSource = dt;
                                        gvResult.DataBind();

                                        //gvResult.HeaderRow.Cells[3].Text = _inBloomApi.GetWellFormattedString(FieldName);
                                        for (int i = 0; i < gvResult.HeaderRow.Cells.Count; i++)
                                        {
                                            String HeaderText = gvResult.HeaderRow.Cells[i].Text;
                                            if (HeaderText == "FieldName")
                                            {

                                                gvResult.HeaderRow.Cells[i].Text = _inBloomApi.GetWellFormattedString(char.ToUpper(_entityName[0]) + _entityName.Substring(1)) + "." + _inBloomApi.GetWellFormattedString(_fieldName);

                                                break;
                                            }
                                        }

                                        HideColumnInitial();

                                        Session.Add("ResultGrid", dt);

                                        if (_sqlHelper.IsFav(_flagId) || _sqlHelper.IsInPublicFavorite(_flagId, _user.ExternalId))
                                        {
                                            btnlnkfavorite.Text = "Remove Favorite";
                                            btnlnkfavorite.CssClass = "";
                                        }
                                        else
                                        {
                                            btnlnkfavorite.Text = "Add to Favorite";
                                            btnlnkfavorite.CssClass = "unfavorite_result";
                                        }
                                    }
                                }
                                else if (_flagType.Equals("AggregateFlag"))
                                {
                                    try
                                    {
                                        IList<AggregateFlag> _aggregateFlag = _sqlHelper.GetAggregateFlag(_flagId);

                                        lblFlagName.Text = _aggregateFlag[0].AggregateFlagName;
                                        lblFlagDescription.Text = _aggregateFlag[0].AggregateFlagDescription;

                                        int[] FlagArrayId = _sqlHelper.GetAllFlagIdOfAggregateFlagByAggregateFlagId(_flagId);

                                        DataTable dt = _inBloomApi.RunAggregateFlag(FlagArrayId, _schoolId, _courseId, _sectionId);

                                        GridViewAggregateFlag.DataSource = dt;
                                        Session.Add("ResultGrid", dt);
                                        GridViewAggregateFlag.DataBind();
                                        for (int i = 0; i < GridViewAggregateFlag.HeaderRow.Cells.Count; i++)
                                        {
                                            //GridViewAggregateFlag.HeaderRow.Cells[i].Text = _inBloomApi.GetWellFormattedString(GridViewAggregateFlag.HeaderRow.Cells[i].Text);
                                        }

                                        HideColumnInitialForAggregate();

                                        //GridViewAggregateFlag.HeaderRow.Cells[0].Visible = false;

                                        if (_sqlHelper.IsAggregateIsFav(_flagId) || _sqlHelper.IsAggregateInPublicFavorite(_flagId, _user.ExternalId))
                                        {
                                            btnlnkfavorite.Text = "Remove Favorite";
                                            btnlnkfavorite.CssClass = "";
                                            btnlnkfavorite.Attributes.Add("onclick", "showunfavmsg();");

                                        }
                                        else
                                        {
                                            btnlnkfavorite.Text = "Add to Favorite";
                                            btnlnkfavorite.CssClass = "unfavorite_result";
                                            btnlnkfavorite.Attributes.Add("onclick", "showfavmsg();");

                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                    }

                                }
                            }

                            //LoadStudentGrid();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Response.Redirect("Search.aspx");
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _sqlHelper = new SqlHelper();

            _inBloomApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());

            if (!IsPostBack)
            {
                _sqlHelper = new SqlHelper();
                LoadDataDomainList();
                LoadGridWithAttribute();
                lstCondition.DataSource = _sDACEntities.Conditions;
                lstCondition.DataBind();

                _user = (User)Session["UserDetail"];

                if (!_user.IsAdminUser)
                    PanelFlagType.Visible = false;

                if (Session["EditFlag"] != null)
                {
                    try
                    {
                        int FlagId = Convert.ToInt16(Session["EditFlag"]);
                        Session.Add("FlagIdForEditPage", FlagId);
                        _flagIdForPage = FlagId;

                        IList<Flag> _Flag = _sqlHelper.GetFlag(FlagId);

                        txtFlagName.Text = _Flag[0].FlagName;
                        txtDescription.Text = _Flag[0].FlagDescription;
                        txtFlag.Text = _Flag[0].Keyword;

                        String IsPublic = _Flag[0].IsPublic.ToString();

                        if (IsPublic == "True")
                        {
                            radioFlagType.SelectedIndex = 0;
                        }
                        else
                        {
                            radioFlagType.SelectedIndex = 1;
                        }

                        lstCondition.SelectedValue = _Flag[0].ConditionId.ToString();

                        if (_Flag[0].ConditionId.ToString() != null && _Flag[0].ConditionId.ToString() != "")
                        {
                            condition.Style.Add("display", "block");
                            condition.Text = lstCondition.SelectedItem.Text;
                        }

                        txtSetVal.Text = _Flag[0].ValueSet1;
                        txtSetVal2.Text = _Flag[0].ValueSet2;

                        if (_Flag[0].ValueSet1 != null && _Flag[0].ValueSet1 != "")
                        {
                            val1.Style.Add("display", "block");
                            val1.Text = _Flag[0].ValueSet1;
                        }

                        if (_Flag[0].ValueSet2 != null && _Flag[0].ValueSet2 != "")
                        {
                            val2.Style.Add("display", "block");
                            val2.Text = " & " + _Flag[0].ValueSet2;
                        }

                        _conditionId = _Flag[0].ConditionId;
                        if (condition.Text == "Is between" || condition.Text == "Is not between")
                        {
                            txtSetVal2.Style.Add("display", "block");
                        }

                        hdnDataElementId.Value = _Flag[0].DataElementId.ToString();

                        int DataElementId = _Flag[0].DataElementId;
                        hdnDataType.Value = _sqlHelper.GetDataTypeByDataElementId(DataElementId);

                        String EntityName = _sqlHelper.GetDataDomainNameByDataElementId(DataElementId);

                        // LoadEntityList();
                        LoadDataDomainList();
                        DropDownListEntity.SelectedItem.Text = EntityName;
                        LoadGridWithAttribute();

                        SelectRow(_Flag[0].DataElementId);

                        // process to call run flag

                        String Entity = _sqlHelper.GetEntityNameByDataElementId(DataElementId);
                        String FieldName = _sqlHelper.GetFieldNameByDataElementId(DataElementId);
                        String DataType = _sqlHelper.GetDataTypeByDataElementId(DataElementId);
                        bool ResponseType = false;
                        _user = (User)Session["UserDetail"];
                        String UserId = _user.ExternalId;
                        String SchoolId = Session[SessionEnum.SchoolId.ToString()].ToString();
                        String CourseId = Session[SessionEnum.CourseId.ToString()].ToString();
                        String SectionId = Session[SessionEnum.SectionId.ToString()].ToString();
                        String Value1 = txtSetVal.Text;
                        String Value2 = txtSetVal2.Text;

                       // String ExternalField = "";
                        bool IsPreview = true;

                        DataTable dt = _inBloomApi.RunFlag(FieldName, DataType, ResponseType, UserId, SchoolId, CourseId, SectionId, _conditionId, Value1, Value2, Entity, IsPreview);

                        gridDisplayResult.DataSource = dt;
                        gridDisplayResult.DataBind();
                        gridDisplayResult.HeaderRow.Cells[1].Text =_inBloomApi.GetWellFormattedString(char.ToUpper(Entity[0]) + Entity.Substring(1))+"."+_inBloomApi.GetWellFormattedString(FieldName);
                        entity.Text = _inBloomApi.GetWellFormattedString(char.ToUpper(Entity[0]) + Entity.Substring(1)) + "." + _inBloomApi.GetWellFormattedString(FieldName); ;

                    }
                    catch (Exception ex)
                    {
                    }
                }
                else
                {
                    Response.Redirect("Search.aspx");
                }
            }
        }