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

RadGrid NeedsDataSource, IQueryable<> and disposing of datacontext

2 Answers 183 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Wired_Nerve
Top achievements
Rank 2
Wired_Nerve asked on 09 May 2014, 03:32 PM
I have a radgrid that calls NeedsDataSource.  
I have a method like this:

protected void RadGridParentComponents_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
       {
           var result = new Inventory().GetComponets(this.HiddenFieldTag.Value);
           RadGridParentComponents.DataSource = result;
       }

Which in turn calls this bll bit of code... GetComponents
public IQueryable<TagInformationComponentView> GetComponets(string tag)
       {
           using (var dbContext = new TIPWebITLibrary.DAL.TIPWebITDataContext())
           {
               if (String.IsNullOrEmpty(tag)) { throw new ArgumentNullException("tag", "Null or Empty is not allowed"); }
               var parent = dbContext.tblTechInventories.Where(x => x.Tag == tag).FirstOrDefault();
               //TODO: Check to ensure it is a parent
               var result = from c in dbContext.tblTechInventories
                            join p in dbContext.tblTechItems on c.ItemUID equals p.ItemUID
                            join t in dbContext.tblTechItemTypes on p.ItemTypeUID equals t.ItemTypeUID
                            where c.ParentInventoryUID == parent.InventoryUID
                            select new TagInformationComponentView()
                            {
                                InventoryUID = c.InventoryUID,
                                ProductName = p.ItemName,
                                ProductType = t.ItemTypeName,
                                Serial = c.Serial,
                                Tag = c.Tag
                            };
 
               if (result.Count() == 0)
                   return new List<TagInformationComponentView>().AsQueryable();
 
               return result.AsQueryable();
           }
 
 
       }


Now the question:

I know I can't wrap this in a USING because the context will be disposed and the RadGrid does not like this...
How do I dispose of the context when I am done with it correctly?  
I have used .toList() in the past but from what i have read this method does not leverage the full power of the radgrids queryable functionality.. Am i wrong in this or what?

2 Answers, 1 is accepted

Sort by
0
Wired_Nerve
Top achievements
Rank 2
answered on 13 May 2014, 12:54 PM
Anyone?

0
Antonio Stoilkov
Telerik team
answered on 14 May 2014, 06:19 AM
Hi Warren,

In order to achieve your scenario you could dispose the context on page unload event as shown in the example below. Another possible approach is to use the .ToList() approach. Note that there is no performance difference in using the .ToList() and using the IQueryable.
protected override void OnUnload(EventArgs e)
{
    base.OnUnload(e);
 
    // dispose
}

Regards,
Antonio Stoilkov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Wired_Nerve
Top achievements
Rank 2
Answers by
Wired_Nerve
Top achievements
Rank 2
Antonio Stoilkov
Telerik team
Share this question
or