Can I reuse a Query?
In Form.Load()
Users = from u in Db.UserTbls select u;
var
mGridView.DataSource = Users;
mGridView.DataBind();
In mGirdView.Sorting()
IQueryable<UserTbl> Users = mGridView.DataSource as IQueryable<UserTbl>;
Users.OrderBy<UserTbl, string>(u => u.Name);
Two questions:
1. Why DataSource == null?
2. If it is not null, can the DataSource, or the Users be reuse? Or must I construct a new query and re-query the db everytime the user click on sort?
Thanks in advance.
Answers
Hi Chui Kean,
In an ASP.NET application, even if your query returns some records, the datasource could be null in a post back event. This is due to stateless behavior of ASP.NET webpages. There can be solved using some caching mechanisms or sessions, viewstates, application states etc. As Yichun suggested, you may get better ideas on this from the ASP.NET forums. You may also want to read up some info on asp.net state management here.
Regards,
Syed Mehroz Alam
My Blog | My Articles- Marked As Answer byYichun_FengMSFT, ModeratorThursday, November 12, 2009 2:29 AM
All Replies
- Hi Kean,
If the datasource is null, that means you don't get any records by the query.
One feature of LINQ is that you can get the newest records in DB. To make sure that the query do not get the records form cache, you can set the gridview's datasoure to null, and then set it with the query.
Since you are working with APS.NET project, it is better that you post in ASP.NET forum,
Data Presentation Controls Forum for ASP.NET
http://forums.asp.net/24.aspx
There are more experts on ASP.NET.
Best Regards
Yichun Feng
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Hi Chui Kean,
In an ASP.NET application, even if your query returns some records, the datasource could be null in a post back event. This is due to stateless behavior of ASP.NET webpages. There can be solved using some caching mechanisms or sessions, viewstates, application states etc. As Yichun suggested, you may get better ideas on this from the ASP.NET forums. You may also want to read up some info on asp.net state management here.
Regards,
Syed Mehroz Alam
My Blog | My Articles- Marked As Answer byYichun_FengMSFT, ModeratorThursday, November 12, 2009 2:29 AM


