This is a migrated thread and some comments may be shown as answers.

[Solved] Dynamically binding the Data Grid creates problem

1 Answer 140 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Uttam Dhakal
Top achievements
Rank 1
Uttam Dhakal asked on 19 Apr 2013, 10:52 PM
I am trying to create a Data Grid in a web part which will be loaded dynamically with user activity feeds.
Users are calculated according to the input provided by the user.
I am using email address as the input so at one time you can display one users events and again change the email address click the button that displays another user's email address.

I have written the code but i am not getting desired out put.
Any assistance please. I am having problem with the needdatasource.

the problem here is i dont see the data table it says null reference to the object at / application.
my datasource is null at first time this page loads because i have not calcualated that. once the page loads then i get the input which will calculate the data source.
other time if i change the email address i get the user changed but again the datasource is not changed and i dont see new data table with new data or if there is no data i should see no records.
any help will be very much appreciated please..............

Here is my code
public partial class CustomFeedUserControl : UserControl
 
    {
 
        private const string ActivityName = "ActivityName";
 
        private const string SiteName = "SiteName";
 
        private const string Date = "Date";
 
        private const string ActivitySummary = "ActivitySummary";
 
        private DataTable sitedetailstable;
 
        private ActivityManager activityManager;
 
        private ActivityEventsCollection activityEvents;
 
        private Boolean error = false;
 
        private string errorText = "";
 
        SyndicationItem syndicationItem;
 
        List<String> categories = new List<string>();
 
        SPWebApplication webapp = SPContext.Current.Site.WebApplication;
 
  
 
  
 
        private String Email = "";
 
  
 
  
 
        protected void Page_Load(object sender, EventArgs e)
 
        {
            //if (!Page.IsPostBack) Nothing here as i am creating the grid ondemand
            //{
            //    LoadData();
            //    RadGridSiteDetails.Visible = false;
            //}        
 
        }
 
  
 
        void LoadData()
 
        {
            RadGridSiteDetails.DataSource = Sitedetailstable.DefaultView;
        }
 
  
 
        public void LoadDatasource(String userloginname)
 
        {
            string siteUrl = SPContext.Current.Site.Url;
 
            SPSecurity.RunWithElevatedPrivileges(delegate
 
            {
                using (SPSite site = new SPSite(siteUrl))
                {
                    try
                    {
                        SPServiceContext context = SPServiceContext.GetContext(site);
                        UserProfileManager userProfileManager = new UserProfileManager(context);
                        UserProfile profile = userProfileManager.GetUserProfile(userloginname);
                        if (profile != null)
                        {
                            test.Text = profile.DisplayName;
                            this.activityManager = new ActivityManager(profile);
                            this.activityEvents = this.activityManager.GetActivitiesByUser(profile);
                           }
                    }
 
                    catch (UserNotFoundException ex)
 
                    {
                        this.error = true;
                        this.errorText = ex.Message;
                   }
 
                    catch (NullReferenceException ex)
                    {
                        this.error = true;
                        this.errorText = ex.Message;
                    }
 
                }
 
            });
        }
 
  
 
 
 
        private DataTable ReturnUserDatadetails( )
 
        {
            sitedetailstable = new DataTable();
            string sitetitle = null;
            string summary;
            sitedetailstable.Columns.Add(ActivityName, System.Type.GetType("System.String"));
            sitedetailstable.Columns.Add(SiteName, System.Type.GetType("System.String"));
            sitedetailstable.Columns.Add(Date, System.Type.GetType("System.String"));
            sitedetailstable.Columns.Add(ActivitySummary, System.Type.GetType("System.String"));
            if (this.activityEvents.Count > 0)
 
            {
                foreach (ActivityEvent activityEvent in this.activityEvents)
                {
      syndicationItem = activityEvent.CreateSyndicationItem(this.activityManager.ActivityTypes, ContentType.Html);
                 Sitetitle = syndicationItem.Summary.Text.ToString();
                    summary = Regex.Replace(syndicationItem.Summary.Text;
                    if ((Sitetitle != null) && (Sitetitle != ""))
                    {
                        DataRow newrow = Sitedetailstable.NewRow();
                        newrow[ActivityName] = activityEvent.Name;
                        newrow[SiteName] = Sitetitle;
                        newrow[Date] = activityEvent.PublishDate.HasValue ?
                         activityEvent.PublishDate.Value.ToLocalTime().ToString("dd MMM yyyy hh:mm tt") :
                          string.Empty;
                        newrow[ActivitySummary] = summary.Substring(summary.IndexOf(":") + 1);
                        categories.Add(activityEvent.Name.ToString());
                        Sitedetailstable.Rows.Add(newrow);
                    }
 
                }
 
  
 
            }
                return Sitedetailstable;
 
        }
 
  
 
        protected void RadGridSiteDetails_SortCommand(object sender, Telerik.Web.UI.GridSortCommandEventArgs e)
 
        {
 
            LoadData();
        }
 
  
 // this is the button event that gets clicked from the user to generate reporty
        protected void UserReport_Click(object sender, EventArgs e)
 
        {
            Email = UserEmailInput.Text;
            SPPrincipalInfo user = SPUtility.ResolvePrincipal(webapp, null, Email, SPPrincipalType.User, SPPrincipalSource.All, true);
            if (user != null)
            {
                LoadDatasource(user.LoginName);
                LoadData();
               RadGridSiteDetails.Visible = true;
 
            }
 
  
 
            else
 
            {
 
                test.Text = "No user Found with that Email Address";
 
            }
 
  
 
        }
 
  
 
        protected void RadGridSiteDetails_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
 
        {
 
            LoadData();
 
        }
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
    }

1 Answer, 1 is accepted

Sort by
0
Iris
Top achievements
Rank 1
answered on 22 Apr 2013, 07:15 AM
I believe I had almost the same problem before. Please see my last post in this thread for my solution to the problem, in case it can help you with yours: http://www.telerik.com/community/forums/aspnet-ajax/grid/grid-disappears-after-clicking-edit.aspx
Tags
Grid
Asked by
Uttam Dhakal
Top achievements
Rank 1
Answers by
Iris
Top achievements
Rank 1
Share this question
or