Answered by:
can i put datatable in cache in webmethod in asp.net?

Question
-
User592456772 posted
what i want to do is to keep my datatable in cache so that each time my webmethod called from the ajax autocomplete extender my data table dosen't gets filled from database again and again. To be brief i dont want to interact with database everytime i press a key in textbox. can this be done? Also The Ajax autocomplete extender is working dead slow why? my code is as below:
[System.Web.Script.Services.ScriptMethod()] [System.Web.Services.WebMethod] public static List<string> GetCountries(string prefixText, int count) { DataTable dt = new DataTable(); User objUser = new User(); dt = objUser.GetDatatable(); string Text = prefixText.ToUpper().ToString(); List<string> LST = new List<string>(); var query = from t in dt.AsEnumerable() where t.Field<string>("DeptName").ToUpper().Contains(Text) select new { DeptName = t.Field<string>("DeptName"), }; foreach (var i in query.Take(5)) { LST.Add(i.DeptName.ToString().Trim()); } return LST; }
an caching be done in webmethod?Thank you.
Tuesday, November 19, 2013 6:30 AM
Answers
-
User-484054684 posted
Option 1: You should be able to use HttpContext.Cache
Reference: http://forums.asp.net/t/1396653.aspx
Option 2: You can also enable server side caching of the whole output for individual WebMethods like below:
[WebMethod(CacheDuration=60)]
Reference: http://msdn.microsoft.com/en-us/library/y4d1bfdb(v=vs.80).aspx- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 19, 2013 7:35 AM -
User1508394307 posted
If list is not big and does not change every time then you can try to use cache
public static List<string> GetCountries(string prefixText, int count)
{
if (Cache["Key1"] != null) {
return (List<string>) Cache["Key1"];} else {
DataTable dt = new DataTable();
...
Cache.Insert("Key1", LST);
return LST;}
}You can also set time of expiration, etc. read more here http://msdn.microsoft.com/en-us/library/system.web.caching.cache_methods(v=vs.110).aspx
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 19, 2013 7:48 AM -
User-488622176 posted
- Simple : http://support.microsoft.com/kb/318299
- Other options : http://forums.asp.net/t/1746734.aspx
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 19, 2013 9:13 AM
All replies
-
User-484054684 posted
Option 1: You should be able to use HttpContext.Cache
Reference: http://forums.asp.net/t/1396653.aspx
Option 2: You can also enable server side caching of the whole output for individual WebMethods like below:
[WebMethod(CacheDuration=60)]
Reference: http://msdn.microsoft.com/en-us/library/y4d1bfdb(v=vs.80).aspx- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 19, 2013 7:35 AM -
User1508394307 posted
If list is not big and does not change every time then you can try to use cache
public static List<string> GetCountries(string prefixText, int count)
{
if (Cache["Key1"] != null) {
return (List<string>) Cache["Key1"];} else {
DataTable dt = new DataTable();
...
Cache.Insert("Key1", LST);
return LST;}
}You can also set time of expiration, etc. read more here http://msdn.microsoft.com/en-us/library/system.web.caching.cache_methods(v=vs.110).aspx
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 19, 2013 7:48 AM -
User-488622176 posted
- Simple : http://support.microsoft.com/kb/318299
- Other options : http://forums.asp.net/t/1746734.aspx
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 19, 2013 9:13 AM