protected void Page_Load(object sender, EventArgs e)
        {
            RedirectToLoginIfAnonymous();
            RedirectToLoginIfNecessary();

            service = new OrganizationService("Xrm");

            var portalContext = Microsoft.Xrm.Portal.Configuration.PortalCrmConfigurationManager.CreatePortalContext();

            var contact = portalContext.User;

            //var redemreqs = Enumerable.Empty<Entity>();
            //var searchQuery = Request["query"];
            //Guid giftid;

            string WebUrl = WebConfigurationManager.ConnectionStrings["Xrm"].ConnectionString;
            int    index  = WebUrl.IndexOf("=") + 1;
            int    index2 = WebUrl.IndexOf(";");

            WebUrl = WebUrl.Substring(index, index2 - index);
            //UrlHelper urlhelper;

            var redemreqs =
                from r in XrmContext.CreateQuery("new_redemptionrequest")
                join g in XrmContext.CreateQuery("new_gift") on r.GetAttributeValue <EntityReference>("new_gift").Id equals
                g.GetAttributeValue <Guid>("new_giftid")
                    where r.GetAttributeValue <EntityReference>("new_gift") != null
                    where
                r.GetAttributeValue <EntityReference>("new_contact").Id == contact.Id &&
                r.GetAttributeValue <bool>("new_isused") == false
                select new
            {
                redemreqid  = r.Id,
                EntityImage = "data:image/png;base64," + Convert.ToBase64String(g.GetAttributeValue <byte[]>("entityimage")),
                code        = r.GetAttributeValue <string>("new_name"),
                giftName    = g.GetAttributeValue <string>("new_name")
            };

            _redemreqs = EnumerableExtensions.CopyToDataTable(redemreqs);

            /*
             * _redemreqs = EnumerableExtensions.CopyToDataTable(redemreqs.Select(redemreq => new
             * {
             *  redemreqid = redemreq.GetAttributeValue<Guid>("new_redemptionrequestid"),
             *  //Image = gift.GetRelatedEntity(ServiceContext, new Relationship("new_gift_Annotations")) != null ? gift.GetRelatedEntity(ServiceContext, new Relationship("new_gift_Annotations")).GetAttributeValue<string>("documentbody") : " ",
             *  //EntityImage = WebUrl + redemreq.GetAttributeValue<string>("entityimage_url"),
             *  //EntityImage = WebUrl + redemreq.GetRelatedEntity(ServiceContext, new Relationship("new_new_gift_new_redemptionrequest_Gift")) != null ? redemreq.GetRelatedEntity(ServiceContext, new Relationship("new_new_gift_new_redemptionrequest_Gift")).GetAttributeValue<string>("entityimage_url") : " ",
             *  Code = redemreq.GetAttributeValue<string>("new_name"),
             *  GiftName = redemreq.GetRelatedEntity(ServiceContext, "new_new_gift_new_redemptionrequest_Gift") != null ? redemreq.GetRelatedEntity(ServiceContext, "new_new_gift_new_redemptionrequest_Gift").GetAttributeValue<string>("new_name") : " ",
             * }));//.OrderBy(gift => gift.GiftName));
             */
            _redemreqs.Columns["EntityImage"].ColumnName = "Gift Image";
            _redemreqs.Columns["Code"].ColumnName        = "Redemption No.";
            _redemreqs.Columns["GiftName"].ColumnName    = "Gift Name";

            MyGiftListView.DataKeyNames = new[] { "redemreqid" };
            MyGiftListView.DataSource   = _redemreqs;
            MyGiftListView.DataBind();
        }
        protected void AcceptedMyGiftList_Sorting(object sender, GridViewSortEventArgs e)
        {
            SortDirection = e.SortExpression == SortExpression
                ? (SortDirection == "ASC" ? "DESC" : "ASC")
                : "ASC";

            SortExpression = e.SortExpression;

            _redemreqs.DefaultView.Sort = string.Format("{0} {1}", SortExpression, SortDirection);

            MyGiftListView.DataSource = _redemreqs;
            MyGiftListView.DataBind();
        }